2 * The Data Control Manager for SMS.
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$";
27 #include "mit-copyright.h"
30 extern char *getenv();
32 extern char *error_message();
38 #define DEADLOCK_WAIT (3 * 60) /* number of seconds to wait after
39 a deadlock before trying again. */
41 /* declared global so that we can get the current time from different places. */
55 dbg = atoi(getenv("DEBUG"));
61 while(++arg - argv < argc) {
65 dbg = atoi((*arg)[2]? *arg+2: *++arg);
69 set_com_err_hook(dcm_com_err_hook);
71 /* if /etc/nodcm exists, punt quietly. */
72 if (!access(NODCMFILE, F_OK)) {
76 if (status = sms_connect("")) {
77 com_err(whoami, status, " on sms_connect");
78 leave("connect failed");
81 if (status = sms_auth("dcm")) {
82 com_err(whoami, status, " on \"authenticate\"");
86 /* if DCM is not enabled, exit after logging */
87 qargv[0] = "dcm_enable";
88 if (status = sms_query("get_value", 1, qargv, gqval, &i)) {
89 com_err(whoami, status, " check dcm_enable");
90 leave("query failed");
94 leave("dcm_enable not set");
104 /* Used by the get_value query when checking for dcm_enable. */
106 gqval(argc, argv, hint)
111 *hint = atoi(argv[0]);
116 /* Used by qualified_get_server to make a list of servers to check */
118 qgetsv(argc, argv, sq)
121 struct save_queue *sq;
123 sq_save_data(sq, strsave(argv[0]));
128 /* Used by get_server_info to record all of the returned information */
130 getsvinfo(argc, argv, sserv)
133 struct service *sserv;
135 sserv->service = strsave(argv[0]);
136 sserv->interval = atoi(argv[1]);
137 sserv->target = strsave(argv[2]);
138 sserv->script = strsave(argv[3]);
139 sserv->dfgen = atoi(argv[4]);
140 sserv->dfcheck = atoi(argv[5]);
141 sserv->type = strsave(argv[6]);
142 sserv->enable = atoi(argv[7]);
143 sserv->inprogress = atoi(argv[8]);
144 sserv->harderror = atoi(argv[9]);
145 sserv->errmsg = strsave(argv[10]);
150 /* Scan the services and process any that need it. */
155 struct save_queue *sq, *sq_create();
156 char *service, dfgen_prog[64], dfgen_cmd[128];
158 int status, lock_fd, ex, (*cstat)();
163 if (dbg & DBG_VERBOSE)
164 com_err(whoami, 0, "starting pass over services");
167 qargv[1] = "dontcare";
170 if (status = sms_query("qualified_get_server", 3, qargv, qgetsv, sq)) {
171 com_err(whoami, status, " getting services");
172 leave("query failed");
174 while (sq_get_data(sq, &service)) {
175 for (p = service; *p; p++)
178 com_err(whoami, 0, "checking %s...", service);
180 sprintf(dfgen_prog, "%s/%s.gen", BIN_DIR, service);
181 if (!file_exists(dfgen_prog)) {
182 com_err(whoami, 0, "prog %s doesn't exist\n", dfgen_prog);
186 sprintf(dfgen_cmd, "exec %s %s/%s.out",
187 dfgen_prog, DCM_DIR, service);
188 gettimeofday(&tv, &tz);
189 if (status = sms_query("get_server_info", 1, qargv, getsvinfo, &svc)) {
190 com_err(whoami, status, " getting service %s info, skipping to next service", service);
193 svc.service = strsave(service);
194 qargv[0] = strsave(service);
195 qargv[1] = itoa(svc.dfgen);
196 qargv[2] = itoa(svc.dfcheck);
197 qargv[3] = strsave("0");
198 qargv[4] = itoa(svc.harderror);
199 qargv[5] = strsave(svc.errmsg);
200 if (svc.interval != 0) {
201 if (svc.interval * 60 + svc.dfcheck < tv.tv_sec) {
202 lock_fd = maybe_lock_update("@db@", service, 1);
208 qargv[3] = strsave("1");
209 qargv[4] = strsave("0");
210 qargv[5] = strsave("");
211 status = sms_query("set_server_internal_flags", 6, qargv,
213 if (status != SMS_SUCCESS) {
214 com_err(whoami, status, " setting server state");
218 com_err(whoami, status, " running %s", dfgen_prog);
219 cstat = signal(SIGCHLD, SIG_DFL);
220 waits.w_status = system(dfgen_cmd);
221 signal(SIGCHLD, cstat);
222 if (waits.w_termsig) {
223 status = SMS_TAR_FAIL;
224 com_err(whoami, status, " %s exited on signal %d",
225 dfgen_prog, waits.w_termsig);
226 } else if (waits.w_retcode) {
227 /* extract the process's exit value */
228 status = waits.w_retcode + sms_err_base;
229 com_err(whoami, status, " %s exited", dfgen_prog);
231 if (SOFT_FAIL(status)) {
233 qargv[5] = strsave(error_message(status));
234 } else if (status == SMS_NO_CHANGE) {
236 qargv[2] = itoa(tv.tv_sec);
237 svc.dfcheck = tv.tv_sec;
238 } else if (status == SMS_SUCCESS) {
241 qargv[1] = itoa(tv.tv_sec);
242 qargv[2] = strsave(qargv[1]);
243 svc.dfcheck = svc.dfgen = tv.tv_sec;
244 } else { /* HARD_FAIL(status) */
248 qargv[2] = itoa(tv.tv_sec);
249 svc.dfcheck = tv.tv_sec;
250 qargv[4] = itoa(status);
251 qargv[5] = strsave(error_message(status));
252 critical_alert("DCM","DCM building config files for %s: %s",
257 qargv[3] = strsave("0");
258 status = sms_query("set_server_internal_flags", 6, qargv,
261 com_err(whoami, status,
262 " setting service state, sleeping");
263 sleep(DEADLOCK_WAIT);
264 status = sms_query("set_server_internal_flags", 6, qargv,
267 com_err(whoami, status, " setting service state again");
277 if (!strcmp(svc.type, "REPLICAT"))
281 lock_fd = maybe_lock_update("@db@", service, ex);
298 /* Used by qualified_get_server_host to make a list of hosts to check */
300 qgethost(argc, argv, sq)
303 struct save_queue *sq;
305 sq_save_data(sq, strsave(argv[1]));
310 /* Used by get_server_host_info to store all of the info about a host */
312 gethostinfo(argc, argv, shost)
315 struct svrhost *shost;
317 shost->service = strsave(argv[0]);
318 shost->machine = strsave(argv[1]);
319 shost->enable = atoi(argv[2]);
320 shost->override = atoi(argv[3]);
321 shost->success = atoi(argv[4]);
322 shost->inprogress = atoi(argv[5]);
323 shost->hosterror = atoi(argv[6]);
324 shost->errmsg = strsave(argv[7]);
325 shost->lasttry = atoi(argv[8]);
326 shost->lastsuccess = atoi(argv[9]);
327 shost->value1 = atoi(argv[10]);
328 shost->value2 = atoi(argv[11]);
329 shost->value3 = strsave(argv[12]);
334 /* Scans all of the hosts for a particular service, and processes them. */
339 char *argv[9], *machine;
341 struct save_queue *sq;
342 struct svrhost shost;
345 argv[0] = svc->service;
347 argv[2] = argv[3] = argv[4] = "DONTCARE";
349 status = sms_query("qualified_get_server_host", 6, argv, qgethost, sq);
350 if (status == SMS_NO_MATCH) {
353 com_err(whoami, status, " getting server_hosts for %s", svc->service);
356 while (sq_get_data(sq, &machine)) {
358 com_err(whoami, 0, "checking %s...", machine);
360 status = sms_query("get_server_host_info", 2, argv,gethostinfo, &shost);
362 com_err(whoami,status, " getting server_host_info for %s", machine);
365 if (!shost.enable || shost.hosterror ||
366 (shost.success && !shost.override &&
367 shost.lastsuccess >= svc->dfgen)) {
369 com_err(whoami, 0, "not updating %s:%s", svc->service, machine);
373 lock_fd = maybe_lock_update(machine, svc->service, 1);
376 argv[0] = svc->service;
378 argv[2] = argv[3] = argv[5] = "0";
380 argv[6] = strsave("");
381 argv[7] = itoa(tv.tv_sec);
382 argv[8] = itoa(shost.lastsuccess);
383 status = sms_query("set_server_host_internal", 9, argv,scream,NULL);
384 if (status != SMS_SUCCESS) {
385 com_err(whoami,status," while setting internal state for %s:%s",
386 svc->service, machine);
389 status = sms_update_server(svc->service, machine, svc->target,
391 if (status == SMS_SUCCESS) {
395 argv[8] = itoa(tv.tv_sec);
396 } else if (SOFT_FAIL(status)) {
398 argv[6] = strsave(error_message(status));
399 } else { /* HARD_FAIL */
400 argv[2] = itoa(shost.override);
401 argv[5] = itoa(status);
403 argv[6] = strsave(error_message(status));
404 critical_alert("DCM", "DCM updating %s:%s: %s",
405 machine, svc->service, argv[6]);
406 if (!strcmp(svc->type, "REPLICAT")) {
409 svc->harderror = status;
410 svc->errmsg = strsave(argv[6]);
411 qargv[0] = strsave(svc->service);
412 qargv[1] = itoa(svc->dfgen);
413 qargv[2] = itoa(svc->dfcheck);
414 qargv[3] = strsave("0");
415 qargv[4] = itoa(svc->harderror);
416 qargv[5] = strsave(svc->errmsg);
417 status = sms_query("set_server_internal_flags",
418 6, qargv, scream, NULL);
420 com_err(whoami, status,
421 " setting service state, sleeping");
422 sleep(DEADLOCK_WAIT);
423 status = sms_query("set_server_internal_flags",
424 6, qargv, scream, NULL);
426 com_err(whoami, status, " setting service state again");
438 status = sms_query("set_server_host_internal",
439 9, argv,scream,NULL);
441 com_err(whoami, status,
442 " setting host state, sleeping");
443 sleep(DEADLOCK_WAIT);
444 status = sms_query("set_server_host_internal",
445 9, argv,scream,NULL);
447 com_err(whoami, status, " setting host state again");
456 status = sms_query("set_server_host_internal", 9, argv,scream,NULL);
458 com_err(whoami, status, " setting host state, sleeping");
459 sleep(DEADLOCK_WAIT);
460 status = sms_query("set_server_host_internal", 9, argv,scream,NULL);
462 com_err(whoami, status, " setting host state again");