Annotation of embedaddon/bmon/src/signal.c, revision 1.1
1.1 ! misho 1: /*
! 2: * signal.c Signal Handling
! 3: *
! 4: * Copyright (c) 2001-2005 Thomas Graf <tgraf@suug.ch>
! 5: *
! 6: * Permission is hereby granted, free of charge, to any person obtaining a
! 7: * copy of this software and associated documentation files (the "Software"),
! 8: * to deal in the Software without restriction, including without limitation
! 9: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
! 10: * and/or sell copies of the Software, and to permit persons to whom the
! 11: * Software is furnished to do so, subject to the following conditions:
! 12: *
! 13: * The above copyright notice and this permission notice shall be included
! 14: * in all copies or substantial portions of the Software.
! 15: *
! 16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
! 17: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! 18: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
! 19: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! 20: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
! 21: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
! 22: * DEALINGS IN THE SOFTWARE.
! 23: */
! 24:
! 25: #include <bmon/bmon.h>
! 26: #include <bmon/conf.h>
! 27: #include <bmon/utils.h>
! 28:
! 29: static int sig_state = 0;
! 30:
! 31: static RETSIGTYPE sig_usr1(int unused)
! 32: {
! 33: sig_state = 1;
! 34: signal(SIGUSR1, sig_usr1);
! 35: }
! 36:
! 37: int is_signal_recvd(void)
! 38: {
! 39: int ret = sig_state;
! 40: sig_state = 0;
! 41: return ret;
! 42: }
! 43:
! 44: #if defined PRAGMA_FALLBACK
! 45: #pragma init (init_signal)
! 46: #endif
! 47:
! 48: static void __init init_signal(void)
! 49: {
! 50: if (signal(SIGUSR1, sig_usr1) == SIG_ERR) {
! 51: perror("signal(SIGUSR1) failed");
! 52: exit(1);
! 53: }
! 54: }
! 55:
! 56: void send_signal(const char *arg)
! 57: {
! 58: if (*arg != '-') {
! 59: long pid = strtol(arg, NULL, 10);
! 60:
! 61: if (pid != LONG_MIN && pid != LONG_MAX) {
! 62: if (kill((int) pid, SIGUSR1) < 0)
! 63: quit("kill(%d, SIGUSR1) failed: %s\n",
! 64: (int) pid, strerror(errno));
! 65: } else
! 66: quit("Invalid pid.\n");
! 67: } else {
! 68: FILE *f;
! 69: char buf[256], hdr[256], bmon_procs[10][256];
! 70: int cnt = 0, pid_to_kill = -1;
! 71: int my_uid = getuid();
! 72: char cmd[64];
! 73:
! 74: memset(cmd, 0, sizeof(cmd));
! 75: snprintf(cmd, sizeof(cmd)-1, "ps -U %d -o pid,uid,args", my_uid);
! 76:
! 77: if (!(f = popen(cmd, "r"))) {
! 78: fprintf(stderr, "popen(%s: %s\n", cmd, strerror(errno));
! 79: quit("Your ps is probably not POSIX compatible, use kill\n");
! 80: }
! 81:
! 82: fgets(hdr, sizeof(hdr), f);
! 83:
! 84: while(fgets(buf, sizeof(buf), f)) {
! 85: int pid, uid, n;
! 86: char args[256], cmd[256];
! 87:
! 88: if (!strstr(buf, "bmon") || !strstr(buf, "-w") || strstr(buf, "-S"))
! 89: continue;
! 90:
! 91: n = sscanf(buf, "%d %d %s %[^\n\r]s", &pid, &uid, cmd, args);
! 92:
! 93: if (n != 4)
! 94: continue;
! 95:
! 96: if (uid == my_uid) {
! 97: if (cnt <= 9) {
! 98: snprintf(bmon_procs[cnt], sizeof(bmon_procs[cnt])-1,
! 99: "%5d %5d %s %s", pid, uid, cmd, args);
! 100: pid_to_kill = pid;
! 101: cnt++;
! 102: } else
! 103: break;
! 104: }
! 105: }
! 106:
! 107: pclose(f);
! 108:
! 109: if (cnt == 1) {
! 110: if (pid_to_kill >= 0)
! 111: if (kill((int) pid_to_kill, SIGUSR1) < 0)
! 112: quit("kill(%d, SIGUSR1) failed: %s\n",
! 113: (int) pid_to_kill, strerror(errno));
! 114: } else {
! 115: int n;
! 116:
! 117: fprintf(stderr, "%d instances of bmon are running, " \
! 118: "use bmon -S <pid>\n", cnt);
! 119:
! 120: printf("%s", hdr);
! 121: for (n=0; n < cnt; n++)
! 122: printf("%s\n", bmon_procs[n]);
! 123: }
! 124: }
! 125: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>