#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "babysit.h"

int main(int argc, const char * const argv[]) {
    /* argv[1] is always set, so this is safe */
    const char *service_dir = argv[1];

    if (argc != 2) {
       fprintf(stderr, "Usage: %s [service_dir]\n", argv[0]);
       exit(100);
    }

    if (chdir(service_dir) != 0) {
        perror("chdir service_dir");
        exit(100);
    }

    if (open(CONTROL_FIFO, O_WRONLY|O_NONBLOCK) == -1) {
        /* According to Single Unix and fifo(4) on Linux, opening a fifo
         * with O_WRONLY|O_NONBLOCK fails with ENXIO if no one is
         * reading from the other end. So iff this open succeeds,
         * babysit is running. If it fails with something other than
         * ENXIO, there is something else hosed anyway, so assume it's
         * not running "properly" */
        exit(100);
    }
    exit(0);
}


syntax highlighted by Code2HTML, v. 0.9.1