linux-hotplug.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [util-linux] flock utility program?
@ 2004-11-08 12:11 Adam J. Richter
  2004-11-08 12:20 ` Marco d'Itri
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Adam J. Richter @ 2004-11-08 12:11 UTC (permalink / raw)
  To: linux-hotplug

Hi Andries,

	With facilities like hotplug invoking shell scripts in
relatively asynchronous ways, I think there is increased need
for an flock shell utility.

	Also, I think in many cases it is advantageous to externalize
the locking policy when they can, making them more adaptable to a
wider variety of system configuration policies.  For example, if
I want to serialize hotplug events, then I'm probably not going to
do any additional locking.  On the other hand, if I have a system
to coalesces hotplug events, I'm probably going to have a locking
system that allows initialization of my USB hard disk to run while
initialization of my printer is also running.

	Is there some existing utility to do this?  I didn't get
any response when I asked about it on kernelnewbies.org
(on #offtopic and #kernelnewbies).

	So, if there is not standard utility for shell scripts to
do flock, here is a simple utility that allows one to do
"flock [--shared | --non-blocking] cmd arg arg...".  I would like to get
it into util-linux.  I would be happy to write a manual page
for it. 

	I want to post some user level software for devfs (which is
also potentially useful for sysfs) that would use this command in
one of its helper shell scripts, and I would really prefer not to
bundle it into software specific to devfs.  So, if I could get into
util-linux or some other set of general purpose shell utilities and
point to that, I think would allow such a utility to be more easily
put to other uses that could benefit from it.

	Please let me know what you think.  I'll cc this to linux-hotplug,
because it's potentially relevant to hotplug scripts, among other things.

                    __     ______________ 
Adam J. Richter        \ /
adam@yggdrasil.com      | g g d r a s i l



/*
    flock - acquires a file lock and executes a command with the lock held.
    Usage: flock {--shared | --non-blocking} lockfile {program and 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 <stdio.h>

static int non_blocking;
static int shared = LOCK_EX;

static const struct option options[] = {
	{ "non-blocking",	no_argument,	&non_blocking,	LOCK_NB },
	{ "shared",		no_argument,	&shared,	LOCK_SH },
	{ NULL,			0,		NULL,		0 },
};

int main(int argc, char **argv)
{
	int fd;
	int opt;
	int pid;
	int child_status;
	int option_index;

	do {
		opt = getopt_long(argc, argv, "+", options, &option_index);
		if (opt = '?') {
			fprintf (stderr, "AJR unknown option, aborting.\n");
			exit(1);
		}
	} while (opt != -1);

	argc -= optind;
	argv += optind;

	if (argc < 2) {
		fprintf(stderr,
			"Usage flock [--non-blocking | --shared] filename command {arg arg...}\n");
		exit(2);
	}

	fd = open(argv[0], O_RDONLY);
	if (fd < 0) {
		perror(argv[1]);
		exit(3);
	}

	if (flock(fd, shared | non_blocking) != 0) {
		perror("flock");
		exit(4);
	}
	pid = vfork();
	if (pid < 0) {
		perror("vfork");
		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);
}


-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_idU88&alloc_id\x12065&op=click
_______________________________________________
Linux-hotplug-devel mailing list  http://linux-hotplug.sourceforge.net
Linux-hotplug-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-hotplug-devel

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2004-11-16 15:33 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-11-08 12:11 [util-linux] flock utility program? Adam J. Richter
2004-11-08 12:20 ` Marco d'Itri
2004-11-08 14:36 ` Adam J. Richter
2004-11-08 14:48 ` Adam J. Richter
2004-11-15 17:21 ` Andries Brouwer
2004-11-16  3:32 ` Adam J. Richter
2004-11-16  7:34 ` Adam J. Richter
2004-11-16  7:38 ` Adam J. Richter
2004-11-16 15:24 ` Andries Brouwer
2004-11-16 15:32 ` Andries Brouwer
2004-11-16 15:33 ` Andries Brouwer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).