3 * The Data Control Manager for Moira.
5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, see the file
10 #include <mit-copyright.h>
12 #include <moira_site.h>
13 #include <moira_schema.h>
16 #include <sys/param.h>
27 EXEC SQL INCLUDE sqlca;
28 void sqlglm(char *, unsigned int *, unsigned int *);
32 int generate_service(char *name, int force);
33 void do_hosts(char *service);
34 int dcm_send_file(char *service, int type, char *host, char *target);
35 int dcm_execute(char *service, char *host, char *script);
38 #define SQL_NO_MATCH 1403
39 #define SOFT_FAIL(x) (((x) == MR_NO_MEM) || ((x) == MR_CANT_CONNECT) || ((x) == MR_CCONFIG) || ((x) == MR_DEADLOCK) || ((x) == MR_BUSY) || ((x) == MR_ABORT))
41 /* argument parsing macro */
42 #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
44 char whobuf[256], *whoami = whobuf, *db = "moira";
46 enum { UNIQUE, DISTRIBUTED, REPLICATED };
48 int main(int argc, char **argv)
51 EXEC SQL BEGIN DECLARE SECTION;
52 char buf[SERVERS_NAME_SIZE], *name;
54 EXEC SQL END DECLARE SECTION;
55 struct save_queue *sq;
56 int status, srvcnt = 0;
57 char **arg = argv, *services[BUFSIZ];
59 if (strchr(argv[0], '/'))
60 strcpy(whoami, strrchr(argv[0], '/') + 1);
61 else strcpy(whoami, argv[0]);
64 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
65 setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
67 initialize_sms_error_table();
68 initialize_krb_error_table();
70 while (++arg - argv < argc)
74 if (argis("f", "force"))
78 com_err(whoami, 0, "Usage: %s [-f] servicename", argv[0]);
83 /* Doesn't begin with a dash, is a service name.
84 * Build an array of them we can iterate through later.
87 services[srvcnt] = malloc(SERVERS_NAME_SIZE);
88 if (!services[srvcnt])
90 com_err(whoami, 0, "Out of memory!");
93 strncpy(services[srvcnt], *arg, SERVERS_NAME_SIZE);
98 /* Iterate through services specified on the command line, if any. */
101 for (i = 0; i < srvcnt; i++)
103 if (generate_service(services[i], force))
105 do_hosts(services[i]);
112 /* if DCM is not enabled, exit after logging */
113 if (!access(NODCMFILE, F_OK))
115 printf("/etc/nodcm exists -- exiting\n");
119 EXEC SQL WHENEVER SQLERROR DO dbmserr();
121 EXEC SQL CONNECT :db IDENTIFIED BY :db;
123 EXEC SQL SELECT value INTO :enable FROM numvalues WHERE name = 'dcm_enable';
126 printf("dcm_enable not set -- exiting\n");
130 /* fetch list of services */
131 EXEC SQL DECLARE csr_svc CURSOR FOR SELECT LOWER(name) FROM servers
132 WHERE enable = 1 AND harderror = 0 AND update_int > 0;
133 EXEC SQL OPEN csr_svc;
137 EXEC SQL FETCH csr_svc INTO :buf;
141 sq_save_data(sq, strdup(strtrim(buf)));
143 EXEC SQL CLOSE csr_svc;
144 /* we will repeatedly open and close the db since it seems to get
145 upset if you keep it open across a fork */
146 EXEC SQL COMMIT RELEASE;
148 /* Now run through list */
149 while (sq_get_data(sq, &name))
151 if (generate_service(name, force))
156 com_err(whoami, errno, "forking for service %s -- exiting",
160 sprintf(strchr(whoami, '\0'), " (%s:%ld)", name, (long)getpid());
162 com_err(whoami, 0, "exiting");
170 com_err(whoami, 0, "All files generated. Waiting for children to exit");
171 while (waitpid(0, &status, 0) > 0)
173 com_err(whoami, 0, "exiting");
177 int generate_service(char *name, int force)
179 EXEC SQL BEGIN DECLARE SECTION;
180 int interval, dfcheck, status, inprogress;
183 EXEC SQL END DECLARE SECTION;
184 char dfgen_prog[MAXPATHLEN], dfgen_cmd[2 * MAXPATHLEN];
185 struct sigaction action, prevaction;
188 EXEC SQL CONNECT :db IDENTIFIED BY :db;
190 EXEC SQL SELECT update_int, dfcheck, inprogress INTO :interval, :dfcheck,
191 :inprogress FROM servers WHERE name = UPPER(:name);
192 if (sqlca.sqlcode == SQL_NO_MATCH)
194 com_err(whoami, 0, "No such service `%s'", name);
195 EXEC SQL COMMIT RELEASE;
199 /* Someone might try to run a DCM from the command line while the
200 * regular one is running, which will bypass the "interval" test.
201 * Check inprogress to make sure they don't stomp on themselves.
203 * Note that there is still a race condition here, and this doesn't
204 * absolutely prevent 2 DCMs from stepping on one another, but it
205 * does reduce the window of vulnerability greatly.
209 com_err(whoami, 0, "DCM for service `%s' already in progress", name);
210 EXEC SQL COMMIT RELEASE;
216 if ((interval * 60 + dfcheck < now) || force)
218 sprintf(dfgen_prog, "%s/%s.gen", BIN_DIR, name);
219 if (access(dfgen_prog, F_OK) != 0)
221 com_err(whoami, 0, "prog %s doesn't exist", dfgen_prog);
222 EXEC SQL COMMIT RELEASE;
225 sprintf(dfgen_cmd, "exec %s %s/%s.out", dfgen_prog, DCM_DIR, name);
226 com_err(whoami, 0, "running %s", dfgen_prog);
228 EXEC SQL WHENEVER SQLERROR GOTO gen_cleanup;
230 EXEC SQL UPDATE servers SET inprogress = 1
231 WHERE name = UPPER(:name);
235 sigemptyset(&action.sa_mask);
236 action.sa_handler = SIG_DFL;
237 sigaction(SIGCHLD, &action, &prevaction);
238 waits = system(dfgen_cmd);
239 sigaction(SIGCHLD, &prevaction, NULL);
240 if (WIFSIGNALED(waits))
242 status = MR_COREDUMP;
243 com_err(whoami, status, " %s exited on signal %d",
244 dfgen_prog, WTERMSIG(waits));
246 else if (WEXITSTATUS(waits))
248 /* extract the process's exit value */
249 status = WEXITSTATUS(waits) + ERROR_TABLE_BASE_sms;
250 if (status != MR_NO_CHANGE)
251 com_err(whoami, status, "in %s", dfgen_prog);
256 if (status == MR_SUCCESS)
258 EXEC SQL UPDATE servers SET dfgen = :now, dfcheck = :now,
259 inprogress = 0 WHERE name = UPPER(:name);
260 EXEC SQL COMMIT RELEASE;
263 else if (status == MR_NO_CHANGE)
265 EXEC SQL UPDATE servers SET dfcheck = :now, inprogress = 0
266 WHERE name = UPPER(:name);
268 else if (SOFT_FAIL(status))
270 errmsg = error_message(status);
271 EXEC SQL UPDATE servers SET errmsg = :errmsg, inprogress = 0
272 WHERE name = UPPER(:name);
274 else /* HARD_FAIL(status) */
276 errmsg = error_message(status);
277 EXEC SQL UPDATE servers SET harderror = :status, errmsg = :errmsg,
278 inprogress = 0 WHERE name = UPPER(:name);
279 critical_alert("DCM", "DCM building config files for %s: %s",
285 com_err(whoami, 0, "DCM for service `%s' has run too recently.", name);
286 com_err(whoami, 0, "Use the -force flag to force a DCM.");
289 EXEC SQL COMMIT RELEASE;
293 EXEC SQL WHENEVER SQLERROR DO dbmserr();
294 EXEC SQL UPDATE servers SET inprogress = 0, harderror = MR_INTERNAL,
295 errmsg = 'DBMS Internal Error' WHERE name = UPPER(:name);
299 void do_hosts(char *service)
301 EXEC SQL BEGIN DECLARE SECTION;
302 char server_type[SERVERS_TYPE_SIZE], host[MACHINE_NAME_SIZE], *name;
303 char target[SERVERS_TARGET_FILE_SIZE], script[SERVERS_SCRIPT_SIZE];
305 int status = 0, dfgen, type, mid, inprogress;
307 EXEC SQL END DECLARE SECTION;
308 struct save_queue *sq;
313 EXEC SQL CONNECT :db IDENTIFIED BY :db;
315 EXEC SQL SELECT dfgen, type, target_file, script, inprogress
316 INTO :dfgen, :server_type, :target, :script, :inprogress
317 FROM servers WHERE name = UPPER(:service);
318 if (!strncmp(strtrim(server_type), "REPLICAT", 8))
320 else if (!strncmp(server_type, "DISTRIB", 8))
327 /* Rudimentary locking. Doesn't eliminate the possibility of 2 DCMs
328 * stepping on one another, but makes it harder.
332 com_err(whoami, 0, "DCM for service `%s' already in progress", name);
333 EXEC SQL COMMIT RELEASE;
337 EXEC SQL DECLARE csr_hst1 CURSOR FOR
338 SELECT m.name, m.mach_id FROM machine m, serverhosts sh
339 WHERE sh.service = UPPER(:service)
340 AND sh.enable = 1 AND sh.hosterror = 0
341 AND sh.lts < :dfgen AND sh.mach_id = m.mach_id;
342 EXEC SQL OPEN csr_hst1;
346 EXEC SQL FETCH csr_hst1 INTO :host, mid;
347 if (sqlca.sqlcode == SQL_NO_MATCH)
350 sq_save_data(sq, strdup(strtrim(host)));
351 sq_save_data(sq, (void *)mid);
353 EXEC SQL CLOSE csr_hst1;
355 EXEC SQL WHENEVER SQLERROR GOTO host_cleanup;
356 while (sq_get_data(sq, &name))
358 sq_get_data(sq, &mid);
360 EXEC SQL SELECT inprogress INTO :inprogress FROM serverhosts
361 WHERE service = UPPER(:service) AND mach_id = :mid;
362 /* Check if someone got here before we did.
363 * There's still a race condition here, but it's a small one. */
366 com_err(whoami, 0, "DCM for service `%s' to host `%s' already in progress", service, name);
367 EXEC SQL COMMIT RELEASE;
371 com_err(whoami, 0, "sending %s data to %s", service, name);
372 EXEC SQL UPDATE serverhosts SET inprogress = 1
373 WHERE service = UPPER(:service) AND mach_id = :mid;
375 status = dcm_send_file(service, type, name, target);
378 errmsg = error_message(status);
379 EXEC SQL UPDATE serverhosts SET hosterrmsg = :errmsg,
380 inprogress = 0, success = 0, ltt = :now
381 WHERE service = UPPER(:service) AND mach_id = :mid;
382 if (!SOFT_FAIL(status))
384 EXEC SQL UPDATE serverhosts SET hosterror = :status
385 WHERE service = UPPER(:service) AND mach_id = :mid;
386 critical_alert("DCM", "DCM updating %s:%s: %s",
387 service, name, errmsg);
391 if (type == REPLICATED)
397 if (status == MR_SUCCESS || type != REPLICATED)
399 EXEC SQL DECLARE csr_hst2 CURSOR FOR
400 SELECT m.name, m.mach_id FROM machine m, serverhosts sh
401 WHERE sh.service = UPPER(:service) AND sh.inprogress = 1
402 AND sh.enable = 1 AND sh.hosterror = 0 AND sh.mach_id = m.mach_id;
403 EXEC SQL OPEN csr_hst2;
408 EXEC SQL FETCH csr_hst2 INTO :host, :mid;
409 if (sqlca.sqlcode == SQL_NO_MATCH)
412 sq_save_data(sq, strdup(strtrim(host)));
413 sq_save_data(sq, (void *)mid);
415 EXEC SQL CLOSE csr_hst2;
417 while (sq_get_data(sq, &name))
419 sq_get_data(sq, &mid);
421 com_err(whoami, 0, "executing instructions on %s", name);
422 status = dcm_execute(service, name, script);
425 errmsg = error_message(status);
426 EXEC SQL UPDATE serverhosts SET hosterrmsg = :errmsg,
427 inprogress = 0, success = 0, ltt = :now
428 WHERE service = UPPER(:service) AND mach_id = :mid;
429 if (!SOFT_FAIL(status))
431 EXEC SQL UPDATE serverhosts SET hosterror = :status
432 WHERE service = UPPER(:service) AND mach_id = :mid;
433 critical_alert("DCM", "DCM updating %s:%s: %s",
434 service, name, errmsg);
437 if (type == REPLICATED)
442 EXEC SQL UPDATE serverhosts SET inprogress = 0, ltt = :now,
443 lts = :now, success = 1 WHERE service = UPPER(:service)
448 EXEC SQL CLOSE csr_hst2;
451 if (type == REPLICATED)
453 /* Clear inprogress flag on any hosts we started but didn't
456 EXEC SQL UPDATE serverhosts SET inprogress = 0
457 WHERE service = UPPER(:service);
460 EXEC SQL WHENEVER SQLERROR DO dbmserr();
461 if (status && !SOFT_FAIL(status) && type == REPLICATED)
463 EXEC SQL UPDATE servers SET harderror = :status, errmsg = :errmsg
464 WHERE name = UPPER(:service);
467 EXEC SQL COMMIT RELEASE;
471 EXEC SQL UPDATE serverhosts SET inprogress = 0, success = 0, ltt = :now,
472 hosterror = MR_INTERNAL, hosterrmsg = 'DBMS Internal Error'
473 WHERE service = UPPER(:service) AND mach_id = :mid;
474 if (type == REPLICATED)
476 EXEC SQL UPDATE servers SET harderror = MR_INTERNAL,
477 errmsg = 'DBMS Internal Error' WHERE name = UPPER(:service);
481 int dcm_send_file(char *service, int type, char *host, char *target)
483 char data[MAXPATHLEN];
486 conn = mr_connect_internal(host, "moira_update");
489 com_err(whoami, errno, "can't connect to %s", host);
490 return MR_CANT_CONNECT;
493 code = mr_send_krb5_auth(conn, host);
495 code = mr_send_auth(conn, host);
498 com_err(whoami, code, "authenticating to %s", host);
502 if (type == DISTRIBUTED)
503 sprintf(data, "%s/%s/%s", DCM_DIR, service, host);
505 sprintf(data, "%s/%s.out", DCM_DIR, service);
506 code = mr_send_file(conn, data, target, 0);
508 com_err(whoami, code, "sending data to %s", host);
516 int dcm_execute(char *service, char *host, char *script)
518 char inst[MAXPATHLEN];
521 conn = mr_connect_internal(host, "moira_update");
524 com_err(whoami, errno, "can't connect to %s", host);
525 return MR_CANT_CONNECT;
528 code = mr_send_auth(conn, host);
531 com_err(whoami, code, "authenticating to %s", host);
535 sprintf(inst, "/tmp/moira-update.XXXXXX");
537 code = mr_send_file(conn, script, inst, 0);
540 com_err(whoami, code, "sending instructions to %s", host);
544 code = mr_execute(conn, inst);
546 com_err(whoami, code, "executing instructions on %s", host);
556 EXEC SQL BEGIN DECLARE SECTION;
558 EXEC SQL END DECLARE SECTION;
559 int bufsize = 256, msglength = 0;
561 sqlglm(err_msg, &bufsize, &msglength);
562 err_msg[msglength] = '\0';
563 com_err(whoami, 0, "Encountered SQL error:\n%s", err_msg);
564 com_err(whoami, 0, "exiting");