]> andersk Git - moira.git/blob - server/startmoira.c
moved directory definitions
[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 #include <sms_app.h>
28
29 #define PROG    "smsd"
30
31 int rdpipe[2];
32 char *sigdescr[] = {
33         0,
34         "hangup",
35         "interrupt",    
36         "quit",
37         "illegal instruction",
38         "trace/BPT trap",
39         "IOT trap",
40         "EMT trap",
41         "floating exception",
42         "kill",
43         "bus error",
44         "segmentation violation",
45         "bad system call",
46         "broken pipe",
47         "alarm clock",
48         "termination",
49         "urgent I/O condition",
50         "stopped",
51         "stopped",
52         "continued",
53         "child exited",
54         "stopped (tty input)",
55         "stopped (tty output)",
56         "I/O possible",
57         "cputime limit exceeded",
58         "filesize limit exceeded",
59         "virtual timer expired",
60         "profiling timer expired",
61         "window size changed",
62         "signal 29",
63         "user defined signal 1",
64         "user defined signal 2",
65         "signal 32"
66 };
67
68 cleanup()
69 {
70         union wait stat;
71         char buf[BUFSIZ];
72         extern int errno;
73         int serrno = errno;
74
75         buf[0]='\0';
76         
77         while (wait3(&stat, WNOHANG, 0) > 0) {
78                 if (WIFEXITED(stat)) {
79                         if (stat.w_retcode)
80                                 sprintf(buf,
81                                         "exited with code %d\n",
82                                         stat.w_retcode);
83                 }
84                 if (WIFSIGNALED(stat)) {
85                         sprintf(buf, "exited on %s signal%s\n",
86                                 sigdescr[stat.w_termsig],
87                                 (stat.w_coredump?"; Core dumped":0));
88                 }
89                 write(rdpipe[1], buf, strlen(buf));
90                 close(rdpipe[1]);
91         }
92         errno = serrno;
93 }
94
95 main(argc, argv)
96 {
97         char buf[BUFSIZ];
98         FILE *log, *prog;
99         int logf, inf, i, done, pid, tty;
100         
101         extern int errno;
102         extern char *sys_errlist[];
103         
104         int nfds = getdtablesize();
105         
106         setreuid(0);
107         signal(SIGCHLD, cleanup);
108         
109         sprintf(buf, "%s/sms.log", SMS_DIR);
110         logf = open(buf, O_CREAT|O_WRONLY|O_APPEND, 0640);
111         if (logf<0) {
112                 perror(buf);
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         sprintf(buf, "%s/%s", BIN_DIR, PROG);
136         
137         if ((pid = fork()) == 0) {
138                 
139                 dup2(inf, 0);
140                 dup2(rdpipe[1], 1);
141                 dup2(1,2);
142                 for (i = 3; i <nfds; i++) close(i);
143                 execl(buf, PROG, 0);
144                 perror("cannot run smsd");
145                 exit(1);
146         }
147         if (pid<0) {
148                 perror("sms_starter");
149                 exit(1);
150         }
151
152         log = fdopen(logf, "w");
153         prog = fdopen(rdpipe[0], "r");
154         
155         
156         do {
157                 char *time_s;
158                 extern char *ctime();
159                 long foo;
160                 
161                 done = 0;
162                 errno = 0;
163                 if (fgets(buf, BUFSIZ, prog) == NULL) {
164                         if (errno) {
165                                 strcpy(buf, "Unable to read from program: ");
166                                 strcat(buf, sys_errlist[errno]);
167                                 strcat(buf, "\n");
168                         } else break;
169                 }
170                 time(&foo);
171                 time_s = ctime(&foo)+4;
172                 time_s[strlen(time_s)-6]='\0';
173                 fprintf(log, "%s <%d> %s", time_s, pid, buf);
174                 fflush(log);
175         } while (!done);
176         exit(0);
177 }
178
179
180
181           
This page took 0.054975 seconds and 5 git commands to generate.