[1548] | 1 | /* |
---|
| 2 | * |
---|
[1929] | 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 | * |
---|
[1548] | 10 | * edsc () -- A subprocess to make implement emacs discuss mode easier. |
---|
| 11 | * |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | #ifndef lint |
---|
[22404] | 16 | static char *rcsid_discuss_c = "$Id: edsc.c,v 1.15 2006-03-10 07:11:37 ghudson Exp $"; |
---|
[12459] | 17 | #endif /* lint */ |
---|
[1548] | 18 | |
---|
| 19 | #include <stdio.h> |
---|
[22404] | 20 | #include <stdlib.h> |
---|
[1548] | 21 | #include <sys/file.h> |
---|
[5563] | 22 | #include <sys/time.h> |
---|
| 23 | #include <sys/resource.h> |
---|
[1548] | 24 | #include <signal.h> |
---|
[8855] | 25 | #include <string.h> |
---|
[1548] | 26 | #include <sys/wait.h> |
---|
| 27 | #include <pwd.h> |
---|
| 28 | #include <ctype.h> |
---|
[5563] | 29 | #include <errno.h> |
---|
[1548] | 30 | #include "config.h" |
---|
[5563] | 31 | #include "edsc.h" |
---|
| 32 | #define INPUT_BUFF_SIZE 10240 |
---|
[1548] | 33 | |
---|
| 34 | char *local_realm(); |
---|
| 35 | int log_warn(); |
---|
| 36 | tfile unix_tfile(); |
---|
[24177] | 37 | void sig_do_quit(); |
---|
[1548] | 38 | |
---|
[5563] | 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 | */ |
---|
[24177] | 78 | void crash_handler(sig) |
---|
[5563] | 79 | int sig; |
---|
| 80 | { |
---|
[5958] | 81 | static int shutting_down_cache = 0; |
---|
[5563] | 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) { |
---|
[23021] | 89 | printf("; Edsc crash (code dump in /tmp) --- signal %d\n", |
---|
[5563] | 90 | sig); |
---|
| 91 | #ifdef EDSC_CACHE |
---|
[5958] | 92 | if (!shutting_down_cache) { |
---|
| 93 | shutting_down_cache++; |
---|
| 94 | cache_shutdown(); |
---|
| 95 | } |
---|
[5563] | 96 | #endif |
---|
| 97 | } |
---|
| 98 | /* |
---|
[23021] | 99 | * If the fork fails or if this is the parent, cd to /tmp |
---|
[5563] | 100 | * and perform a crash dump |
---|
| 101 | */ |
---|
| 102 | if (pid != 0) { |
---|
[23021] | 103 | (void) chdir("/tmp"); |
---|
[5563] | 104 | signal(SIGILL, SIG_DFL); |
---|
| 105 | abort(); |
---|
| 106 | } |
---|
| 107 | exit(1); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | |
---|
[1548] | 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 | { |
---|
[5563] | 120 | int code,i; |
---|
| 121 | static char input_buf[INPUT_BUFF_SIZE]; |
---|
[1548] | 122 | char *cp,*op,delim,*args; |
---|
[5563] | 123 | struct rlimit limit; |
---|
[1548] | 124 | |
---|
[23271] | 125 | #if defined(__APPLE__) && defined(__MACH__) |
---|
| 126 | add_error_table(&et_dsc_error_table); |
---|
| 127 | #else |
---|
[22864] | 128 | initialize_dsc_error_table(); |
---|
[23271] | 129 | #endif |
---|
[1548] | 130 | |
---|
| 131 | temp_file = malloc(64); |
---|
| 132 | pgm = malloc(64); |
---|
| 133 | (void) sprintf(temp_file, "/tmp/mtg%d.%d", (int)getuid(), getpid()); |
---|
| 134 | |
---|
[5563] | 135 | code = find_rc_filename(); |
---|
| 136 | if (code && (code != EACCES)) { |
---|
[1548] | 137 | char buf[100]; |
---|
| 138 | sprintf(buf, "%s -q", DSC_SETUP); |
---|
| 139 | system(buf); |
---|
[5563] | 140 | code = find_rc_filename(); |
---|
[1548] | 141 | } |
---|
[5563] | 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); |
---|
[5958] | 156 | #ifdef SIGILL |
---|
[5563] | 157 | signal(SIGILL, crash_handler); |
---|
[5958] | 158 | #endif |
---|
| 159 | #ifdef SIGIOT |
---|
[5563] | 160 | signal(SIGIOT, crash_handler); |
---|
[5958] | 161 | #endif |
---|
| 162 | #ifdef SIGEMT |
---|
[5563] | 163 | signal(SIGEMT, crash_handler); |
---|
[5958] | 164 | #endif |
---|
| 165 | #ifdef SIGFPE |
---|
[5563] | 166 | signal(SIGFPE, crash_handler); |
---|
[5958] | 167 | #endif |
---|
| 168 | #ifdef SIGBUS |
---|
[5563] | 169 | signal(SIGBUS, crash_handler); |
---|
[5958] | 170 | #endif |
---|
| 171 | #ifdef SIGSEGV |
---|
[5563] | 172 | signal(SIGSEGV, crash_handler); |
---|
[5958] | 173 | #endif |
---|
| 174 | #ifdef SIGPIPE |
---|
| 175 | signal(SIGPIPE, SIG_IGN); |
---|
| 176 | #endif |
---|
[5563] | 177 | /* |
---|
| 178 | * Set up hooks in case we get a graceful die signal |
---|
| 179 | */ |
---|
[12439] | 180 | signal(SIGHUP, sig_do_quit); |
---|
| 181 | signal(SIGINT, sig_do_quit); |
---|
| 182 | signal(SIGTERM, sig_do_quit); |
---|
[5563] | 183 | |
---|
[1548] | 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); |
---|
[5563] | 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); |
---|
[1548] | 205 | |
---|
| 206 | if (input_buf[0] != '(') { |
---|
| 207 | bad_syntax: |
---|
[5563] | 208 | printf(";Incorrect syntax\n"); |
---|
[1548] | 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 */ |
---|
[5563] | 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 | } |
---|
[1548] | 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 | } |
---|
[5563] | 243 | |
---|
| 244 | do_quit(args) |
---|
| 245 | char *args; |
---|
| 246 | { |
---|
| 247 | #ifdef EDSC_CACHE |
---|
[5958] | 248 | signal(SIGHUP, SIG_IGN); |
---|
| 249 | signal(SIGINT, SIG_IGN); |
---|
| 250 | signal(SIGTERM, SIG_IGN); |
---|
[5563] | 251 | cache_shutdown(); |
---|
| 252 | #endif |
---|
| 253 | exit(0); |
---|
| 254 | } |
---|
[12439] | 255 | |
---|
[24177] | 256 | void sig_do_quit() |
---|
[12439] | 257 | { |
---|
| 258 | do_quit(NULL); |
---|
| 259 | } |
---|