]> andersk Git - moira.git/blob - server/startmoira.c
added copyright message
[moira.git] / server / startmoira.c
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 by the Massachusetts Institute of Technology
7  *      For copying and distribution information, please see the file
8  *      <mit-copyright.h>.
9  *
10  *      This program starts the sms server in a "clean" environment.
11  *      and then waits for it to exit.
12  * 
13  */
14
15 #ifndef lint
16 static char *rcsid_sms_starter_c = "$Header$";
17 #endif lint
18
19 #include <mit-copyright.h>
20 #include <stdio.h>
21 #include <strings.h>
22 #include <sys/types.h>
23 #include <sys/file.h>
24 #include <sys/wait.h>
25 #include <sys/signal.h>
26 #include <sys/ioctl.h>
27
28 #define SMS_LOG_FILE "/u1/sms/sms.log"
29
30 #define SMS_PROG "/u1/sms/bin/smsd"
31
32 int rdpipe[2];
33 char *sigdescr[] = {
34         0,
35         "hangup",
36         "interrupt",    
37         "quit",
38         "illegal instruction",
39         "trace/BPT trap",
40         "IOT trap",
41         "EMT trap",
42         "floating exception",
43         "kill",
44         "bus error",
45         "segmentation violation",
46         "bad system call",
47         "broken pipe",
48         "alarm clock",
49         "termination",
50         "urgent I/O condition",
51         "stopped",
52         "stopped",
53         "continued",
54         "child exited",
55         "stopped (tty input)",
56         "stopped (tty output)",
57         "I/O possible",
58         "cputime limit exceeded",
59         "filesize limit exceeded",
60         "virtual timer expired",
61         "profiling timer expired",
62         "window size changed",
63         "signal 29",
64         "user defined signal 1",
65         "user defined signal 2",
66         "signal 32"
67 };
68
69 cleanup()
70 {
71         union wait stat;
72         char buf[BUFSIZ];
73         extern int errno;
74         int serrno = errno;
75
76         buf[0]='\0';
77         
78         while (wait3(&stat, WNOHANG, 0) > 0) {
79                 if (WIFEXITED(stat)) {
80                         if (stat.w_retcode)
81                                 sprintf(buf,
82                                         "exited with code %d\n",
83                                         stat.w_retcode);
84                 }
85                 if (WIFSIGNALED(stat)) {
86                         sprintf(buf, "exited on %s signal%s\n",
87                                 sigdescr[stat.w_termsig],
88                                 (stat.w_coredump?"; Core dumped":0));
89                 }
90                 write(rdpipe[1], buf, strlen(buf));
91                 close(rdpipe[1]);
92         }
93         errno = serrno;
94 }
95
96 main(argc, argv)
97 {
98         char buf[BUFSIZ];
99         FILE *log, *prog;
100         int logf, inf, i, done, pid, tty;
101         
102         extern int errno;
103         extern char *sys_errlist[];
104         
105         int nfds = getdtablesize();
106         
107         setreuid(0);
108         signal(SIGCHLD, cleanup);
109         
110         logf = open(SMS_LOG_FILE, O_CREAT|O_WRONLY|O_APPEND, 0640);
111         if (logf<0) {
112                 perror(SMS_LOG_FILE);
113                 exit(1);
114         }
115         inf = open("/dev/null", O_RDONLY , 0);
116         if (inf < 0) {
117                 perror("/dev/null");
118                 exit(1);
119         }
120         pipe(rdpipe);
121         if (fork()) {
122                 exit();
123         }
124         chdir("/");     
125         close(0);
126         close(1);
127         close(2);
128         dup2(inf, 0);
129         dup2(inf, 1);
130         dup2(inf, 2);
131         
132         tty = open("/dev/tty");
133         ioctl(tty, TIOCNOTTY, 0);
134         close(tty);
135         
136         if ((pid = fork()) == 0) {
137                 
138                 dup2(inf, 0);
139                 dup2(rdpipe[1], 1);
140                 dup2(1,2);
141                 for (i = 3; i <nfds; i++) close(i);
142                 execl(SMS_PROG, "smsd", 0);
143                 perror("cannot run smsd");
144                 exit(1);
145         }
146         if (pid<0) {
147                 perror("sms_starter");
148                 exit(1);
149         }
150
151         log = fdopen(logf, "w");
152         prog = fdopen(rdpipe[0], "r");
153         
154         
155         do {
156                 char *time_s;
157                 extern char *ctime();
158                 long foo;
159                 
160                 done = 0;
161                 errno = 0;
162                 if (fgets(buf, BUFSIZ, prog) == NULL) {
163                         if (errno) {
164                                 strcpy(buf, "Unable to read from program: ");
165                                 strcat(buf, sys_errlist[errno]);
166                                 strcat(buf, "\n");
167                         } else break;
168                 }
169                 time(&foo);
170                 time_s = ctime(&foo)+4;
171                 time_s[strlen(time_s)-6]='\0';
172                 fprintf(log, "%s <%d> %s", time_s, pid, buf);
173                 fflush(log);
174         } while (!done);
175         exit(0);
176 }
177
178
179
180           
This page took 0.059584 seconds and 5 git commands to generate.