diff options
author | Karel Zak | 2006-12-07 00:26:24 +0100 |
---|---|---|
committer | Karel Zak | 2006-12-07 00:26:24 +0100 |
commit | d162fcb550a77875c8f58fda0e0a0bd91f211c99 (patch) | |
tree | bd984959acd465b3058bd805f216d29dc0c3c1a6 /sys-utils | |
parent | Imported from util-linux-2.12i tarball. (diff) | |
download | kernel-qcow2-util-linux-d162fcb550a77875c8f58fda0e0a0bd91f211c99.tar.gz kernel-qcow2-util-linux-d162fcb550a77875c8f58fda0e0a0bd91f211c99.tar.xz kernel-qcow2-util-linux-d162fcb550a77875c8f58fda0e0a0bd91f211c99.zip |
Imported from util-linux-2.12j tarball.
Diffstat (limited to 'sys-utils')
-rw-r--r-- | sys-utils/Makefile | 4 | ||||
-rw-r--r-- | sys-utils/flock.1 | 45 | ||||
-rw-r--r-- | sys-utils/flock.c | 116 | ||||
-rw-r--r-- | sys-utils/readprofile.c | 4 |
4 files changed, 167 insertions, 2 deletions
diff --git a/sys-utils/Makefile b/sys-utils/Makefile index 00944781d..c61ed7772 100644 --- a/sys-utils/Makefile +++ b/sys-utils/Makefile @@ -8,7 +8,7 @@ include ../MCONFIG # Where to put man pages? -MAN1= arch.1 readprofile.1 +MAN1= arch.1 flock.1 readprofile.1 MAN8= ctrlaltdel.8 cytune.8 dmesg.8 \ ipcrm.8 ipcs.8 renice.8 \ @@ -19,7 +19,7 @@ MAN8= ctrlaltdel.8 cytune.8 dmesg.8 \ BIN= arch dmesg -USRBIN= cytune ipcrm ipcs renice setsid +USRBIN= cytune flock ipcrm ipcs renice setsid USRSBIN= readprofile tunelp diff --git a/sys-utils/flock.1 b/sys-utils/flock.1 new file mode 100644 index 000000000..dda0a0cfc --- /dev/null +++ b/sys-utils/flock.1 @@ -0,0 +1,45 @@ +.TH FLOCK "1" "November 2004" "flock (util-linux)" "User Commands" +.SH NAME +flock \- acquire a file lock and then execute a command with the lock held +.SH SYNOPSIS +.BR flock +[ \fB\-\-shared\fR | \fB\-\-timeout=\fR\fIseconds\fR ] lockfile command .. +.SH DESCRIPTION +.\" Add any additional description here +.PP +Acquire a file lock using the flock(2) system call and then execute +the given command with the lock held. Depending on the options given, +the lock can be either exclusive or shared, and the behavior in the +event of lock contention can be specified as either waiting +indefinitely for the lock to become available (the default), or +failing if the lock does not become available after a specific time, +which can be specified as zero to make the command not wait at all. +.PP +.TP +\fB\-\-shared\fR +Acquire a shared lock. Acquiring a shared lock does +not stop others from acquiring a shared lock, but it will stop others +from acquiring an exclusive lock. Conversely, acquiring an exclusive +lock (the default) stops both exclusive and shared attempts to acquire +the lock. Typically, a shared lock is used if a command is just going +to read the locked data, and an exclusive lock is used if the command +might write to it. +.TP +\fB\-\-timeout=n\fR +Abort if the lock cannot be acquired before \fIn\fR seconds. +For a completely non-blocking attempt to acquire a lock, specify +\fB\-\-timeout=0\fR. +The timer applies only to the attempt to acquire the lock. As soon +as the lock is acquired, the timeout is cancelled. The command to +be run is not subject to the timeout. +.PP +.SH "EXAMPLES (invoking some imaginary programs)" +.hl +.PP +flock /etc/passwd read-and-write-to-passwd +.PP +flock \-\-shared /etc/passwd just-read-something-from-passwd +.PP +flock \-\-timeout=0 /sys /usr/local/bin/update-hotplug /sys +.SH AUTHOR +Written by Adam J. Richter diff --git a/sys-utils/flock.c b/sys-utils/flock.c new file mode 100644 index 000000000..524d28a64 --- /dev/null +++ b/sys-utils/flock.c @@ -0,0 +1,116 @@ +/* + flock - acquires a file lock and executes a command with the lock held. + Usage: flock [--shared | --timeout=seconds] lockfile program [args...] + + Written by Adam J. Richter + Copyright (C) 2004 Yggdrasil Computing, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + 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. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +#include <sys/file.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <getopt.h> +#include <unistd.h> +#include <stdlib.h> /* exit */ +#include <signal.h> /* kill */ +#include <stdio.h> +#include "nls.h" + +static int non_blocking = 0; +static int shared = LOCK_EX; + +static const struct option options[] = { + {"shared", no_argument, &shared, LOCK_SH }, + {"timeout", required_argument, NULL, 't' }, + {NULL, 0, NULL, 0 }, +}; + +int main(int argc, char **argv) +{ + int fd; + int opt; + int pid; + int child_status; + int option_index; + int timeout = 0; + + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); + + do { + opt = getopt_long(argc, argv, "+", options, &option_index); + switch(opt) { + case '?': + fprintf (stderr, + _("flock: unknown option, aborting.\n")); + exit(1); + break; + case 't': + timeout = atoi(optarg); + if (timeout == 0) + non_blocking |= LOCK_NB; + break; + default: + break; + } + } while (opt != -1); + + argc -= optind; + argv += optind; + + if (argc < 2) { + fprintf(stderr, + _("Usage flock [--shared | --timeout=seconds] " + "filename command {arg arg...}\n")); + exit(2); + } + + fd = open(argv[0], O_RDONLY); + if (fd < 0) { + perror(argv[0]); + exit(3); + } + + alarm(timeout); + if (flock(fd, shared | non_blocking) != 0) { + perror("flock"); + exit(4); + } + alarm(0); + + pid = fork(); + if (pid < 0) { + perror("fork"); + exit(5); + } + if (pid == 0) { + execvp(argv[1], argv+1); + perror(argv[1]); + exit(6); + } + waitpid(pid, &child_status, 0); + + /* flock(fd, LOCK_UN); */ + /* No need to explicitly release the flock, since we are just + going to exit now anyhow. */ + + /* Lame attempt to simulate child's mode of death. */ + if (WIFSIGNALED(child_status)) + kill(0, WTERMSIG(child_status)); + + return WEXITSTATUS(child_status); +} diff --git a/sys-utils/readprofile.c b/sys-utils/readprofile.c index efb156587..61ba428cc 100644 --- a/sys-utils/readprofile.c +++ b/sys-utils/readprofile.c @@ -388,6 +388,10 @@ main(int argc, char **argv) { maplineno++; } + + /* clock ticks, out of kernel text - probably modules */ + printf("%6i %s\n", buf[len/sizeof(*buf)-1], "*unknown*"); + /* trailer */ if (optVerbose) printf("%016x %-40s %6i %8.4f\n", |