[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 | |
---|
[25411] | 75 | void our_com_err(const char *who, long code, const char *fmt, va_list args) |
---|
| 76 | { |
---|
| 77 | /* A simple com_err that keeps library errors from confusing emacs */ |
---|
| 78 | fprintf(stderr, ";%s: %s\n", who, error_message(code)); |
---|
| 79 | } |
---|
| 80 | |
---|
[5563] | 81 | /* |
---|
| 82 | * This is we can cleanup when we have problems |
---|
| 83 | */ |
---|
[24177] | 84 | void crash_handler(sig) |
---|
[5563] | 85 | int sig; |
---|
| 86 | { |
---|
[5958] | 87 | static int shutting_down_cache = 0; |
---|
[5563] | 88 | int pid; |
---|
| 89 | |
---|
| 90 | pid = fork(); |
---|
| 91 | /* |
---|
| 92 | * If the fork() fails or if this is the child, do a cache shutdown |
---|
| 93 | */ |
---|
| 94 | if (pid <= 0) { |
---|
[23021] | 95 | printf("; Edsc crash (code dump in /tmp) --- signal %d\n", |
---|
[5563] | 96 | sig); |
---|
| 97 | #ifdef EDSC_CACHE |
---|
[5958] | 98 | if (!shutting_down_cache) { |
---|
| 99 | shutting_down_cache++; |
---|
| 100 | cache_shutdown(); |
---|
| 101 | } |
---|
[5563] | 102 | #endif |
---|
| 103 | } |
---|
| 104 | /* |
---|
[23021] | 105 | * If the fork fails or if this is the parent, cd to /tmp |
---|
[5563] | 106 | * and perform a crash dump |
---|
| 107 | */ |
---|
| 108 | if (pid != 0) { |
---|
[23021] | 109 | (void) chdir("/tmp"); |
---|
[5563] | 110 | signal(SIGILL, SIG_DFL); |
---|
| 111 | abort(); |
---|
| 112 | } |
---|
| 113 | exit(1); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | |
---|
[1548] | 118 | char *temp_file; |
---|
| 119 | char *pgm; |
---|
| 120 | char *user_id; |
---|
| 121 | tfile stdout_tf; |
---|
| 122 | main(argc, argv) |
---|
| 123 | int argc; |
---|
| 124 | char **argv; |
---|
| 125 | { |
---|
[5563] | 126 | int code,i; |
---|
| 127 | static char input_buf[INPUT_BUFF_SIZE]; |
---|
[1548] | 128 | char *cp,*op,delim,*args; |
---|
[5563] | 129 | struct rlimit limit; |
---|
[1548] | 130 | |
---|
[23271] | 131 | #if defined(__APPLE__) && defined(__MACH__) |
---|
| 132 | add_error_table(&et_dsc_error_table); |
---|
| 133 | #else |
---|
[22864] | 134 | initialize_dsc_error_table(); |
---|
[23271] | 135 | #endif |
---|
[1548] | 136 | |
---|
[25411] | 137 | /* Use our com_err so the library doesn't spew and confuse emacs */ |
---|
[25412] | 138 | (void) set_com_err_hook(our_com_err); |
---|
[25411] | 139 | |
---|
[1548] | 140 | temp_file = malloc(64); |
---|
| 141 | pgm = malloc(64); |
---|
| 142 | (void) sprintf(temp_file, "/tmp/mtg%d.%d", (int)getuid(), getpid()); |
---|
| 143 | |
---|
[5563] | 144 | code = find_rc_filename(); |
---|
| 145 | if (code && (code != EACCES)) { |
---|
[1548] | 146 | char buf[100]; |
---|
| 147 | sprintf(buf, "%s -q", DSC_SETUP); |
---|
| 148 | system(buf); |
---|
[5563] | 149 | code = find_rc_filename(); |
---|
[1548] | 150 | } |
---|
[5563] | 151 | if (code) { |
---|
| 152 | printf(";%s\n", error_message(code)); |
---|
| 153 | exit(1); |
---|
| 154 | } |
---|
| 155 | #ifdef EDSC_CACHE |
---|
| 156 | cache_init(0); |
---|
| 157 | #endif |
---|
| 158 | /* |
---|
| 159 | * Set up debugging hooks. Also useful becuase it means we clean |
---|
| 160 | * up our cache files in case we die ungracefully. |
---|
| 161 | */ |
---|
| 162 | getrlimit(RLIMIT_CORE, &limit); |
---|
| 163 | limit.rlim_cur = limit.rlim_max; |
---|
| 164 | setrlimit(RLIMIT_CORE, &limit); |
---|
[5958] | 165 | #ifdef SIGILL |
---|
[5563] | 166 | signal(SIGILL, crash_handler); |
---|
[5958] | 167 | #endif |
---|
| 168 | #ifdef SIGIOT |
---|
[5563] | 169 | signal(SIGIOT, crash_handler); |
---|
[5958] | 170 | #endif |
---|
| 171 | #ifdef SIGEMT |
---|
[5563] | 172 | signal(SIGEMT, crash_handler); |
---|
[5958] | 173 | #endif |
---|
| 174 | #ifdef SIGFPE |
---|
[5563] | 175 | signal(SIGFPE, crash_handler); |
---|
[5958] | 176 | #endif |
---|
| 177 | #ifdef SIGBUS |
---|
[5563] | 178 | signal(SIGBUS, crash_handler); |
---|
[5958] | 179 | #endif |
---|
| 180 | #ifdef SIGSEGV |
---|
[5563] | 181 | signal(SIGSEGV, crash_handler); |
---|
[5958] | 182 | #endif |
---|
| 183 | #ifdef SIGPIPE |
---|
| 184 | signal(SIGPIPE, SIG_IGN); |
---|
| 185 | #endif |
---|
[5563] | 186 | /* |
---|
| 187 | * Set up hooks in case we get a graceful die signal |
---|
| 188 | */ |
---|
[12439] | 189 | signal(SIGHUP, sig_do_quit); |
---|
| 190 | signal(SIGINT, sig_do_quit); |
---|
| 191 | signal(SIGTERM, sig_do_quit); |
---|
[5563] | 192 | |
---|
[1548] | 193 | { |
---|
| 194 | register char *user = getpwuid(getuid())->pw_name; |
---|
| 195 | register char *realm = local_realm(); |
---|
| 196 | |
---|
| 197 | user_id = malloc((unsigned)(strlen(user)+strlen(realm)+2)); |
---|
| 198 | strcpy(user_id, user); |
---|
| 199 | strcat(user_id, "@"); |
---|
| 200 | strcat(user_id, realm); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | stdout_tf = unix_tfile(1); /* stdout tfile */ |
---|
| 204 | |
---|
| 205 | while (1) { |
---|
| 206 | set_warning_hook(log_warn); |
---|
[5563] | 207 | #ifdef EDSC_CACHE |
---|
| 208 | if (cache_working) |
---|
| 209 | do_cache_work(); |
---|
| 210 | #endif |
---|
| 211 | |
---|
| 212 | if (fgets(input_buf, INPUT_BUFF_SIZE, stdin) == NULL) |
---|
| 213 | do_quit(0); |
---|
[1548] | 214 | |
---|
| 215 | if (input_buf[0] != '(') { |
---|
| 216 | bad_syntax: |
---|
[5563] | 217 | printf(";Incorrect syntax\n"); |
---|
[1548] | 218 | continue; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | cp = &input_buf[1]; |
---|
| 222 | if (get_word(&cp,&op,") ",&delim) < 0) |
---|
| 223 | goto bad_syntax; |
---|
| 224 | |
---|
| 225 | args = cp; |
---|
| 226 | /* Depending on the operation, call the routine */ |
---|
[5563] | 227 | for (i = 0; i < NUM_EDSC_REQUESTS; i++) { |
---|
| 228 | if (!strcmp (op, edscr[i].name)) { |
---|
| 229 | (*(edscr[i].routine))(args); |
---|
| 230 | break; |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | if (i >= NUM_EDSC_REQUESTS) |
---|
| 234 | printf(";Unimplemented operation\n"); |
---|
| 235 | |
---|
| 236 | fflush(stdout); |
---|
| 237 | } |
---|
[1548] | 238 | } |
---|
| 239 | |
---|
| 240 | log_warn(code, message) |
---|
| 241 | int code; |
---|
| 242 | char *message; |
---|
| 243 | { |
---|
| 244 | printf("-%s %s\n", error_message(code), message); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | bit_bucket(code, message) |
---|
| 248 | int code; |
---|
| 249 | char *message; |
---|
| 250 | { |
---|
| 251 | } |
---|
[5563] | 252 | |
---|
| 253 | do_quit(args) |
---|
| 254 | char *args; |
---|
| 255 | { |
---|
| 256 | #ifdef EDSC_CACHE |
---|
[5958] | 257 | signal(SIGHUP, SIG_IGN); |
---|
| 258 | signal(SIGINT, SIG_IGN); |
---|
| 259 | signal(SIGTERM, SIG_IGN); |
---|
[5563] | 260 | cache_shutdown(); |
---|
| 261 | #endif |
---|
| 262 | exit(0); |
---|
| 263 | } |
---|
[12439] | 264 | |
---|
[24177] | 265 | void sig_do_quit() |
---|
[12439] | 266 | { |
---|
| 267 | do_quit(NULL); |
---|
| 268 | } |
---|