[lxc-devel] [PATCH v2] lxc-start: add option -p, --pidfile=FILE

Stéphane Graber stgraber at ubuntu.com
Thu Nov 15 16:14:23 UTC 2012


On 11/15/2012 10:31 AM, Serge Hallyn wrote:
> Quoting Natanael Copa (ncopa at alpinelinux.org):
>> Add option to create a pidfile for lxc-start. This is helpful for
>> init scripts and process monitors when running as daemon.
>>
>> Signed-off-by: Natanael Copa <ncopa at alpinelinux.org>
> 
> Acked-by: Serge E. Hallyn <serge.hallyn at ubuntu.com>

Acked-by: Stéphane Graber <stgraber at ubuntu.com>

Applied to staging with an extra commit changing the error message to
Serge's suggested one.

> Only one comment below
> 
>> ---
>> Changes v1 -> v2:
>>  - allow use --pidfile without --daemon
>>  - update the doc to reflect the above
>>  - reword usage help text slightly
>>
>>
>>  doc/lxc-start.sgml.in | 12 ++++++++++++
>>  src/lxc/arguments.h   |  1 +
>>  src/lxc/lxc_start.c   | 24 ++++++++++++++++++++++++
>>  3 files changed, 37 insertions(+)
>>
>> diff --git a/doc/lxc-start.sgml.in b/doc/lxc-start.sgml.in
>> index af79bbc..e4b9007 100644
>> --- a/doc/lxc-start.sgml.in
>> +++ b/doc/lxc-start.sgml.in
>> @@ -53,6 +53,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
>>        <arg choice="opt">-f <replaceable>config_file</replaceable></arg>
>>        <arg choice="opt">-c <replaceable>console_file</replaceable></arg>
>>        <arg choice="opt">-d</arg>
>> +      <arg choice="opt">-p <replaceable>pid_file</replaceable></arg>
>>        <arg choice="opt">-s KEY=VAL</arg>
>>        <arg choice="opt">-C</arg>
>>        <arg choice="opt">command</arg>
>> @@ -109,6 +110,17 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
>>  
>>        <varlistentry>
>>  	<term>
>> +	  <option>-p, --pidfile <replaceable>pid_file</replaceable></option>
>> +	</term>
>> +	<listitem>
>> +	  <para>
>> +	    Create a file with the process id.
>> +	  </para>
>> +	</listitem>
>> +      </varlistentry>
>> +
>> +      <varlistentry>
>> +	<term>
>>  	  <option>-f, --rcfile <replaceable>config_file</replaceable></option>
>>  	</term>
>>  	<listitem>
>> diff --git a/src/lxc/arguments.h b/src/lxc/arguments.h
>> index 3c9d28f..188c460 100644
>> --- a/src/lxc/arguments.h
>> +++ b/src/lxc/arguments.h
>> @@ -45,6 +45,7 @@ struct lxc_arguments {
>>  	int daemonize;
>>  	const char *rcfile;
>>  	const char *console;
>> +	const char *pidfile;
>>  
>>  	/* for lxc-checkpoint/restart */
>>  	const char *statefile;
>> diff --git a/src/lxc/lxc_start.c b/src/lxc/lxc_start.c
>> index 81a5774..ca1cc2a 100644
>> --- a/src/lxc/lxc_start.c
>> +++ b/src/lxc/lxc_start.c
>> @@ -62,6 +62,7 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg)
>>  	case 'f': args->rcfile = arg; break;
>>  	case 'C': args->close_all_fds = 1; break;
>>  	case 's': return lxc_config_define_add(&defines, arg);
>> +	case 'p': args->pidfile = arg; break;
>>  	}
>>  	return 0;
>>  }
>> @@ -72,6 +73,7 @@ static const struct option my_longopts[] = {
>>  	{"define", required_argument, 0, 's'},
>>  	{"console", required_argument, 0, 'c'},
>>  	{"close-all-fds", no_argument, 0, 'C'},
>> +	{"pidfile", required_argument, 0, 'p'},
>>  	LXC_COMMON_OPTIONS
>>  };
>>  
>> @@ -85,6 +87,7 @@ lxc-start start COMMAND in specified container NAME\n\
>>  Options :\n\
>>    -n, --name=NAME      NAME for name of the container\n\
>>    -d, --daemon         daemonize the container\n\
>> +  -p, --pidfile=FILE   Create a file with the process id\n\
>>    -f, --rcfile=FILE    Load configuration file FILE\n\
>>    -c, --console=FILE   Set the file output for the container console\n\
>>    -C, --close-all-fds  If any fds are inherited, close them\n\
>> @@ -95,6 +98,7 @@ Options :\n\
>>  	.parser    = my_parser,
>>  	.checker   = NULL,
>>  	.daemonize = 0,
>> +	.pidfile = NULL,
>>  };
>>  
>>  int main(int argc, char *argv[])
>> @@ -107,6 +111,7 @@ int main(int argc, char *argv[])
>>  		"/sbin/init",
>>  		'\0',
>>  	};
>> +	FILE *pid_fp = NULL;
>>  
>>  	lxc_list_init(&defines);
>>  
>> @@ -199,6 +204,14 @@ int main(int argc, char *argv[])
>>  		free(console);
>>  	}
>>  
>> +	if (my_args.pidfile != NULL) {
>> +		pid_fp = fopen(my_args.pidfile, "w");
>> +		if (pid_fp == NULL) {
>> +			SYSERROR("failed to create '%s'", my_args.name);
> 
> This should be more helpful, i.e.
> 
> SYSERROR("failed to create pidfile '%s' for '%s'", my_args.pidfile, my_args.name);
> 
>> +			return err;
>> +		}
>> +	}
>> +
>>  	if (my_args.daemonize) {
>>  		/* do an early check for needed privs, since otherwise the
>>  		 * user won't see the error */
>> @@ -214,6 +227,14 @@ int main(int argc, char *argv[])
>>  		}
>>  	}
>>  
>> +	if (pid_fp != NULL) {
>> +		if (fprintf(pid_fp, "%d\n", getpid()) < 0) {
>> +			SYSERROR("failed to write '%s'", my_args.pidfile);
>> +			return err;
>> +		}
>> +		fclose(pid_fp);
>> +	}
>> +
>>  	if (my_args.close_all_fds)
>>  		conf->close_all_fds = 1;
>>  
>> @@ -230,6 +251,9 @@ int main(int argc, char *argv[])
>>  		err = -1;
>>  	}
>>  
>> +	if (my_args.pidfile)
>> +		unlink(my_args.pidfile);
>> +
>>  	return err;
>>  }
>>  
>> -- 
>> 1.8.0
>>
>>
>> ------------------------------------------------------------------------------
>> Monitor your physical, virtual and cloud infrastructure from a single
>> web console. Get in-depth insight into apps, servers, databases, vmware,
>> SAP, cloud infrastructure, etc. Download 30-day Free Trial.
>> Pricing starts from $795 for 25 servers or applications!
>> http://p.sf.net/sfu/zoho_dev2dev_nov
>> _______________________________________________
>> Lxc-devel mailing list
>> Lxc-devel at lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/lxc-devel
> 
> ------------------------------------------------------------------------------
> Monitor your physical, virtual and cloud infrastructure from a single
> web console. Get in-depth insight into apps, servers, databases, vmware,
> SAP, cloud infrastructure, etc. Download 30-day Free Trial.
> Pricing starts from $795 for 25 servers or applications!
> http://p.sf.net/sfu/zoho_dev2dev_nov
> _______________________________________________
> Lxc-devel mailing list
> Lxc-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/lxc-devel
> 


-- 
Stéphane Graber



-- 
Stéphane Graber
Ubuntu developer
http://www.ubuntu.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 899 bytes
Desc: OpenPGP digital signature
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20121115/e28ba162/attachment.pgp>


More information about the lxc-devel mailing list