static int check_pid()

in src/native/unix/native/jsvc-unix.c [566:613]


static int check_pid(arg_data *args)
{
    int fd;
    FILE *pidf;
    char buff[80];
    pid_t pidn = getpid();
    int i, pid;
    int once = 0;

    /* skip writing the pid file if version or check */
    if (args->vers || args->chck) {
        return 0;
    }

retry:
    fd = open(args->pidf, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    if (fd < 0) {
        if (once == 0 && (errno == ENOTDIR || errno == ENOENT)) {
            once = 1;
            if (mkdir2(args->pidf, S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH) == 0)
                goto retry;
        }
        log_error("Cannot open PID file %s, PID is %d", args->pidf, pidn);
        return -1;
    }
    else {
        lockf(fd, F_LOCK, 0);
        i = read(fd, buff, sizeof(buff));
        if (i > 0) {
            buff[i] = '\0';
            pid = atoi(buff);
            if (kill(pid, 0) == 0) {
                log_error("Still running according to PID file %s, PID is %d", args->pidf, pid);
                lockf(fd, F_ULOCK, 0);
                close(fd);
                return 122;
            }
        }
        lseek(fd, SEEK_SET, 0);
        pidf = fdopen(fd, "r+");
        fprintf(pidf, "%d\n", (int)getpid());
        fflush(pidf);
        fclose(pidf);
        lockf(fd, F_ULOCK, 0);
        close(fd);
    }
    return 0;
}