diff options
author | Neil Brown <neilb@suse.de> | 2007-03-20 09:50:33 +1100 |
---|---|---|
committer | Neil Brown <neilb@suse.de> | 2007-03-20 09:50:33 +1100 |
commit | 08ed16cab8a460ea56df19b7e27fd663cc96316d (patch) | |
tree | e496f3d3c7f4473c5fe05abd045c92b382a6fdf8 /utils/statd/sm-notify.c | |
parent | a6970289026e18fcb51f1305df96d868d94dae78 (diff) | |
download | nfs-utils-08ed16cab8a460ea56df19b7e27fd663cc96316d.tar.gz nfs-utils-08ed16cab8a460ea56df19b7e27fd663cc96316d.tar.xz nfs-utils-08ed16cab8a460ea56df19b7e27fd663cc96316d.zip |
Prevent sm-notify from being run multiple times per reboot.
As "mount.nfs" can start statd, and as statd can start sm-notify,
the risk of sm-notify being run multiple times increases.
As this is not normally appropriate, sm-notify now creates a
file in /var/run which will stop future instances from being
run (though ofcourse this behaviour can be controlled by a
new command line option).
Diffstat (limited to 'utils/statd/sm-notify.c')
-rw-r--r-- | utils/statd/sm-notify.c | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/utils/statd/sm-notify.c b/utils/statd/sm-notify.c index aa2c7d1..0090137 100644 --- a/utils/statd/sm-notify.c +++ b/utils/statd/sm-notify.c @@ -80,6 +80,7 @@ static int addr_get_port(nsm_address *); static void addr_set_port(nsm_address *, int); static int host_lookup(int, const char *, nsm_address *); void nsm_log(int fac, const char *fmt, ...); +static int record_pid(); static struct nsm_host * hosts = NULL; @@ -87,9 +88,13 @@ int main(int argc, char **argv) { int c; + int force = 0; - while ((c = getopt(argc, argv, "dm:np:v:qP:")) != -1) { + while ((c = getopt(argc, argv, "dm:np:v:qP:f")) != -1) { switch (c) { + case 'f': + force = 1; + break; case 'd': opt_debug++; break; @@ -131,10 +136,18 @@ main(int argc, char **argv) } if (optind < argc) { -usage: fprintf(stderr, "sm-notify [-d]\n"); +usage: fprintf(stderr, + "Usage: sm-notify [-dfq] [-m max-retry-minutes] [-p srcport]\n" + " [-P /path/to/state/directory] [-N my_host_name\n"); return 1; } + if (strcmp(_SM_BASE_PATH, BASEDIR) == 0) { + if (record_pid() == 0 && force == 0 && opt_update_state == 0) + /* already run, don't try again */ + exit(0); + } + if (opt_srcaddr) { strncpy(nsm_hostname, opt_srcaddr, sizeof(nsm_hostname)-1); } else @@ -678,3 +691,23 @@ nsm_log(int fac, const char *fmt, ...) } va_end(ap); } + +/* + * Record pid in /var/run/sm-notify.pid + * This file should remain until a reboot, even if the + * program exits. + * If file already exists, fail. + */ +static int record_pid() +{ + char pid[20]; + int fd; + + snprintf(pid, 20, "%d\n", getpid()); + fd = open("/var/run/sm-notify.pid", O_CREAT|O_EXCL|O_WRONLY, 0600); + if (!fd) + return 0; + write(fd, pid, strlen(pid)); + close(fd); + return 1; +} |