1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
/* $Id: signals.c,v 1.5 2005/09/14 02:30:56 rcook Exp $ */
#include <signal.h>
#include <stdio.h>
#ifdef WIN32
# include <winsock2.h>
#else
# include <netdb.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
/* set a signal handler for the given signal
sig == the signal to catch
func == the handler
useful functions are SIG_IGN, SIG_DFL
can of course pass a pointer to your own sigfunc
e.g.,
void myfunc(int sig);
if (!setsignal(SIGHUP, myfunc))
errexit("Can't set SIGHUP handler!\n");
q.v. Stephenson's Advance Unix Programming, page 270
*/
typedef void sigfunc (int);
int setsignal(int sig, sigfunc *func){
fprintf(stderr, "setting signal %d\n", sig);
if (signal(sig, func) == SIG_ERR) {
fprintf(stderr, "Error: couldn't set signal %d\n", sig);
return 0;
}
return 1;
}
void handler(int sig) {
printf("test prog received signal %d\n", sig);
/*exit(0);*/
}
/* this is code I used to see if I was getting signaled at one point, and it might be useful*/
int GettingSignaled(void)
{
char localhost[256];
struct hostent *hep = NULL;
int i=0;
/* see if we're getting signaled, damnit!*/
setsignal(SIGINT, handler);
/*setsignal(SIGKILL, handler);*/
/*setsignal(SIGSTOP, handler);*/
setsignal(SIGTERM, handler);
setsignal(SIGABRT, handler);
#ifndef WIN32
setsignal(SIGCHLD, handler);
setsignal(SIGHUP, SIG_IGN);
setsignal(SIGPIPE, handler);
setsignal(SIGQUIT, handler);
setsignal(SIGTTIN, handler);
#endif
gethostname(localhost, 256);
hep = gethostbyname(localhost);
printf("test host is %s\n", hep->h_name);
fflush(stdout);
/* switch (fork())
{
case -1: error
{
printf("failed to fork\n");
exit (1);
}
case 0: child
{
*/
fprintf(stdout, "Child stdout.\n");
fprintf(stderr, "Child stderr new.\n");
/*fclose(stdout);*/
/*fclose(stderr);*/
i=30;
while (i--){
#ifndef WIN32
int err = usleep (999999);
if (err) {
fprintf(stderr, "child sleep err %d\n", err);
exit(1);
}
#else
Sleep(999);
#endif
printf("Child sleeping... (%d) \n", i);
fflush(stdout);
}
/*
}
default:
{
fprintf(stdout, "parent stdout.");
fprintf(stderr, "parent stderr x.");
fclose(stdout);/
fclose(stderr);/
return 0;
}
}
*/
return 0;
}
|