Status: ok Pid file support From: Luca Berra Adds a -i (pid) option to monitor mode which causes mdadm to write its own pid to the specified file, this comes useful to simplify init scripts that call mdadm --monitor --daemonize. Signed-off-by: Neil Brown ### Diffstat output ./ChangeLog | 1 + ./Monitor.c | 17 +++++++++++++++-- ./ReadMe.c | 4 +++- ./mdadm.8 | 7 +++++++ ./mdadm.c | 15 ++++++++++++++- ./mdadm.h | 2 +- 6 files changed, 41 insertions(+), 5 deletions(-) diff ./ChangeLog~current~ ./ChangeLog --- ./ChangeLog~current~ 2004-11-02 14:41:38.000000000 +1100 +++ ./ChangeLog 2004-11-02 14:41:38.000000000 +1100 @@ -1,5 +1,6 @@ Changes Prior to this release - Makefile cleanup from Luca Berra + - --pid-file (-i) to set a pid file to use with --monitor --daemonise Changes Prior to 1.7.0 release - Support "--grow --add" to add a device to a linear array, if the diff ./Monitor.c~current~ ./Monitor.c --- ./Monitor.c~current~ 2004-11-02 14:41:38.000000000 +1100 +++ ./Monitor.c 2004-11-02 14:41:38.000000000 +1100 @@ -47,7 +47,7 @@ static char *percentalerts[] = { int Monitor(mddev_dev_t devlist, char *mailaddr, char *alert_cmd, int period, int daemonise, int scan, int oneshot, - char *config, int test) + char *config, int test, char* pidfile) { /* * Every few seconds, scan every md device looking for changes @@ -127,7 +127,18 @@ int Monitor(mddev_dev_t devlist, if (daemonise) { int pid = fork(); if (pid > 0) { - printf("%d\n", pid); + if (!pidfile) + printf("%d\n", pid); + else { + FILE *pid_file; + pid_file=fopen(pidfile, "w"); + if (!pid_file) + perror("cannot create pid file"); + else { + fprintf(pid_file,"%d\n", pid); + fclose(pid_file); + } + } return 0; } if (pid < 0) { @@ -428,6 +439,8 @@ int Monitor(mddev_dev_t devlist, } test = 0; } + if (pidfile) + unlink(pidfile); return 0; } diff ./ReadMe.c~current~ ./ReadMe.c --- ./ReadMe.c~current~ 2004-11-02 14:41:38.000000000 +1100 +++ ./ReadMe.c 2004-11-02 14:41:38.000000000 +1100 @@ -90,7 +90,7 @@ char Version[] = Name " - v1.7.0 - 11 Au * At the time if writing, there is only minimal support. */ -char short_options[]="-ABCDEFGQhVvbc:l:p:m:n:x:u:c:d:z:U:sa::rfRSow1t"; +char short_options[]="-ABCDEFGQhVvbc:i:l:p:m:n:x:u:c:d:z:U:sa::rfRSow1t"; struct option long_options[] = { {"manage", 0, 0, '@'}, {"misc", 0, 0, '#'}, @@ -157,6 +157,7 @@ struct option long_options[] = { {"daemonise", 0, 0, 'f'}, {"daemonize", 0, 0, 'f'}, {"oneshot", 0, 0, '1'}, + {"pid-file", 1, 0, 'i'}, {0, 0, 0, 0} }; @@ -418,6 +419,7 @@ char Help_monitor[] = " --config= -c : specify a different config file\n" " --scan -s : find mail-address/program in config file\n" " --daemonise -f : Fork and continue in child, parent exits\n" +" --pid-file= -i : In daemon mode write pid to specified file instead of stdout\n" " --oneshot -1 : Check for degraded arrays, then exit\n" " --test -t : Generate a TestMessage event against each array at startup\n" ; diff ./mdadm.8~current~ ./mdadm.8 --- ./mdadm.8~current~ 2004-11-02 14:41:38.000000000 +1100 +++ ./mdadm.8 2004-11-02 14:41:38.000000000 +1100 @@ -540,6 +540,13 @@ which will only continue monitoring if a is found in the config file. .TP +.BR -i ", " --pid-file +When +.B mdadm +is running in daemon mode, write the pid of the daemon process to +the specified file, instead of printing it on standard output. + +.TP .BR -1 ", " --oneshot Check arrays only once. This will generate .B NewArray diff ./mdadm.c~current~ ./mdadm.c --- ./mdadm.c~current~ 2004-11-02 14:41:38.000000000 +1100 +++ ./mdadm.c 2004-11-02 14:41:38.000000000 +1100 @@ -233,6 +233,7 @@ int main(int argc, char *argv[]) char *program = NULL; int delay = 0; int daemonise = 0; + char *pidfile = NULL; int oneshot = 0; int copies; @@ -677,6 +678,13 @@ int main(int argc, char *argv[]) case O(MONITOR,'f'): /* daemonise */ daemonise = 1; continue; + case O(MONITOR,'i'): /* pid */ + if (pidfile) + fprintf(stderr, Name ": only specify one pid file. %s ignored.\n", + optarg); + else + pidfile = optarg; + continue; case O(MONITOR,'1'): /* oneshot */ oneshot = 1; continue; @@ -963,8 +971,13 @@ int main(int argc, char *argv[]) rv = 1; break; } + if (pidfile && !daemonise) { + fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n"); + rv = 1; + break; + } rv= Monitor(devlist, mailaddr, program, - delay?delay:60, daemonise, scan, oneshot, configfile, test); + delay?delay:60, daemonise, scan, oneshot, configfile, test, pidfile); break; case GROW: diff ./mdadm.h~current~ ./mdadm.h --- ./mdadm.h~current~ 2004-11-02 14:41:38.000000000 +1100 +++ ./mdadm.h 2004-11-02 14:41:38.000000000 +1100 @@ -187,7 +187,7 @@ extern int Examine(mddev_dev_t devlist, extern int Monitor(mddev_dev_t devlist, char *mailaddr, char *alert_cmd, int period, int daemonise, int scan, int oneshot, - char *config, int test); + char *config, int test, char *pidfile); extern int Kill(char *dev, int force);