diff options
author | csjp <csjp@FreeBSD.org> | 2005-10-07 11:49:27 +0000 |
---|---|---|
committer | csjp <csjp@FreeBSD.org> | 2005-10-07 11:49:27 +0000 |
commit | c1fc62329932a8e2822ba1bb6e4193677b448f20 (patch) | |
tree | 81123f7bb762788e1ebafbe663a436c4da584402 /usr.bin | |
parent | a5d56eaf9fe2683eac471e46637802b871644aa2 (diff) | |
download | FreeBSD-src-c1fc62329932a8e2822ba1bb6e4193677b448f20.zip FreeBSD-src-c1fc62329932a8e2822ba1bb6e4193677b448f20.tar.gz |
Do not ignore ENOENT
Pointed out by: Amir Shalem
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/lockf/lockf.c | 156 |
1 files changed, 68 insertions, 88 deletions
diff --git a/usr.bin/lockf/lockf.c b/usr.bin/lockf/lockf.c index 69da1c9..7fb8791 100644 --- a/usr.bin/lockf/lockf.c +++ b/usr.bin/lockf/lockf.c @@ -55,94 +55,74 @@ static volatile sig_atomic_t timed_out; int main(int argc, char **argv) { - int ch; - int silent; - int status; - int waitsec; - pid_t child; - - silent = 0; - keep = 0; - waitsec = -1; /* Infinite. */ - while ((ch = getopt(argc, argv, "skt:")) != -1) { - switch (ch) { - - case 'k': - keep = 1; - break; - - case 's': - silent = 1; - break; - - case 't': - { - char *endptr; - waitsec = strtol(optarg, &endptr, 0); - if (*optarg == '\0' || *endptr != '\0' || waitsec < 0) - errx(EX_USAGE, "invalid timeout \"%s\"", optarg); - } - break; - - default: - usage(); + int ch, silent, status, waitsec; + pid_t child; + + silent = keep = 0; + waitsec = -1; /* Infinite. */ + while ((ch = getopt(argc, argv, "skt:")) != -1) { + switch (ch) { + case 'k': + keep = 1; + break; + case 's': + silent = 1; + break; + case 't': + { + char *endptr; + waitsec = strtol(optarg, &endptr, 0); + if (*optarg == '\0' || *endptr != '\0' || waitsec < 0) + errx(EX_USAGE, + "invalid timeout \"%s\"", optarg); + } + break; + default: + usage(); + } } - } - if (argc - optind < 2) - usage(); - lockname = argv[optind++]; - argc -= optind; - argv += optind; - - if (waitsec > 0) { /* Set up a timeout. */ - struct sigaction act; - - act.sa_handler = timeout; - sigemptyset(&act.sa_mask); - act.sa_flags = 0; /* Note that we do not set SA_RESTART. */ - - sigaction(SIGALRM, &act, NULL); - alarm(waitsec); - } - - lockfd = wait_for_lock(lockname, O_NONBLOCK); - while (lockfd == -1 && !timed_out && waitsec != 0) - lockfd = wait_for_lock(lockname, 0); - - if (waitsec > 0) - alarm(0); - - if (lockfd == -1) { /* We failed to acquire the lock. */ - if (silent) - exit(EX_TEMPFAIL); - errx(EX_TEMPFAIL, "%s: already locked", lockname); - } - - /* At this point, we own the lock. */ - - if (atexit(cleanup) == -1) - err(EX_OSERR, "atexit failed"); - - if ((child = fork()) == -1) - err(EX_OSERR, "cannot fork"); - - if (child == 0) { /* The child process. */ - close(lockfd); - execvp(argv[0], argv); - warn("%s", argv[0]); - _exit(1); - } - - /* This is the parent process. */ - - signal(SIGINT, SIG_IGN); - signal(SIGQUIT, SIG_IGN); - signal(SIGTERM, killed); - - if (waitpid(child, &status, 0) == -1) - err(EX_OSERR, "waitpid failed"); - - return WIFEXITED(status) ? WEXITSTATUS(status) : 1; + if (argc - optind < 2) + usage(); + lockname = argv[optind++]; + argc -= optind; + argv += optind; + if (waitsec > 0) { /* Set up a timeout. */ + struct sigaction act; + + act.sa_handler = timeout; + sigemptyset(&act.sa_mask); + act.sa_flags = 0; /* Note that we do not set SA_RESTART. */ + sigaction(SIGALRM, &act, NULL); + alarm(waitsec); + } + lockfd = wait_for_lock(lockname, O_NONBLOCK); + while (lockfd == -1 && !timed_out && waitsec != 0) + lockfd = wait_for_lock(lockname, 0); + if (waitsec > 0) + alarm(0); + if (lockfd == -1) { /* We failed to acquire the lock. */ + if (silent) + exit(EX_TEMPFAIL); + errx(EX_TEMPFAIL, "%s: already locked", lockname); + } + /* At this point, we own the lock. */ + if (atexit(cleanup) == -1) + err(EX_OSERR, "atexit failed"); + if ((child = fork()) == -1) + err(EX_OSERR, "cannot fork"); + if (child == 0) { /* The child process. */ + close(lockfd); + execvp(argv[0], argv); + warn("%s", argv[0]); + _exit(1); + } + /* This is the parent process. */ + signal(SIGINT, SIG_IGN); + signal(SIGQUIT, SIG_IGN); + signal(SIGTERM, killed); + if (waitpid(child, &status, 0) == -1) + err(EX_OSERR, "waitpid failed"); + return (WIFEXITED(status) ? WEXITSTATUS(status) : 1); } /* @@ -196,7 +176,7 @@ wait_for_lock(const char *name, int flags) int fd; if ((fd = open(name, O_CREAT|O_RDONLY|O_EXLOCK|flags, 0666)) == -1) { - if (errno == ENOENT || errno == EINTR || errno == EAGAIN) + if (errno == EINTR || errno == EAGAIN) return (-1); err(EX_CANTCREAT, "cannot open %s", name); } |