1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/*
* Copyright (c) 2001-2003 The Trustees of Indiana University.
* All rights reserved.
* Copyright (c) 1998-2001 University of Notre Dame.
* All rights reserved.
* Copyright (c) 1994-1998 The Ohio State University.
* All rights reserved.
*
* This file is part of the LAM/MPI software package. For license
* information, see the LICENSE file in the top level directory of the
* LAM/MPI source distribution.
*
* $HEADER$
*
* $Id: tkill.c,v 6.33 2003/04/09 15:26:28 brbarret Exp $
*
* Function: - kills OTB environment
* - reads process IDs from the kill file, aka
* reset file, aka lock file for historical reasons
* - makes a very good attempt at ensuring the
* death of each pid in the file
*/
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <syslog.h>
#include <all_opt.h>
#include <args.h>
#include <lamdebug.h>
#include <terror.h>
#include <laminternal.h>
#include <etc_misc.h>
#define TIMEOUT 10000000 /* timeout on process death */
/*
* global variables
*/
int fl_debug; /* debugging option */
int fl_verbose; /* debugging option */
int
main(int argc, char *argv[])
{
FILE *fp_kill; /* kill file ptr */
int fd_kill; /* kill file desc. */
int fl_pretend; /* pretend option */
int n; /* favourite counter */
int pid; /* lots of space to hold a PID */
unsigned int usec; /* microseconds to sleep */
char *f_kill; /* kill file */
char *f_sock; /* socket file */
char *f_iosock; /* io daemon socket file */
char extra[1024]; /* place for ASCII messages */
char *prefix = NULL;
char *suffix = NULL;
OPT *ad = NULL;
int ret;
int debug;
lam_debug_stream_info_t debug_lds;
char *debug_loc;
/*
* Initialize option parser.
*/
ad = ao_init();
if (ad == 0) {
ret = errno;
perror("hboot (ao_init)");
exit(ret);
}
ao_setflags(ad, AOPRESERVE_ARGV);
ao_setopt1(ad, "dhvN", 0, 0, 0);
ao_setopt(ad, "sessionprefix", 0, 1, 0);
ao_setopt(ad, "sessionsuffix", 0, 1, 0);
ao_setopt(ad, "setsid", 0, 0, 0);
ao_setopt(ad, "f", 0, 1, 0);
ao_setopt(ad, "debug-output", 0, 1, 0);
if (ao_parse(ad, &argc, argv)) {
show_help("tkill", "usage", NULL);
exit(EUSAGE);
}
if (ao_taken(ad, "setsid"))
setsid();
/* Root can only run this program if "-N" is supplied (i.e., if it
was launched from recon */
fl_pretend = ao_taken(ad, "N");
#ifndef LAM_ALLOW_RUN_AS_ROOT
if (!fl_pretend && (getuid() == 0 || geteuid() == 0)) {
show_help(NULL, "deny-root", NULL);
exit(EACCES);
}
#endif
if (ao_taken(ad, "h")) {
show_help("tkill", "usage", NULL);
ao_free(ad);
exit(0);
}
/*
* debugging option parsing code
*/
fl_debug = ao_taken(ad, "d");
fl_verbose = ao_taken(ad, "v") || fl_debug;
debug_lds.lds_fl_debug = 0;
debug_lds.lds_fl_syslog = 0;
debug_lds.lds_fl_stdout = 0;
debug_lds.lds_fl_stderr = 0;
debug_lds.lds_fl_file = 0;
debug_lds.lds_prefix = "tkill: ";
if (ao_taken(ad, "debug-output")) {
debug_loc = ao_param(ad, "debug-output", 0, 0);
if (!strcmp(debug_loc, "stdout")) {
debug_lds.lds_fl_stdout = 1;
} else if (!strcmp(debug_loc, "syslog")) {
debug_lds.lds_fl_syslog = 1;
debug_lds.lds_syslog_priority = LOG_INFO;
debug_lds.lds_syslog_ident = argv[0];
} else {
show_help("tkill", "usage", NULL);
exit(EUSAGE);
}
} else {
debug_lds.lds_fl_stdout = 1;
}
debug = lam_debug_open(&debug_lds);
/* turn-off at init appears "not good" */
lam_debug_switch(debug, fl_debug);
/*
* Batch system gorp
*/
if (ao_taken(ad, "sessionprefix")) {
prefix = ao_param(ad, "sessionprefix", 0, 0);
} else {
prefix = NULL;
}
if (ao_taken(ad, "sessionsuffix")) {
suffix = ao_param(ad, "sessionsuffix", 0, 0);
} else {
suffix = NULL;
}
lam_debug(debug, "setting prefix to %s",
(prefix == NULL) ? "(null)" : prefix);
lam_debug(debug, "setting suffix to %s",
(suffix == NULL) ? "(null)" : suffix);
lam_tmpdir_init(prefix, suffix);
f_kill = lam_get_killfile();
lam_debug(debug, "got killname back: %s\n", f_kill);
free(f_kill);
/*
* Get the kill filename.
*/
if (ao_taken(ad, "f")) {
f_kill = ao_param(ad, "f", 0, 0);
} else {
f_kill = lam_get_killfile();
if (f_kill == 0) {
show_help(NULL, "lib-call-fail", "killname", NULL);
exit(errno);
}
}
/*
* Dump debugging information.
*/
lam_debug(debug, "f_kill = \"%s\"\n", f_kill);
/*
* Try to open the file.
*/
if ((fd_kill = open(f_kill, O_RDWR, 0)) < 0) {
if (errno == ENOENT) {
lam_tmpdir_remove();
lam_debug(debug, "nothing to kill: \"%s\"\n", f_kill);
lam_debug_close(debug);
if (!ao_taken(ad, "f"))
free(f_kill);
ao_free(ad);
exit(0);
} else {
snprintf(extra, 1024, "open(\"%s\", O_RDWR)", f_kill);
show_help(NULL, "system-call-fail", extra, NULL);
exit(errno);
}
}
fp_kill = fdopen(fd_kill, "r");
if (! fp_kill) {
snprintf(extra, 1024, "fdopen(\"%s\", \"r\")", f_kill);
show_help(NULL, "lib-call-fail", extra, NULL);
exit(errno);
}
lam_debug(debug, "killing LAM...\n");
n = fscanf(fp_kill, "%d", &pid);
while (n > 0) {
lam_debug(debug, "killing PID (SIGHUP) %d ...", pid);
/*
* Send SIGHUP to the process.
*/
if (fl_pretend) {
lam_debug(debug, "\n");
n = fscanf(fp_kill, "%d", &pid);
continue;
}
if (kill(pid, SIGHUP)) {
if (errno == ESRCH) {
lam_debug(debug, " already dead");
} else if (errno == EPERM) {
/*
* So if something happens to a process and its
* pid ends up being reused and we try to kill it
* and it's someone else's process, tkill used to
* get annoyed and complain on stderr. The
* problem is that lamboot takes that to mean
* something really bad happens and aborts the
* lamboot. Since that seems silly, we are
* instead going to print a warning and continue
* on.
*/
lam_debug(debug, " permission denied (ignoring pid)");
} else {
lam_debug(debug, "\n");
perror("tkill (kill)");
exit(errno);
}
} else {
usec = 2;
while ((kill(pid, SIGHUP) == 0) && (usec <= TIMEOUT)) {
microsleep(usec);
usec *= 2;
}
if (usec > TIMEOUT) {
lam_debug(debug, "trying -9 ...");
kill(pid, SIGKILL);
microsleep(500000);
if (kill(pid, SIGHUP)) {
lam_debug(debug, "killed");
} else {
lam_debug(debug, "cannot kill");
snprintf(extra, 1024, "%d", pid);
show_help("tkill", "cannot-kill", extra, NULL);
}
} else {
lam_debug(debug, "killed");
}
}
n = fscanf(fp_kill, "%d", &pid);
}
if (fclose(fp_kill)) {
snprintf(extra, 1024, "fclose(for file \"%s\")", f_kill);
show_help(NULL, "lib-call-fail", extra, NULL);
exit(errno);
}
if (fl_pretend) {
exit(0);
}
/*
* Cleanup all registered objects.
*/
lam_cleanup_objects();
/*
* Remove the socket file.
*/
lam_debug(debug, "removing socket file ...\n");
f_sock = lam_get_sockname();
if (f_sock == 0) {
show_help("lam-temp-files", "badness", "tkill/sockname", NULL);
exit(errno);
}
if (!fl_pretend) {
lam_debug(debug, "socket file: %sd\n", f_sock);
if (unlink(f_sock)) {
if (errno != ENOENT) {
snprintf(extra, 1024, "unlink(\"%s\")", f_sock);
show_help(NULL, "system-call-fail", extra, NULL);
}
}
}
/*
* Remove the IO daemon socket file.
*/
lam_debug(debug, "removing IO daemon socket file ...\n");
f_iosock = lam_get_iosockname();
if (f_iosock == 0) {
show_help("lam-temp-files", "badness", "tkill/iosockname", NULL);
exit(errno);
}
if (!fl_pretend) {
lam_debug(debug, "IO daemon socket file: %s\n", f_iosock);
if (unlink(f_iosock)) {
if (errno != ENOENT) {
snprintf(extra, 1024, "unlink(\"%s\")", f_iosock);
show_help(NULL, "system-call-fail", extra, NULL);
}
}
}
/*
* Remove the session directory. Go remove all entries in it first.
*/
lam_tmpdir_remove();
if (fl_debug) {
lam_debug(debug, "all finished\n");
fflush(stdout);
lam_debug_close(debug);
}
if (!ao_taken(ad, "f"))
free(f_kill);
free(f_sock);
free(f_iosock);
ao_free(ad);
return(0);
}
|