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