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 <arpa/inet.h>
25 #include "mr_server.h"
32 extern void clist_delete(), do_auth(), do_shutdown();
34 extern int dbms_errno, mr_errcode;
37 /* Put this in a variable so that we can patch it if necessary */
38 int max_row_count = 4096;
41 * Welcome to the (finite state) machine (highest level).
50 if (OP_STATUS(cp->pending_op) == OP_CANCELLED) {
51 com_err(whoami, 0, "Closed connection (now %d client%s, %d new queries, %d old)",
57 /* if we no longer have any clients, and we're supposed to
58 * go down, then go down now.
60 if ((dormant == AWAKE) && (nclients == 0) &&
61 (stat(MOIRA_MOTD_FILE, &stbuf) == 0)) {
62 com_err(whoami, 0, "motd file exists, slumbertime");
70 /* Start recieving next request */
71 initialize_operation(cp->pending_op, mr_start_recv,
72 (char *)&cp->args, (int (*)())NULL);
73 queue_operation(cp->con, CON_INPUT, cp->pending_op);
74 cp->action = CL_RECEIVE;
77 /* Data is here. Process it & start it heading back */
78 do_call(cp); /* This may block for a while. */
79 mr_destroy_reply(cp->args);
81 initialize_operation(cp->pending_op, mr_start_send,
82 (char *)&cp->reply, (int (*)())NULL);
83 queue_operation(cp->con, CON_OUTPUT, cp->pending_op);
105 cl->reply.mr_argc = 0;
106 cl->reply.mr_status = 0;
107 cl->reply.mr_version_no = cl->args->mr_version_no;
108 if (((pn = cl->args->mr_procno) < 0) ||
109 (pn > MR_MAX_PROC)) {
110 com_err(whoami, 0, "procno out of range");
111 cl->reply.mr_status = MR_UNKNOWN_PROC;
114 if (log_flags & LOG_ARGS)
115 log_args(procnames[pn], cl->args->mr_version_no,
116 cl->args->mr_argc, cl->args->mr_argv);
117 else if (log_flags & LOG_REQUESTS)
118 com_err(whoami, 0, "%s", procnames[pn]);
120 if ((dormant == ASLEEP || dormant == GROGGY) &&
121 pn != MR_NOOP && pn != MR_MOTD) {
122 cl->reply.mr_status = MR_DOWN;
123 if (log_flags & LOG_RES)
124 com_err(whoami, MR_DOWN, "(query refused)");
128 /* make sure this gets cleared before every operation */
133 cl->reply.mr_status = 0;
153 trigger_dcm(0, 0, cl);
165 register returned_tuples *temp;
166 for (temp=cp->first; temp && OP_DONE(temp->op); ) {
167 register returned_tuples *t1=temp;
169 if (t1 == cp->last) cp->last = NULL;
171 mr_destroy_reply(t1->retval);
172 delete_operation(t1->op);
178 retr_callback(argc, argv, p_cp)
180 register char **argv;
183 register client *cp = (client *)p_cp;
187 register char **nargv;
190 if (row_count++ >= max_row_count) {
191 dbms_errno = mr_errcode = MR_NO_MEM;
196 * This takes too much advantage of the fact that
197 * serialization of the data happens during the queue operation.
199 arg_tmp = (mr_params *)db_alloc(sizeof(mr_params));
200 tp = (returned_tuples *)db_alloc(sizeof(returned_tuples));
201 nargv = (char **)malloc(argc * sizeof(char *));
203 op_tmp = create_operation();
205 if (mr_trim_args(argc, argv) == MR_NO_MEM) {
206 com_err(whoami, MR_NO_MEM, "while trimming args");
208 if (log_flags & LOG_RESP)
209 log_args("return: ", cp->args->mr_version_no, argc, argv);
212 tp->retval = arg_tmp;
215 arg_tmp->mr_status = MR_MORE_DATA;
216 arg_tmp->mr_version_no = cp->args->mr_version_no;
217 arg_tmp->mr_argc = argc;
218 arg_tmp->mr_argv = nargv;
219 for (i = 0; i < argc; i++) {
220 register int len = strlen(argv[i]) + 1;
221 nargv[i] = malloc(len);
222 memcpy(nargv[i], argv[i], len);
224 arg_tmp->mr_flattened = (char *)NULL;
225 arg_tmp->mr_argl = (int *)NULL;
231 cp->last = cp->first = tp;
234 reset_operation(op_tmp);
235 initialize_operation(op_tmp, mr_start_send, (char *)arg_tmp,
237 queue_operation(cp->con, CON_OUTPUT, op_tmp);
240 list_users(callbk, callarg)
248 extern client **clients;
252 for (i = 0; i < nclients; i++) {
253 register client *cl = clients[i];
255 argv[0] = cl->clname;
256 else argv[0] = "unauthenticated";
258 argv[1] = inet_ntoa(cl->haddr.sin_addr);
260 sprintf(buf, "port %d", ntohs(cl->haddr.sin_port));
261 argv[3] = ctime(&cl->last_time_used);
262 cp = strchr(argv[3], '\n');
265 sprintf(buf1, "[#%d]", cl->id);
266 (*callbk)(5, argv, callarg);
274 register char *queryname;
276 cl->reply.mr_argc = 0;
277 cl->reply.mr_status = 0;
280 if (cl->args->mr_argc < 1) {
281 cl->reply.mr_status = MR_ARGS;
282 com_err(whoami, MR_ARGS, "got nameless query");
285 queryname = cl->args->mr_argv[0];
287 if (cl->args->mr_version_no == MR_VERSION_2)
292 if (strcmp(queryname, "_list_users") == 0)
293 cl->reply.mr_status = list_users(retr_callback, (char *)cl);
295 cl->reply.mr_status =
303 if (row_count >= max_row_count) {
304 critical_alert("moirad", "attempted query %s with %d rows\n",
305 queryname, row_count);
308 if (log_flags & LOG_RES)
309 com_err(whoami, 0, "Query complete.");
315 if (cl->args->mr_argc < 1) {
316 cl->reply.mr_status = MR_ARGS;
317 com_err(whoami, MR_ARGS, "got nameless access");
320 cl->reply.mr_argc = 0;
322 cl->reply.mr_status =
324 cl->args->mr_argv[0],
326 cl->args->mr_argv+1);
328 com_err(whoami, 0, "Access check complete.");
332 /* trigger_dcm is also used as a followup routine to the
333 * set_server_host_override query, hence the two dummy arguments.
336 struct query pseudo_query = {
341 trigger_dcm(dummy0, dummy1, cl)
348 cl->reply.mr_argc = 0;
350 if (cl->reply.mr_status = check_query_access(&pseudo_query, 0, cl) )
351 return(cl->reply.mr_status);
353 sprintf(prog, "%s/startdcm", BIN_DIR);
357 execl(prog, "startdcm", 0);
361 cl->reply.mr_status = errno;
378 cl->reply.mr_status = 0;
379 motd = open(MOIRA_MOTD_FILE, 0, O_RDONLY);
380 if (motd < 0) return;
381 len = read(motd, buffer, sizeof(buffer) - 1);
385 retr_callback(1, arg, cl);
386 cl->reply.mr_status = 0;