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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef INT64
void mrccend_(long*);
void dmrccend_(long*);
long ERROR_SIGTERM = -1;
long ERROR_SIGSEGV = -2;
#else
void mrccend_(int*);
void dmrccend_(int*);
int ERROR_SIGTERM = -1;
int ERROR_SIGSEGV = -2;
#endif
void parent_sigterm_(int, siginfo_t *, void *);
void child_sigterm_(int, siginfo_t *, void *);
void child_sigsegv_(int, siginfo_t *, void *);
void sendSignalToChildren(int);
static struct sigaction old_act;
void signalinit_() {
// initialise dmrcc's responses to signals
struct sigaction act_term;
memset(&act_term, '\0', sizeof(act_term));
act_term.sa_sigaction = &parent_sigterm_;
act_term.sa_flags = SA_SIGINFO;
sigaction(SIGTERM, &act_term, NULL);
}
void parent_sigterm_(int signum, siginfo_t *siginfo, void *context) {
// initialise response to signal SIGTERM
pid_t pid_parent;
char pidchar[10];
char command[40];
pid_parent = getpid();
sprintf(pidchar, "%d", pid_parent);
printf("\n Program dmrcc recieved SIGTERM\n"); fflush(stdout);
sendSignalToChildren(SIGTERM);
sleep(5);
sendSignalToChildren(SIGKILL);
printf("\n Program dmrcc terminating\n"); fflush(stdout);
dmrccend_(&ERROR_SIGTERM);
}
void sendSignalToChildren(int sig) {
int ownPid = getpid();
int pid, numPids = 0;
int *pids;
FILE *pidfile = fopen("pids", "r");
if (pidfile == NULL) {
printf("Error: Could not open pids file\n");
return;
}
// number of running processes other than the current process
while (fscanf(pidfile, "%d", &pid) != EOF) {
if (pid != ownPid) {
numPids++;
}
}
rewind(pidfile);
// read other process' PIDs
pids = (int *)malloc(numPids * sizeof(int));
int i = -1;
while (fscanf(pidfile, "%d", &pid) != EOF) {
if (pid != ownPid) {
pids[++i] = pid;
}
}
// send signal sig to processes
printf("\n Sending signal %2d to child processes\n", sig); fflush(stdout);
for (i = 0; i < numPids; i++) {
kill((pid_t)pids[i], sig);
}
fclose(pidfile);
free(pids);
}
void signalinitchild_() {
// initialise child's responses to signals
struct sigaction act_term;
memset (&act_term, '\0', sizeof(act_term));
act_term.sa_sigaction = &child_sigterm_;
act_term.sa_flags = SA_SIGINFO;
sigaction(SIGTERM, &act_term, NULL);
struct sigaction act_segv;
memset (&act_segv, '\0', sizeof(act_segv));
act_segv.sa_sigaction = &child_sigsegv_;
act_segv.sa_flags = SA_SIGINFO;
sigaction(SIGSEGV, &act_segv, &old_act);
}
void child_sigterm_(int signum, siginfo_t *siginfo, void *context) {
// initialise child's response to signal SIGTERM
mrccend_(&ERROR_SIGTERM);
}
void child_sigsegv_(int signum, siginfo_t *siginfo, void *context) {
// initialise child's response to signal SIGSEGV
mrccend_(&ERROR_SIGSEGV);
if(old_act.sa_flags & SA_SIGINFO)
{
(*old_act.sa_sigaction)(signum, siginfo, context);
}
else
{
(*old_act.sa_handler)(signum);
}
}
#ifdef __cplusplus
}
#endif
|