0

I found this basic daemon code and wanted to see AddressSanitizer summary with daemon process. The process does terminate at strcpy(mem, sentence) in main but I don't see the Summary

static void skeleton_daemon()
{
    pid_t pid;

    /* Fork off the parent process */
    pid = fork();

    /* An error occurred */
    if (pid < 0)
        exit(EXIT_FAILURE);

     /* Success: Let the parent terminate */
    if (pid > 0)
        exit(EXIT_SUCCESS);

    /* On success: The child process becomes session leader */
    if (setsid() < 0)
        exit(EXIT_FAILURE);

    signal(SIGCHLD, SIG_IGN);
    signal(SIGHUP, SIG_IGN);

    /* Fork off for the second time*/
    pid = fork();

    /* An error occurred */
    if (pid < 0)
        exit(EXIT_FAILURE);

    /* Success: Let the parent terminate */
    if (pid > 0)
        exit(EXIT_SUCCESS);

    /* Set new file permissions */
    umask(0);

    chdir("/");

    /* Close all open file descriptors */
    int x;
    for (x = sysconf(_SC_OPEN_MAX); x>=0; x--)
    {
        close (x);
    }

    /* Open the log file */
    openlog ("firstdaemon", LOG_PID, LOG_DAEMON);
}

int main()
{
    skeleton_daemon();

    while (1)
    {
        syslog (LOG_NOTICE, "First daemon started."); // I only see this
        sleep (20);
        char sentence[] = "Hello, this is a sentence1.";
        char *mem = malloc(10 * sizeof(char));
        strcpy(mem, sentence); // address sanitizer should show output here.
        syslog (LOG_NOTICE, "Didn't hit sanitizer.");
        break;
    }

    syslog (LOG_NOTICE, "First daemon terminated.");
    closelog();

    return EXIT_SUCCESS;
}

compile: gcc12 -g3 -fsanitize=address -fsanitize=leak -static-libasan -o firstdaemon mydeamon.c

Nabz C
  • 538
  • 1
  • 9
  • 28
  • 1
    Your best bet would probably be to redirect the daemon's `stderr` (I assume that's where address sanitizer sends its messages) to a file. Background processes generally cannot produce output to their controlling terminal, if they even have one. – John Bollinger Mar 14 '23 at 20:34
  • Yup, that works – Nabz C Mar 14 '23 at 21:45

0 Answers0