]> andersk Git - moira.git/blob - update/update_server.c
7be11608f46f9e96024abd59e24e17b11242c6f2
[moira.git] / update / update_server.c
1 /*
2  *      $Source$
3  *      $Header$
4  */
5 /*  (c) Copyright 1988 by the Massachusetts Institute of Technology. */
6 /*  For copying and distribution information, please see the file */
7 /*  <mit-copyright.h>. */
8
9 #ifndef lint
10 static char *rcsid_dispatch_c = "$Header$";
11 #endif  lint
12
13 #include <mit-copyright.h>
14 #include <stdio.h>
15 #include <gdb.h>
16 #include <errno.h>
17 #include <strings.h>
18 #include <moira.h>
19 #include <sys/file.h>
20 #include <sys/ioctl.h>
21 #include "update.h"
22
23 extern int auth_001(), inst_001();
24 extern int xfer_002(), exec_002();
25
26 extern int sync_proc(), quit();
27
28 extern void gdb_debug();
29 extern int exit(), abort(), errno;
30 extern STRING instructions;
31
32 CONNECTION conn;
33 int code;
34 char *whoami;
35
36 int have_authorization = 0;
37 int have_file = 0;
38 int have_instructions = 0;
39 int done = 0;
40
41
42 #define send_int(n) \
43      (_send_int=(n),send_object(conn,(char *)&_send_int,INTEGER_T))
44 int _send_int;
45
46 struct _dt {
47      char *str;
48      int (*proc)();
49 } dispatch_table[] = {
50      { "INST_001", inst_001 },
51      { "AUTH_001", auth_001 },
52      { "XFER_002", xfer_002 },
53      { "EXEC_002", exec_002 },
54      { "quit", quit },
55      { (char *)NULL, abort }
56 };
57
58 /*
59  * general scratch space -- useful for building
60  * error messages et al...
61  */
62 char buf[BUFSIZ];
63 err(code, fmt)
64      int code;
65      char *fmt;
66 {
67      sprintf(buf, fmt, error_message(code));
68      mr_log_error(buf);
69 }
70
71 main(argc, argv)
72      int argc;
73      char **argv;
74 {
75      STRING str;
76      struct _dt *d;
77      int n;
78
79 #ifdef DEBUG
80      gdb_debug(GDB_NOFORK);
81 #endif /* DEBUG */
82
83      whoami = rindex(argv[0], '/');
84      if (whoami)
85          whoami++;
86      else
87          whoami = argv[0];
88
89      /* interpret arguments here */
90      if (argc != 1) {
91           fprintf(stderr, "Usage:  %s\n", whoami);
92           exit(1);
93      }
94      /* well, sort of... */
95
96 #ifndef DEBUG
97      if (fork())
98        exit(0);
99      n = open("/dev/tty", O_RDWR|FNDELAY);
100      if (n > 0) {
101          (void) ioctl(n, TIOCNOTTY, (char *)NULL);
102          (void) close(n);
103      }
104 #endif
105
106      umask(0022);
107      initialize_sms_error_table();
108      initialize_krb_error_table();
109      mr_update_initialize();
110
111      /* wait for connection */
112      gdb_init();
113      conn = create_forking_server(SERVICE_NAME, 0);
114      if (!conn) {
115           err(errno, "%s: can't get connection");
116           exit(1);
117      }
118      if (connection_status(conn) == CON_STOPPED) {
119          com_err(whoami, connection_errno(conn), ": can't get connection");
120          exit(1);
121      }
122
123      mr_log_info("got connection");
124      /* got a connection; loop forever */
125      while (1) {
126           register char *cp;
127           code = receive_object(conn, (char *)&str, STRING_T);
128           if (code) {
129                err(connection_errno(conn), "%s: receiving command");
130                sever_connection(conn);
131                exit(1);
132           }
133           cp = index(STRING_DATA(str), ' ');
134           if (cp)
135                *cp = '\0';
136           for (d = dispatch_table; d->str; d++) {
137               if (!strcmp(d->str, STRING_DATA(str))) {
138                   if (cp)
139                       *cp = ' ';
140 #ifdef  DEBUG
141                   printf("got request: %s\n", STRING_DATA(str));
142 #endif  /* DEBUG */
143                   (void)(d->proc)(STRING_DATA(str));
144                   goto ok;
145               }
146           }
147           sprintf(buf, "unknown request received: %s\n", STRING_DATA(str));
148           mr_log_error(buf);
149           code = send_int(MR_UNKNOWN_PROC);
150           if (code) {
151               err(connection_errno(conn), "%s: sending UNKNOWN_PROC");
152           }
153      ok:
154           string_free(&str);
155      }
156 }
157
158 int
159 send_ok()
160 {
161      static int zero = 0;
162      return((code = send_object(conn, (char *)&zero, INTEGER_T)));
163 }
164
165
166 initialize()
167 {
168      /* keep have_authorization around */
169      have_file = 0;
170      have_instructions = 0;
171      done = 0;
172      if (STRING_DATA(instructions) != (char *)NULL)
173           string_free(&instructions);
174 }
175
176
177 /*
178  * quit request:
179  *
180  * syntax:
181  * >>> quit
182  * <<< (int)0
183  * any arguments are ignored
184  *
185  * function:
186  *      closes connection from MR
187  */
188 int
189 quit(str)
190      char *str;
191 {
192 #ifdef lint
193      str = (char *)NULL;
194 #endif /* lint */
195      (void) send_ok();
196      sever_connection(conn);
197      mr_log_info("Closing connection.");
198      exit(0);
199 }
200
201
202 /*
203  * lose(msg)
204  *
205  * put <msg> to log as error, break connection, and exit
206  */
207
208 lose(msg)
209     char *msg;
210 {
211     sprintf(buf, "%s: %s", error_message(code), msg);
212     mr_log_error(buf);
213     if (conn)
214         sever_connection(conn);
215     exit(1);
216 }
217
218 /*
219  * report_error(msg)
220  *
221  * send back (external) <code>; if error, punt big with <lose(msg)>
222  */
223
224 report_error(msg)
225     char *msg;
226 {
227     code = send_object(conn, (char *)&code, INTEGER_T);
228     if (code) {
229         code = connection_errno(conn);
230         lose(msg);
231     }
232 }
233
234 /*
235  * reject_call(c)
236  *
237  * set (external) <code> to <c> and call <report_error>
238  */
239
240 reject_call(c)
241     int c;
242 {
243     code = c;
244     report_error("call rejected");
245 }
This page took 0.042124 seconds and 3 git commands to generate.