netplug / lib.c

/*
 * lib.c - random library routines
 *
 * Copyright 2003 Key Research, Inc.
 * Copyright 2003 Jeremy Fitzhardinge
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.  You are
 * forbidden from redistributing or modifying it under the terms of
 * any other license, including other versions of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <syslog.h>
#include <unistd.h>
#include <assert.h>

#include "netplug.h"


void
do_log(int pri, const char *fmt, ...)
{
    extern int use_syslog;
    va_list ap;
    va_start(ap, fmt);

    if (pri == LOG_DEBUG && !debug)
        return;

    if (use_syslog) {
        vsyslog(pri, fmt, ap);
    } else {
        FILE *fp;

        switch (pri) {
        case LOG_INFO:
        case LOG_NOTICE:
        case LOG_DEBUG:
            fp = stdout;
            break;
        default:
            fp = stderr;
            break;
        }

        switch (pri) {
        case LOG_WARNING:
            fputs("Warning: ", fp);
            break;
        case LOG_NOTICE:
            fputs("Notice: ", fp);
            break;
        case LOG_ERR:
            fputs("Error: ", fp);
            break;
        case LOG_INFO:
        case LOG_DEBUG:
            break;
        default:
            fprintf(fp, "Log type %d: ", pri);
            break;
        }

        vfprintf(fp, fmt, ap);
        fputc('\n', fp);
    }

    va_end(ap);
}


pid_t
run_netplug_bg(char *ifname, char *action)
{
    pid_t pid;

    if ((pid = fork()) == -1) {
        do_log(LOG_ERR, "fork: %m");
        exit(1);
    }
    else if (pid != 0) {
        return pid;
    }

    setpgrp();                  /* become group leader */

    do_log(LOG_INFO, "%s %s %s -> %d", NP_SCRIPT, ifname, action, getpid());

    execl(NP_SCRIPT, NP_SCRIPT, ifname, action, NULL);

    do_log(LOG_ERR, NP_SCRIPT ": %m");
    exit(1);
}


int
run_netplug(char *ifname, char *action)
{
    pid_t pid = run_netplug_bg(ifname, action);
    int status, ret;

    if ((ret = waitpid(pid, &status, 0)) == -1) {
        do_log(LOG_ERR, "waitpid: %m");
        exit(1);
    }

    return WIFEXITED(status) ? WEXITSTATUS(status) : -WTERMSIG(status);
}

/*
   Synchronously kill a script

   Assumes the pid is actually a leader of a group.  Kills first with
   SIGTERM at first; if that doesn't work, follow up with a SIGKILL.
 */
void
kill_script(pid_t pid)
{
    pid_t ret;
    int status;
    sigset_t mask, origmask;

    if (pid == -1)
        return;

    assert(pid > 0);

    /* Block SIGCHLD while we go around killing things, so the SIGCHLD
       handler doesn't steal things behind our back. */
    sigemptyset(&mask);
    sigaddset(&mask, SIGCHLD);
    sigprocmask(SIG_BLOCK, &mask, &origmask);

    /* ask nicely */
    if (killpg(pid, SIGTERM) == -1) {
        do_log(LOG_ERR, "Can't kill script pgrp %d: %m", pid);
        goto done;
    }

    sleep(1);

    ret = waitpid(pid, &status, WNOHANG);

    if (ret == -1) {
        do_log(LOG_ERR, "Failed to wait for %d: %m?!", pid);
        goto done;
    } else if (ret == 0) {
        /* no more Mr. nice guy */
        if (killpg(pid, SIGKILL) == -1) {
            do_log(LOG_ERR, "2nd kill %d failed: %m?!", pid);
            goto done;
        }
        ret = waitpid(pid, &status, 0);
    }

    assert(ret == pid);

 done:
    sigprocmask(SIG_SETMASK, &origmask, NULL);
}

void *
xmalloc(size_t n)
{
    void *x = malloc(n);

    if (n > 0 && x == NULL) {
        do_log(LOG_ERR, "malloc: %m");
        exit(1);
    }

    return x;
}


/*
 * Local variables:
 * c-file-style: "stroustrup"
 * End:
 */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.