1 | /* |
---|
2 | * |
---|
3 | * Copyright (C) 1989 by the Massachusetts Institute of Technology |
---|
4 | * Developed by the MIT Student Information Processing Board (SIPB). |
---|
5 | * For copying information, see the file mit-copyright.h in this release. |
---|
6 | * |
---|
7 | */ |
---|
8 | /* |
---|
9 | * |
---|
10 | * edsc () -- A subprocess to make implement emacs discuss mode easier. |
---|
11 | * |
---|
12 | */ |
---|
13 | |
---|
14 | |
---|
15 | #ifndef lint |
---|
16 | static char *rcsid_discuss_c = "$Id: edsc.c,v 1.15 2006-03-10 07:11:37 ghudson Exp $"; |
---|
17 | #endif /* lint */ |
---|
18 | |
---|
19 | #include <stdio.h> |
---|
20 | #include <stdlib.h> |
---|
21 | #include <sys/file.h> |
---|
22 | #include <sys/time.h> |
---|
23 | #include <sys/resource.h> |
---|
24 | #include <signal.h> |
---|
25 | #include <string.h> |
---|
26 | #include <sys/wait.h> |
---|
27 | #include <pwd.h> |
---|
28 | #include <ctype.h> |
---|
29 | #include <errno.h> |
---|
30 | #include "config.h" |
---|
31 | #include "edsc.h" |
---|
32 | #define INPUT_BUFF_SIZE 10240 |
---|
33 | |
---|
34 | char *local_realm(); |
---|
35 | int log_warn(); |
---|
36 | tfile unix_tfile(); |
---|
37 | RETSIGTYPE sig_do_quit(); |
---|
38 | |
---|
39 | static struct edsc_req { |
---|
40 | char *name; /* Name of request */ |
---|
41 | int (*routine)(); /* Routine to call */ |
---|
42 | } edscr[] = { |
---|
43 | {"quit", do_quit}, |
---|
44 | {"gmi", do_gmi}, |
---|
45 | {"gti", do_gti}, |
---|
46 | {"gml", do_gml}, |
---|
47 | {"gt", do_gt}, |
---|
48 | {"gtf", do_gtf}, |
---|
49 | {"grt", do_grt}, |
---|
50 | {"grtn", do_grtn}, |
---|
51 | {"ss", do_ss}, |
---|
52 | {"at", do_at}, |
---|
53 | {"nut", do_nut}, |
---|
54 | {"sfl", do_sfl}, |
---|
55 | {"am", do_am}, |
---|
56 | {"dm", do_dm}, |
---|
57 | {"pacl", do_pacl}, |
---|
58 | {"dacl", do_dacl}, |
---|
59 | {"sacl", do_sacl}, |
---|
60 | {"ls", do_ls}, |
---|
61 | {"dt", do_dt}, |
---|
62 | {"rt", do_rt}, |
---|
63 | #ifdef EDSC_CACHE |
---|
64 | {"scd", do_scd}, |
---|
65 | {"gtfc", do_gtfc}, |
---|
66 | {"it", do_it}, |
---|
67 | {"itn", do_itn}, |
---|
68 | {"im", do_im}, |
---|
69 | #endif |
---|
70 | {"gpv", do_gpv} |
---|
71 | }; |
---|
72 | |
---|
73 | #define NUM_EDSC_REQUESTS (sizeof (edscr) / sizeof (struct edsc_req)) |
---|
74 | |
---|
75 | /* |
---|
76 | * This is we can cleanup when we have problems |
---|
77 | */ |
---|
78 | RETSIGTYPE crash_handler(sig) |
---|
79 | int sig; |
---|
80 | { |
---|
81 | static int shutting_down_cache = 0; |
---|
82 | int pid; |
---|
83 | |
---|
84 | pid = fork(); |
---|
85 | /* |
---|
86 | * If the fork() fails or if this is the child, do a cache shutdown |
---|
87 | */ |
---|
88 | if (pid <= 0) { |
---|
89 | printf("; Edsc crash (code dump in /tmp) --- signal %d\n", |
---|
90 | sig); |
---|
91 | #ifdef EDSC_CACHE |
---|
92 | if (!shutting_down_cache) { |
---|
93 | shutting_down_cache++; |
---|
94 | cache_shutdown(); |
---|
95 | } |
---|
96 | #endif |
---|
97 | } |
---|
98 | /* |
---|
99 | * If the fork fails or if this is the parent, cd to /tmp |
---|
100 | * and perform a crash dump |
---|
101 | */ |
---|
102 | if (pid != 0) { |
---|
103 | (void) chdir("/tmp"); |
---|
104 | signal(SIGILL, SIG_DFL); |
---|
105 | abort(); |
---|
106 | } |
---|
107 | exit(1); |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | char *temp_file; |
---|
113 | char *pgm; |
---|
114 | char *user_id; |
---|
115 | tfile stdout_tf; |
---|
116 | main(argc, argv) |
---|
117 | int argc; |
---|
118 | char **argv; |
---|
119 | { |
---|
120 | int code,i; |
---|
121 | static char input_buf[INPUT_BUFF_SIZE]; |
---|
122 | char *cp,*op,delim,*args; |
---|
123 | struct rlimit limit; |
---|
124 | |
---|
125 | #if defined(__APPLE__) && defined(__MACH__) |
---|
126 | add_error_table(&et_dsc_error_table); |
---|
127 | #else |
---|
128 | initialize_dsc_error_table(); |
---|
129 | #endif |
---|
130 | |
---|
131 | temp_file = malloc(64); |
---|
132 | pgm = malloc(64); |
---|
133 | (void) sprintf(temp_file, "/tmp/mtg%d.%d", (int)getuid(), getpid()); |
---|
134 | |
---|
135 | code = find_rc_filename(); |
---|
136 | if (code && (code != EACCES)) { |
---|
137 | char buf[100]; |
---|
138 | sprintf(buf, "%s -q", DSC_SETUP); |
---|
139 | system(buf); |
---|
140 | code = find_rc_filename(); |
---|
141 | } |
---|
142 | if (code) { |
---|
143 | printf(";%s\n", error_message(code)); |
---|
144 | exit(1); |
---|
145 | } |
---|
146 | #ifdef EDSC_CACHE |
---|
147 | cache_init(0); |
---|
148 | #endif |
---|
149 | /* |
---|
150 | * Set up debugging hooks. Also useful becuase it means we clean |
---|
151 | * up our cache files in case we die ungracefully. |
---|
152 | */ |
---|
153 | getrlimit(RLIMIT_CORE, &limit); |
---|
154 | limit.rlim_cur = limit.rlim_max; |
---|
155 | setrlimit(RLIMIT_CORE, &limit); |
---|
156 | #ifdef SIGILL |
---|
157 | signal(SIGILL, crash_handler); |
---|
158 | #endif |
---|
159 | #ifdef SIGIOT |
---|
160 | signal(SIGIOT, crash_handler); |
---|
161 | #endif |
---|
162 | #ifdef SIGEMT |
---|
163 | signal(SIGEMT, crash_handler); |
---|
164 | #endif |
---|
165 | #ifdef SIGFPE |
---|
166 | signal(SIGFPE, crash_handler); |
---|
167 | #endif |
---|
168 | #ifdef SIGBUS |
---|
169 | signal(SIGBUS, crash_handler); |
---|
170 | #endif |
---|
171 | #ifdef SIGSEGV |
---|
172 | signal(SIGSEGV, crash_handler); |
---|
173 | #endif |
---|
174 | #ifdef SIGPIPE |
---|
175 | signal(SIGPIPE, SIG_IGN); |
---|
176 | #endif |
---|
177 | /* |
---|
178 | * Set up hooks in case we get a graceful die signal |
---|
179 | */ |
---|
180 | signal(SIGHUP, sig_do_quit); |
---|
181 | signal(SIGINT, sig_do_quit); |
---|
182 | signal(SIGTERM, sig_do_quit); |
---|
183 | |
---|
184 | { |
---|
185 | register char *user = getpwuid(getuid())->pw_name; |
---|
186 | register char *realm = local_realm(); |
---|
187 | |
---|
188 | user_id = malloc((unsigned)(strlen(user)+strlen(realm)+2)); |
---|
189 | strcpy(user_id, user); |
---|
190 | strcat(user_id, "@"); |
---|
191 | strcat(user_id, realm); |
---|
192 | } |
---|
193 | |
---|
194 | stdout_tf = unix_tfile(1); /* stdout tfile */ |
---|
195 | |
---|
196 | while (1) { |
---|
197 | set_warning_hook(log_warn); |
---|
198 | #ifdef EDSC_CACHE |
---|
199 | if (cache_working) |
---|
200 | do_cache_work(); |
---|
201 | #endif |
---|
202 | |
---|
203 | if (fgets(input_buf, INPUT_BUFF_SIZE, stdin) == NULL) |
---|
204 | do_quit(0); |
---|
205 | |
---|
206 | if (input_buf[0] != '(') { |
---|
207 | bad_syntax: |
---|
208 | printf(";Incorrect syntax\n"); |
---|
209 | continue; |
---|
210 | } |
---|
211 | |
---|
212 | cp = &input_buf[1]; |
---|
213 | if (get_word(&cp,&op,") ",&delim) < 0) |
---|
214 | goto bad_syntax; |
---|
215 | |
---|
216 | args = cp; |
---|
217 | /* Depending on the operation, call the routine */ |
---|
218 | for (i = 0; i < NUM_EDSC_REQUESTS; i++) { |
---|
219 | if (!strcmp (op, edscr[i].name)) { |
---|
220 | (*(edscr[i].routine))(args); |
---|
221 | break; |
---|
222 | } |
---|
223 | } |
---|
224 | if (i >= NUM_EDSC_REQUESTS) |
---|
225 | printf(";Unimplemented operation\n"); |
---|
226 | |
---|
227 | fflush(stdout); |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | log_warn(code, message) |
---|
232 | int code; |
---|
233 | char *message; |
---|
234 | { |
---|
235 | printf("-%s %s\n", error_message(code), message); |
---|
236 | } |
---|
237 | |
---|
238 | bit_bucket(code, message) |
---|
239 | int code; |
---|
240 | char *message; |
---|
241 | { |
---|
242 | } |
---|
243 | |
---|
244 | do_quit(args) |
---|
245 | char *args; |
---|
246 | { |
---|
247 | #ifdef EDSC_CACHE |
---|
248 | signal(SIGHUP, SIG_IGN); |
---|
249 | signal(SIGINT, SIG_IGN); |
---|
250 | signal(SIGTERM, SIG_IGN); |
---|
251 | cache_shutdown(); |
---|
252 | #endif |
---|
253 | exit(0); |
---|
254 | } |
---|
255 | |
---|
256 | RETSIGTYPE sig_do_quit() |
---|
257 | { |
---|
258 | do_quit(NULL); |
---|
259 | } |
---|