Commit | Line | Data |
---|---|---|
ff31d49a C |
1 | #include <signal.h> |
2 | #include <stdio.h> | |
3 | #include <setjmp.h> | |
4 | #include <sys/ioctl.h> | |
5 | #include <sys/types.h> | |
6 | #include <sys/time.h> | |
7 | ||
8 | #define CTIME 1 | |
9 | #define ASCTIME 2 | |
10 | #define TZSET 3 | |
11 | #define LOCALTIME 4 | |
12 | #define GMTIME 5 | |
13 | #define OFFTIME 6 | |
14 | ||
15 | extern struct tm *offtime(); | |
16 | ||
17 | jmp_buf env; | |
18 | char *cp, junk[52]; | |
19 | long l, off; | |
20 | int timeout(), checkppid(); | |
21 | struct tm tmtmp, *tp; | |
22 | ||
23 | main() | |
24 | { | |
25 | register int i; | |
26 | struct itimerval it; | |
27 | u_char c; | |
28 | ||
29 | signal(SIGPIPE, SIG_DFL); | |
30 | for (i = getdtablesize(); --i > 2; ) | |
31 | close(i); | |
32 | /* | |
33 | * Need a timer running while we disassociate from the control terminal | |
34 | * in case of a modem line which has lost carrier. | |
35 | */ | |
36 | timerclear(&it.it_interval); | |
37 | it.it_value.tv_sec = 5; | |
38 | it.it_value.tv_usec = 0; | |
39 | signal(SIGALRM, timeout); | |
40 | setitimer(ITIMER_REAL, &it, (struct itimerval *) NULL); | |
41 | if (setjmp(env) == 0) | |
42 | { | |
43 | i = open("/dev/tty", 0); | |
44 | if (i >= 0) | |
45 | { | |
46 | ioctl(i, TIOCNOTTY, NULL); | |
47 | close(i); | |
48 | } | |
49 | } | |
50 | /* | |
51 | * Now start a timer with one minute refresh. In the signal service | |
52 | * routine, check the parent process id to see if this process has | |
53 | * been orphaned and if so exit. This is primarily aimed at removing | |
54 | * the 'ctimed' process left behind by 'sendmail's multi-fork startup | |
55 | * but may prove useful in preventing accumulation of 'ctimed' processes | |
56 | * in other circumstances as well. Normally this process is short | |
57 | * lived. | |
58 | */ | |
59 | it.it_interval.tv_sec = 60; | |
60 | it.it_interval.tv_usec = 0; | |
61 | it.it_value.tv_sec = 60; | |
62 | it.it_value.tv_usec = 0; | |
63 | signal(SIGALRM, checkppid); | |
64 | setitimer(ITIMER_REAL, &it, (struct itimerval *) NULL); | |
65 | ||
66 | while (read(fileno(stdin), &c, 1) == 1) | |
67 | { | |
68 | switch (c) | |
69 | { | |
70 | case CTIME: | |
71 | l = 0L; | |
72 | getb(fileno(stdin), &l, sizeof l); | |
73 | cp = ctime(&l); | |
74 | write(fileno(stdout), cp, 26); | |
75 | break; | |
76 | case ASCTIME: | |
77 | getb(fileno(stdin), &tmtmp, sizeof tmtmp); | |
78 | cp = asctime(&tmtmp); | |
79 | write(fileno(stdout), cp, 26); | |
80 | break; | |
81 | case TZSET: | |
82 | (void) tzset(); | |
83 | break; | |
84 | case LOCALTIME: | |
85 | l = 0L; | |
86 | getb(fileno(stdin), &l, sizeof l); | |
87 | tp = localtime(&l); | |
88 | write(fileno(stdout), tp, sizeof (*tp)); | |
89 | strcpy(junk, tp->tm_zone); | |
90 | junk[24] = '\0'; | |
91 | write(fileno(stdout), junk, 24); | |
92 | break; | |
93 | case GMTIME: | |
94 | l = 0L; | |
95 | getb(fileno(stdin), &l, sizeof l); | |
96 | tp = gmtime(&l); | |
97 | write(fileno(stdout), tp, sizeof (*tp)); | |
98 | strcpy(junk, tp->tm_zone); | |
99 | junk[24] = '\0'; | |
100 | write(fileno(stdout), junk, 24); | |
101 | break; | |
102 | case OFFTIME: | |
103 | getb(fileno(stdin), &l, sizeof l); | |
104 | getb(fileno(stdin), &off, sizeof off); | |
105 | tp = offtime(&l, off); | |
106 | write(fileno(stdout), tp, sizeof (*tp)); | |
107 | break; | |
108 | default: | |
109 | abort("switch"); | |
110 | } | |
111 | } | |
112 | } | |
113 | ||
114 | getb(f, p, n) | |
115 | int f; | |
116 | register char *p; | |
117 | register int n; | |
118 | { | |
119 | register int i; | |
120 | ||
121 | while (n) | |
122 | { | |
123 | i = read(f, p, n); | |
124 | if (i <= 0) | |
125 | return; | |
126 | p += i; | |
127 | n -= i; | |
128 | } | |
129 | } | |
130 | ||
131 | timeout() | |
132 | { | |
133 | ||
134 | longjmp(env, 1); | |
135 | } | |
136 | ||
137 | checkppid() | |
138 | { | |
139 | ||
140 | if (getppid() == 1) | |
141 | exit(0); | |
142 | } |