On Fri, 17 May 2019 14:04:20 +0800 zhenwei pi <pizhenwei@xxxxxxxxxxxxx> wrote: > Specify the pid file by command argument -p/--pid-file, tgtd can > write pid in pid file after running into daemon, and remove the > file before exiting. > > Signed-off-by: zhenwei pi <pizhenwei@xxxxxxxxxxxxx> > --- > usr/tgtd.c | 38 +++++++++++++++++++++++++++++++++++++- > 1 file changed, 37 insertions(+), 1 deletion(-) I got the following error: /usr/include/x86_64-linux-gnu/bits/fcntl2.h:50:24: error: call to ʽ__open_missing_modeʼ declared with attribute error: open with O_CREAT in second argument needs 3 arguments __open_missing_mode (); > diff --git a/usr/tgtd.c b/usr/tgtd.c > index ae2f489..328a942 100644 > --- a/usr/tgtd.c > +++ b/usr/tgtd.c > @@ -55,13 +55,14 @@ static struct option const long_options[] = { > {"foreground", no_argument, 0, 'f'}, > {"control-port", required_argument, 0, 'C'}, > {"nr_iothreads", required_argument, 0, 't'}, > + {"pid-file", required_argument, 0, 'p'}, > {"debug", required_argument, 0, 'd'}, > {"version", no_argument, 0, 'V'}, > {"help", no_argument, 0, 'h'}, > {0, 0, 0, 0}, > }; > > -static char *short_options = "fC:d:t:Vh"; > +static char *short_options = "fC:d:t:p:Vh"; > static char *spare_args; > > static void usage(int status) > @@ -77,6 +78,7 @@ static void usage(int status) > "-f, --foreground make the program run in the foreground\n" > "-C, --control-port NNNN use port NNNN for the mgmt channel\n" > "-t, --nr_iothreads NNNN specify the number of I/O threads\n" > + "-p, --pid-file filename specify the pid file\n" > "-d, --debug debuglevel print debugging information\n" > "-V, --version print version and exit\n" > "-h, --help display this help and exit\n", > @@ -174,6 +176,26 @@ set_rlimit: > return 0; > } > > +void create_pid_file(const char *path) > +{ > + int fd, ret; > + char buf[32] = {0}; > + > + fd = open(path, O_RDWR | O_CREAT); > + if (fd < 0) { > + eprintf("can't create %s, %m\n", path); > + return; > + } > + > + snprintf(buf, sizeof(buf), "%d", getpid()); > + ret = write(fd, buf, strlen(buf)); > + if (ret < 0) { > + eprintf("can't write %s, %m\n", path); > + } > + > + close(fd); I think that we had better to unlink here instead of close. Then even tgtd crashes due to a bug, the pid file will be removed.