check_script.c revision 1.2
1/* $OpenBSD: check_script.c,v 1.2 2007/06/12 15:16:10 msf Exp $ */ 2 3/* 4 * Copyright (c) 2007 Reyk Floeter <reyk@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19#include <sys/types.h> 20#include <sys/queue.h> 21#include <sys/socket.h> 22#include <sys/param.h> 23#include <sys/types.h> 24#include <sys/wait.h> 25 26#include <net/if.h> 27 28#include <limits.h> 29#include <event.h> 30#include <errno.h> 31#include <unistd.h> 32#include <string.h> 33#include <stdlib.h> 34#include <signal.h> 35#include <pwd.h> 36#include <err.h> 37 38#include <openssl/ssl.h> 39 40#include "hoststated.h" 41 42void script_sig_alarm(int); 43 44extern struct imsgbuf *ibuf_main; 45pid_t child = -1; 46 47void 48check_script(struct host *host) 49{ 50 struct ctl_script scr; 51 52 host->last_up = host->up; 53 host->flags &= ~(F_CHECK_SENT|F_CHECK_DONE); 54 55 scr.host = host->conf.id; 56 imsg_compose(ibuf_main, IMSG_SCRIPT, 0, 0, -1, &scr, sizeof(scr)); 57} 58 59void 60script_done(struct hoststated *env, struct ctl_script *scr) 61{ 62 struct host *host; 63 64 if ((host = host_find(env, scr->host)) == NULL) 65 fatalx("hce_dispatch_parent: invalid host id"); 66 67 if (scr->retval < 0) 68 host->up = HOST_UNKNOWN; 69 else if (scr->retval == 0) 70 host->up = HOST_DOWN; 71 else 72 host->up = HOST_UP; 73 host->flags |= F_CHECK_DONE; 74 75 hce_notify_done(host, host->up == HOST_UP ? 76 "script: done" : "script: failed"); 77} 78 79void 80script_sig_alarm(int sig) 81{ 82 if (child != -1) 83 kill(child, SIGKILL); 84} 85 86int 87script_exec(struct hoststated *env, struct ctl_script *scr) 88{ 89 int status = 0, ret = 0; 90 sig_t save_quit, save_int, save_chld; 91 struct itimerval it; 92 struct timeval *tv; 93 const char *file, *arg; 94 struct host *host; 95 struct table *table; 96 struct passwd *pw; 97 98 if ((host = host_find(env, scr->host)) == NULL) 99 fatalx("script_exec: invalid host id"); 100 if ((table = table_find(env, host->conf.tableid)) == NULL) 101 fatalx("script_exec: invalid table id"); 102 103 arg = host->conf.name; 104 file = table->conf.path; 105 tv = &table->conf.timeout; 106 107 save_quit = signal(SIGQUIT, SIG_IGN); 108 save_int = signal(SIGINT, SIG_IGN); 109 save_chld = signal(SIGCHLD, SIG_DFL); 110 111 switch (child = fork()) { 112 case -1: 113 ret = -1; 114 goto done; 115 case 0: 116 signal(SIGQUIT, SIG_DFL); 117 signal(SIGINT, SIG_DFL); 118 signal(SIGCHLD, SIG_DFL); 119 120 if ((pw = getpwnam(HOSTSTATED_USER)) == NULL) 121 fatal("script_exec: getpwnam"); 122 if (chdir("/") == -1) 123 fatal("script_exec: chdir(\"/\")"); 124 if (setgroups(1, &pw->pw_gid) || 125 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || 126 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) 127 fatal("script_exec: can't drop privileges"); 128 129 execlp(file, file, arg, (char *)NULL); 130 _exit(0); 131 break; 132 default: 133 /* Kill the process after a timeout */ 134 signal(SIGALRM, script_sig_alarm); 135 bzero(&it, sizeof(it)); 136 bcopy(tv, &it.it_value, sizeof(it.it_value)); 137 setitimer(ITIMER_REAL, &it, NULL); 138 139 waitpid(child, &status, 0); 140 break; 141 } 142 143 switch (ret) { 144 case -1: 145 ret = -1; 146 break; 147 default: 148 if (WIFEXITED(status)) 149 ret = WEXITSTATUS(status); 150 else 151 ret = -1; 152 } 153 154 done: 155 /* Disable the process timeout timer */ 156 bzero(&it, sizeof(it)); 157 setitimer(ITIMER_REAL, &it, NULL); 158 child = -1; 159 160 signal(SIGQUIT, save_quit); 161 signal(SIGINT, save_int); 162 signal(SIGCHLD, save_chld); 163 signal(SIGALRM, SIG_DFL); 164 165 return (ret); 166} 167