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$";
26 #include "mit-copyright.h"
29 extern char *getenv();
31 extern char *error_message();
37 /* declared global so that we can get the current time from different places. */
51 dbg = atoi(getenv("DEBUG"));
57 while(++arg - argv < argc) {
61 dbg = atoi((*arg)[2]? *arg+2: *++arg);
65 set_com_err_hook(dcm_com_err_hook);
67 /* if /etc/nodcm exists, punt quietly. */
68 if (!access("/etc/nodcm", F_OK)) {
72 if (status = sms_connect()) {
73 com_err(whoami, status, " on sms_connect");
74 leave("connect failed");
77 if (status = sms_auth("dcm")) {
78 com_err(whoami, status, " on \"authenticate\"");
82 /* if DCM is not enabled, exit after logging */
83 qargv[0] = "dcm_enable";
84 if (status = sms_query("get_value", 1, qargv, gqval, &i)) {
85 com_err(whoami, status, " check dcm_enable");
86 leave("query failed");
90 leave("dcm_enable not set");
100 /* Used by the get_value query when checking for dcm_enable. */
102 gqval(argc, argv, hint)
107 *hint = atoi(argv[0]);
112 /* Used by qualified_get_server to make a list of servers to check */
114 qgetsv(argc, argv, sq)
117 struct save_queue *sq;
119 sq_save_data(sq, strsave(argv[0]));
124 /* Used by get_server_info to record all of the returned information */
126 getsvinfo(argc, argv, sserv)
129 struct service *sserv;
131 sserv->service = strsave(argv[0]);
132 sserv->interval = atoi(argv[1]);
133 sserv->target = strsave(argv[2]);
134 sserv->script = strsave(argv[3]);
135 sserv->dfgen = atoi(argv[4]);
136 sserv->dfcheck = atoi(argv[5]);
137 sserv->type = strsave(argv[6]);
138 sserv->enable = atoi(argv[7]);
139 sserv->inprogress = atoi(argv[8]);
140 sserv->harderror = atoi(argv[9]);
141 sserv->errmsg = strsave(argv[10]);
146 /* Scan the services and process any that need it. */
151 struct save_queue *sq, *sq_create();
152 char *service, dfgen_prog[64], dfgen_cmd[128];
154 int status, lock_fd, ex;
159 if (dbg & DBG_VERBOSE)
160 com_err(whoami, 0, "starting pass over services");
163 qargv[1] = "dontcare";
166 if (status = sms_query("qualified_get_server", 3, qargv, qgetsv, sq)) {
167 com_err(whoami, status, " getting services");
168 leave("query failed");
170 while (sq_get_data(sq, &service)) {
171 for (p = service; *p; p++)
174 com_err(whoami, 0, "checking %s...", service);
176 sprintf(dfgen_prog, "%s/bin/%s.gen", SMS_DIR, service);
177 if (!file_exists(dfgen_prog)) {
178 com_err(whoami, 0, "prog %s doesn't exist\n", dfgen_prog);
182 sprintf(dfgen_cmd, "exec %s %s/dcm/%s.out",
183 dfgen_prog, SMS_DIR, service);
184 gettimeofday(&tv, &tz);
185 if (status = sms_query("get_server_info", 1, qargv, getsvinfo, &svc)) {
186 com_err(whoami, status, " getting service %s info", service);
188 svc.service = strsave(service);
189 qargv[0] = strsave(service);
190 qargv[1] = itoa(svc.dfgen);
191 qargv[2] = itoa(svc.dfcheck);
192 qargv[3] = strsave("0");
193 qargv[4] = itoa(svc.harderror);
194 qargv[5] = strsave(svc.errmsg);
195 if (svc.interval != 0) {
196 if (svc.interval * 60 + svc.dfcheck < tv.tv_sec) {
197 lock_fd = maybe_lock_update(SMS_DIR, "@db@", service, 1);
203 qargv[3] = strsave("1");
204 qargv[4] = strsave("0");
205 qargv[5] = strsave("");
206 status = sms_query("set_server_internal_flags", 6, qargv,
208 if (status != SMS_SUCCESS) {
209 com_err(whoami, status, " setting server state");
213 com_err(whoami, status, " running %s", dfgen_prog);
214 waits.w_status = system(dfgen_cmd);
215 /* extract the process's exit value */
216 status = waits.w_retcode;
218 status += sms_err_base;
219 com_err(whoami, status, " %s exited", dfgen_prog);
221 if (SOFT_FAIL(status)) {
223 qargv[5] = strsave(error_message(status));
224 } else if (status == SMS_NO_CHANGE) {
226 qargv[2] = itoa(tv.tv_sec);
227 svc.dfcheck = tv.tv_sec;
228 } else if (status == SMS_SUCCESS) {
231 qargv[1] = itoa(tv.tv_sec);
232 qargv[2] = strsave(qargv[1]);
233 svc.dfcheck = svc.dfgen = tv.tv_sec;
234 } else { /* HARD_FAIL(status) */
238 qargv[2] = itoa(tv.tv_sec);
239 svc.dfcheck = tv.tv_sec;
240 qargv[4] = itoa(status);
241 qargv[5] = strsave(error_message(status));
242 critical_alert("DCM","DCM building config files for %s: %s",
247 qargv[3] = strsave("0");
248 status = sms_query("set_server_internal_flags", 6, qargv,
258 if (!strcmp(svc.type, "REPLICAT"))
262 lock_fd = maybe_lock_update(SMS_DIR, "@db@", service, ex);
279 /* Used by qualified_get_server_host to make a list of hosts to check */
281 qgethost(argc, argv, sq)
284 struct save_queue *sq;
286 sq_save_data(sq, strsave(argv[1]));
291 /* Used by get_server_host_info to store all of the info about a host */
293 gethostinfo(argc, argv, shost)
296 struct svrhost *shost;
298 shost->service = strsave(argv[0]);
299 shost->machine = strsave(argv[1]);
300 shost->enable = atoi(argv[2]);
301 shost->override = atoi(argv[3]);
302 shost->success = atoi(argv[4]);
303 shost->inprogress = atoi(argv[5]);
304 shost->hosterror = atoi(argv[6]);
305 shost->errmsg = strsave(argv[7]);
306 shost->lasttry = atoi(argv[8]);
307 shost->lastsuccess = atoi(argv[9]);
308 shost->value1 = atoi(argv[10]);
309 shost->value2 = atoi(argv[11]);
310 shost->value3 = strsave(argv[12]);
315 /* Scans all of the hosts for a particular service, and processes them. */
320 char *argv[9], *machine;
322 struct save_queue *sq;
323 struct svrhost shost;
326 argv[0] = svc->service;
328 argv[2] = argv[3] = argv[4] = "DONTCARE";
330 status = sms_query("qualified_get_server_host", 6, argv, qgethost, sq);
331 if (status == SMS_NO_MATCH) {
334 com_err(whoami, status, " getting server_hosts for %s", svc->service);
337 while (sq_get_data(sq, &machine)) {
339 com_err(whoami, 0, "checking %s...", machine);
341 status = sms_query("get_server_host_info", 2, argv,gethostinfo, &shost);
343 com_err(whoami,status, " getting server_host_info for %s", machine);
346 if (!shost.enable || shost.hosterror ||
347 (shost.success && !shost.override &&
348 shost.lastsuccess > svc->dfgen)) {
350 com_err(whoami, 0, "not updating %s:%s", svc->service, machine);
353 if (!shost.success || shost.override ||
354 shost.lasttry + svc->interval < tv.tv_sec) {
355 lock_fd = maybe_lock_update(SMS_DIR, machine, svc->service, 1);
358 argv[0] = svc->service;
360 argv[2] = argv[3] = argv[5] = "0";
362 argv[6] = strsave("");
363 argv[7] = itoa(tv.tv_sec);
364 argv[8] = itoa(shost.lastsuccess);
365 status = sms_query("set_server_host_internal", 9, argv,scream,NULL);
366 if (status != SMS_SUCCESS) {
367 com_err(whoami,status," while setting internal state for %s:%s",
368 svc->service, machine);
371 status = sms_update_server(svc->service, machine, svc->target,
373 if (status == SMS_SUCCESS) {
377 argv[8] = itoa(tv.tv_sec);
378 } else if (SOFT_FAIL(status)) {
380 argv[6] = strsave(error_message(status));
381 } else { /* HARD_FAIL */
382 argv[2] = itoa(shost.override);
383 argv[5] = itoa(status);
385 argv[6] = strsave(error_message(status));
386 critical_alert("DCM", "DCM updating %s:%s: %s",
387 machine, svc->service, argv[6]);
388 if (!strcmp(svc->type, "REPLICAT")) {
391 svc->harderror = status;
392 svc->errmsg = strsave(argv[6]);
393 qargv[0] = strsave(svc->service);
394 qargv[1] = itoa(svc->dfgen);
395 qargv[2] = itoa(svc->dfcheck);
396 qargv[3] = strsave("0");
397 qargv[4] = itoa(svc->harderror);
398 qargv[5] = strsave(svc->errmsg);
399 status = sms_query("set_server_internal_flags",
400 6, qargv, scream, NULL);
411 status = sms_query("set_server_host_internal",
412 9, argv,scream,NULL);
420 status = sms_query("set_server_host_internal", 9, argv,scream,NULL);
423 com_err(whoami, 0, "not updating %s", machine);