6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
13 static char *rcsid_sms_scall_c = "$Header$";
16 #include <mit-copyright.h>
17 #include <sys/types.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
26 #include "mr_server.h"
33 extern void clist_delete(), do_auth(), do_shutdown();
35 extern int dbms_errno, mr_errcode;
38 /* Put this in a variable so that we can patch it if necessary */
39 int max_row_count = 4096;
42 * Welcome to the (finite state) machine (highest level).
51 if (OP_STATUS(cp->pending_op) == OP_CANCELLED) {
52 com_err(whoami, 0, "Closed connection (now %d client%s, %d new queries, %d old)",
58 /* if we no longer have any clients, and we're supposed to
59 * go down, then go down now.
61 if ((dormant == AWAKE) && (nclients == 0) &&
62 (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) {
63 com_err(whoami, 0, "motd file exists, slumbertime");
71 /* Start recieving next request */
72 initialize_operation(cp->pending_op, mr_start_recv,
73 (char *)&cp->args, (int (*)())NULL);
74 queue_operation(cp->con, CON_INPUT, cp->pending_op);
75 cp->action = CL_RECEIVE;
78 /* Data is here. Process it & start it heading back */
79 do_call(cp); /* This may block for a while. */
80 mr_destroy_reply(cp->args);
82 initialize_operation(cp->pending_op, mr_start_send,
83 (char *)&cp->reply, (int (*)())NULL);
84 queue_operation(cp->con, CON_OUTPUT, cp->pending_op);
106 cl->reply.mr_argc = 0;
107 cl->reply.mr_status = 0;
108 cl->reply.mr_version_no = cl->args->mr_version_no;
109 if (((pn = cl->args->mr_procno) < 0) ||
110 (pn > MR_MAX_PROC)) {
111 com_err(whoami, 0, "procno out of range");
112 cl->reply.mr_status = MR_UNKNOWN_PROC;
115 if (log_flags & LOG_ARGS)
116 log_args(procnames[pn], cl->args->mr_version_no,
117 cl->args->mr_argc, cl->args->mr_argv);
118 else if (log_flags & LOG_REQUESTS)
119 com_err(whoami, 0, "%s", procnames[pn]);
121 if ((dormant == ASLEEP || dormant == GROGGY) &&
122 pn != MR_NOOP && pn != MR_MOTD) {
123 cl->reply.mr_status = MR_DOWN;
124 if (log_flags & LOG_RES)
125 com_err(whoami, MR_DOWN, "(query refused)");
129 /* make sure this gets cleared before every operation */
134 cl->reply.mr_status = 0;
154 trigger_dcm(0, 0, cl);
166 register returned_tuples *temp;
167 for (temp=cp->first; temp && OP_DONE(temp->op); ) {
168 register returned_tuples *t1=temp;
170 if (t1 == cp->last) cp->last = NULL;
172 mr_destroy_reply(t1->retval);
173 delete_operation(t1->op);
179 retr_callback(argc, argv, p_cp)
181 register char **argv;
184 register client *cp = (client *)p_cp;
188 register char **nargv;
191 if (row_count++ >= max_row_count) {
192 dbms_errno = mr_errcode = MR_NO_MEM;
197 * This takes too much advantage of the fact that
198 * serialization of the data happens during the queue operation.
200 arg_tmp = (mr_params *)db_alloc(sizeof(mr_params));
201 tp = (returned_tuples *)db_alloc(sizeof(returned_tuples));
202 nargv = (char **)malloc(argc * sizeof(char *));
204 op_tmp = create_operation();
206 if (mr_trim_args(argc, argv) == MR_NO_MEM) {
207 com_err(whoami, MR_NO_MEM, "while trimming args");
209 if (log_flags & LOG_RESP)
210 log_args("return: ", cp->args->mr_version_no, argc, argv);
213 tp->retval = arg_tmp;
216 arg_tmp->mr_status = MR_MORE_DATA;
217 arg_tmp->mr_version_no = cp->args->mr_version_no;
218 arg_tmp->mr_argc = argc;
219 arg_tmp->mr_argv = nargv;
220 for (i = 0; i < argc; i++) {
221 register int len = strlen(argv[i]) + 1;
222 nargv[i] = malloc(len);
223 memcpy(nargv[i], argv[i], len);
225 arg_tmp->mr_flattened = (char *)NULL;
226 arg_tmp->mr_argl = (int *)NULL;
232 cp->last = cp->first = tp;
235 reset_operation(op_tmp);
236 initialize_operation(op_tmp, mr_start_send, (char *)arg_tmp,
238 queue_operation(cp->con, CON_OUTPUT, op_tmp);
241 list_users(callbk, callarg)
249 extern client **clients;
253 for (i = 0; i < nclients; i++) {
254 register client *cl = clients[i];
256 argv[0] = cl->clname;
257 else argv[0] = "unauthenticated";
259 argv[1] = inet_ntoa(cl->haddr.sin_addr);
261 sprintf(buf, "port %d", ntohs(cl->haddr.sin_port));
262 argv[3] = ctime(&cl->last_time_used);
263 cp = strchr(argv[3], '\n');
266 sprintf(buf1, "[#%d]", cl->id);
267 (*callbk)(5, argv, callarg);
275 register char *queryname;
277 cl->reply.mr_argc = 0;
278 cl->reply.mr_status = 0;
281 if (cl->args->mr_argc < 1) {
282 cl->reply.mr_status = MR_ARGS;
283 com_err(whoami, MR_ARGS, "got nameless query");
286 queryname = cl->args->mr_argv[0];
288 if (cl->args->mr_version_no == MR_VERSION_2)
293 if (strcmp(queryname, "_list_users") == 0)
294 cl->reply.mr_status = list_users(retr_callback, (char *)cl);
296 cl->reply.mr_status =
304 if (row_count >= max_row_count) {
305 critical_alert("moirad", "attempted query %s with %d rows\n",
306 queryname, row_count);
309 if (log_flags & LOG_RES)
310 com_err(whoami, 0, "Query complete.");
316 if (cl->args->mr_argc < 1) {
317 cl->reply.mr_status = MR_ARGS;
318 com_err(whoami, MR_ARGS, "got nameless access");
321 cl->reply.mr_argc = 0;
323 cl->reply.mr_status =
325 cl->args->mr_argv[0],
327 cl->args->mr_argv+1);
329 com_err(whoami, 0, "Access check complete.");
333 /* trigger_dcm is also used as a followup routine to the
334 * set_server_host_override query, hence the two dummy arguments.
337 struct query pseudo_query = {
342 trigger_dcm(dummy0, dummy1, cl)
349 cl->reply.mr_argc = 0;
351 if (cl->reply.mr_status = check_query_access(&pseudo_query, 0, cl) )
352 return(cl->reply.mr_status);
354 sprintf(prog, "%s/startdcm", BIN_DIR);
358 execl(prog, "startdcm", 0);
362 cl->reply.mr_status = errno;
379 cl->reply.mr_status = 0;
380 motd = open(MOIRA_MOTD_FILE, 0, O_RDONLY);
381 if (motd < 0) return;
382 len = read(motd, buffer, sizeof(buffer) - 1);
386 retr_callback(1, arg, cl);
387 cl->reply.mr_status = 0;