/* sig_src - tells you the source PID of a SIGHUP signal * * Steps: * 1. rename your target_program to target_program_real. * 2. The compile this program and name it target_program * 3. Go! sig_src (pretending to be target_program) will * launch your real target program, while standing by to * intercept the SIGHUP * * * Written by Marc Randolph (mrand) * This source code is provided for unrestricted use. * Users may copy or modify this source code without charge. * * Rev history * 2.0 - 12 May 2009 - release to the wild * */ #define __USE_GNU #include #include #include #include static void sigHandler ( int sig, siginfo_t *info, void *data ) { if ( info == NULL ) { fprintf ( stderr, "signal: [%d]\n", sig ); return; } else { fprintf ( stderr, "signo: [%d], errno: [%d], code: [%d], pid: [%d], uid: [%d]\n", info->si_signo, /* same as sig */ info->si_errno, info->si_code, info->si_pid, info->si_uid ); } } int main ( int argc, char *argv[] ) { char *suffix = "_real\0"; char *args, *args_save; int argc_cnt, i; int char_cnt = 0; struct sigaction act; pid_t child_pid; memset ( &act, '\0', sizeof ( struct sigaction ) ); /* Set up signal handler to catch SIGHUP */ act.sa_sigaction = &sigHandler; act.sa_flags |= SA_SIGINFO; sigemptyset ( &act.sa_mask ); sigaction ( SIGHUP, &act, NULL ); /* count the number of characters in the argv list */ for (argc_cnt = 0; argc_cnt < argc; argc_cnt++) char_cnt += strlen ( argv[argc_cnt] ); /* calloc ( number of characters in argv + suffix + spaces + a null ) */ args = calloc ( char_cnt + strlen ( suffix ) + argc_cnt + 1, 1); args_save = args; if (NULL == args) { fprintf(stderr, "No memory allocated. Aborted.\n"); exit ( 1 ); } char_cnt = 0; /* Now combine argv list into a single null terminated string */ for (argc_cnt = 0; argc_cnt < argc; argc_cnt++) { args = mempcpy ( args, argv[argc_cnt], strlen( argv[argc_cnt] ) ); /* Special case: append first arg (our program name) with suffix */ if (0 == argc_cnt) args = mempcpy (args, suffix, strlen(suffix) ); *args++ = ' '; /* add a space between every argument */ } /* memory was null terminated to begin with */ fprintf(stderr, "Executing (%s)\n", args_save); #if 1 child_pid = fork(); if (0 == child_pid) { /* child */ system(args_save); } else { // free(args); while (1) pause(); } #endif return ( 0 ); }