2 * The Data Control Manager for MOIRA.
4 * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
5 * For copying and distribution information, see the file
14 static char rcsid_dcm_c[] = "$Header$";
25 #include <moira_site.h>
27 #include "mit-copyright.h"
30 extern char *getenv();
32 extern char *error_message();
38 /* declared global so that we can get the current time from different places. */
52 dbg = atoi(getenv("DEBUG"));
58 while(++arg - argv < argc) {
62 dbg = atoi((*arg)[2]? *arg+2: *++arg);
66 set_com_err_hook(dcm_com_err_hook);
68 /* if /etc/nodcm exists, punt quietly. */
69 if (!access(NODCMFILE, F_OK)) {
73 if (status = mr_connect("")) {
74 com_err(whoami, status, " on mr_connect");
75 leave("connect failed");
78 if (status = mr_auth("dcm")) {
79 com_err(whoami, status, " on \"authenticate\"");
83 /* if DCM is not enabled, exit after logging */
84 qargv[0] = "dcm_enable";
85 if (status = mr_query("get_value", 1, qargv, gqval, &i)) {
86 com_err(whoami, status, " check dcm_enable");
87 leave("query failed");
91 leave("dcm_enable not set");
101 /* Used by the get_value query when checking for dcm_enable. */
103 gqval(argc, argv, hint)
108 *hint = atoi(argv[0]);
113 /* Used by qualified_get_server to make a list of servers to check */
115 qgetsv(argc, argv, sq)
118 struct save_queue *sq;
120 sq_save_data(sq, strsave(argv[0]));
125 /* Used by get_server_info to record all of the returned information */
127 getsvinfo(argc, argv, sserv)
130 struct service *sserv;
132 sserv->service = strsave(argv[0]);
133 sserv->interval = atoi(argv[1]);
134 sserv->target = strsave(argv[2]);
135 sserv->script = strsave(argv[3]);
136 sserv->dfgen = atoi(argv[4]);
137 sserv->dfcheck = atoi(argv[5]);
138 sserv->type = strsave(argv[6]);
139 sserv->enable = atoi(argv[7]);
140 sserv->inprogress = atoi(argv[8]);
141 sserv->harderror = atoi(argv[9]);
142 sserv->errmsg = strsave(argv[10]);
147 /* Scan the services and process any that need it. */
152 struct save_queue *sq, *sq_create();
153 char *service, dfgen_prog[64], dfgen_cmd[128];
155 int status, lock_fd, ex, (*cstat)();
160 if (dbg & DBG_VERBOSE)
161 com_err(whoami, 0, "starting pass over services");
164 qargv[1] = "dontcare";
167 if (status = mr_query_with_retry("qualified_get_server", 3, qargv,
169 com_err(whoami, status, " getting services");
170 leave("query failed");
172 while (sq_get_data(sq, &service)) {
173 for (p = service; *p; p++)
176 com_err(whoami, 0, "checking %s...", service);
178 sprintf(dfgen_prog, "%s/%s.gen", BIN_DIR, service);
179 if (!file_exists(dfgen_prog)) {
180 com_err(whoami, 0, "prog %s doesn't exist\n", dfgen_prog);
184 sprintf(dfgen_cmd, "exec %s %s/%s.out",
185 dfgen_prog, DCM_DIR, service);
186 gettimeofday(&tv, &tz);
187 if (status = mr_query_with_retry("get_server_info", 1, qargv,
189 com_err(whoami, status, " getting service %s info, skipping to next service", service);
192 svc.service = strsave(service);
193 qargv[0] = strsave(service);
194 qargv[1] = itoa(svc.dfgen);
195 qargv[2] = itoa(svc.dfcheck);
196 qargv[3] = strsave("0");
197 qargv[4] = itoa(svc.harderror);
198 qargv[5] = strsave(svc.errmsg);
199 if (svc.interval != 0) {
200 if (svc.interval * 60 + svc.dfcheck < tv.tv_sec) {
201 lock_fd = maybe_lock_update("@db@", service, 1);
207 qargv[3] = strsave("1");
208 qargv[4] = strsave("0");
209 qargv[5] = strsave("");
210 status = mr_query_with_retry("set_server_internal_flags", 6,
211 qargv, scream, NULL);
212 if (status != MR_SUCCESS) {
213 com_err(whoami, status, " setting server state");
217 com_err(whoami, status, " running %s", dfgen_prog);
218 cstat = signal(SIGCHLD, SIG_DFL);
219 waits.w_status = system(dfgen_cmd);
220 signal(SIGCHLD, cstat);
221 if (waits.w_termsig) {
222 status = MR_TAR_FAIL;
223 com_err(whoami, status, " %s exited on signal %d",
224 dfgen_prog, waits.w_termsig);
225 } else if (waits.w_retcode) {
226 /* extract the process's exit value */
227 status = waits.w_retcode + ERROR_TABLE_BASE_sms;
228 com_err(whoami, status, " %s exited", dfgen_prog);
230 if (SOFT_FAIL(status)) {
232 qargv[5] = strsave(error_message(status));
233 } else if (status == MR_NO_CHANGE) {
235 qargv[2] = itoa(tv.tv_sec);
236 svc.dfcheck = tv.tv_sec;
237 } else if (status == MR_SUCCESS) {
240 qargv[1] = itoa(tv.tv_sec);
241 qargv[2] = strsave(qargv[1]);
242 svc.dfcheck = svc.dfgen = tv.tv_sec;
243 } else { /* HARD_FAIL(status) */
247 qargv[2] = itoa(tv.tv_sec);
248 svc.dfcheck = tv.tv_sec;
249 qargv[4] = itoa(status);
250 qargv[5] = strsave(error_message(status));
251 critical_alert("DCM","DCM building config files for %s: %s",
256 qargv[3] = strsave("0");
257 status = mr_query_with_retry("set_server_internal_flags", 6,
258 qargv, scream, NULL);
260 com_err(whoami, status, " setting service state");
269 if (!strcmp(svc.type, "REPLICAT"))
273 lock_fd = maybe_lock_update("@db@", service, ex);
290 /* Used by qualified_get_server_host to make a list of hosts to check */
292 qgethost(argc, argv, sq)
295 struct save_queue *sq;
297 sq_save_data(sq, strsave(argv[1]));
302 /* Used by get_server_host_info to store all of the info about a host */
304 gethostinfo(argc, argv, shost)
307 struct svrhost *shost;
309 shost->service = strsave(argv[0]);
310 shost->machine = strsave(argv[1]);
311 shost->enable = atoi(argv[2]);
312 shost->override = atoi(argv[3]);
313 shost->success = atoi(argv[4]);
314 shost->inprogress = atoi(argv[5]);
315 shost->hosterror = atoi(argv[6]);
316 shost->errmsg = strsave(argv[7]);
317 shost->lasttry = atoi(argv[8]);
318 shost->lastsuccess = atoi(argv[9]);
319 shost->value1 = atoi(argv[10]);
320 shost->value2 = atoi(argv[11]);
321 shost->value3 = strsave(argv[12]);
326 /* Scans all of the hosts for a particular service, and processes them. */
331 char *argv[9], *machine;
333 struct save_queue *sq;
334 struct svrhost shost;
337 argv[0] = svc->service;
339 argv[2] = argv[3] = argv[4] = "DONTCARE";
341 status = mr_query_with_retry("qualified_get_server_host", 6, argv,
343 if (status == MR_NO_MATCH) {
346 com_err(whoami, status, " getting server_hosts for %s", svc->service);
349 while (sq_get_data(sq, &machine)) {
351 com_err(whoami, 0, "checking %s...", machine);
353 status = mr_query_with_retry("get_server_host_info", 2, argv,
354 gethostinfo, &shost);
356 com_err(whoami,status, " getting server_host_info for %s", machine);
359 if (!shost.enable || shost.hosterror ||
360 (shost.success && !shost.override &&
361 shost.lastsuccess >= svc->dfgen)) {
363 com_err(whoami, 0, "not updating %s:%s", svc->service, machine);
367 lock_fd = maybe_lock_update(machine, svc->service, 1);
370 argv[0] = svc->service;
372 argv[2] = argv[3] = argv[5] = "0";
374 argv[6] = strsave("");
375 argv[7] = itoa(tv.tv_sec);
376 argv[8] = itoa(shost.lastsuccess);
377 status = mr_query_with_retry("set_server_host_internal", 9, argv,
379 if (status != MR_SUCCESS) {
380 com_err(whoami,status," while setting internal state for %s:%s",
381 svc->service, machine);
384 status = mr_update_server(svc->service, machine, svc->target,
386 if (status == MR_SUCCESS) {
390 argv[8] = itoa(tv.tv_sec);
391 } else if (SOFT_FAIL(status)) {
393 argv[6] = strsave(error_message(status));
394 } else { /* HARD_FAIL */
395 argv[2] = itoa(shost.override);
396 argv[5] = itoa(status);
398 argv[6] = strsave(error_message(status));
399 critical_alert("DCM", "DCM updating %s:%s: %s",
400 machine, svc->service, argv[6]);
401 if (!strcmp(svc->type, "REPLICAT")) {
404 svc->harderror = status;
405 svc->errmsg = strsave(argv[6]);
406 qargv[0] = strsave(svc->service);
407 qargv[1] = itoa(svc->dfgen);
408 qargv[2] = itoa(svc->dfcheck);
409 qargv[3] = strsave("0");
410 qargv[4] = itoa(svc->harderror);
411 qargv[5] = strsave(svc->errmsg);
412 status = mr_query_with_retry("set_server_internal_flags",
413 6, qargv, scream, NULL);
415 com_err(whoami, status, " setting service state again");
426 status = mr_query_with_retry("set_server_host_internal",
427 9, argv,scream,NULL);
429 com_err(whoami, status, " setting host state again");
437 status = mr_query_with_retry("set_server_host_internal", 9, argv,
440 com_err(whoami, status, " setting host state again");