1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
From 3f3c1b2ad94338ba974477f53b2ee6f462aa77ae Mon Sep 17 00:00:00 2001
From: Natanael Copa <ncopa@alpinelinux.org>
Date: Tue, 13 Nov 2012 13:48:57 +0100
Subject: [PATCH] lxc-start: add option -p, --pidfile=FILE for use with
--daemon
Add option to create a pidfile for lxc-start daemon. This is helpful
for init scripts and process monitors.
Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
---
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 2b6778f..bd875d6 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 pidfile when running as daemon.
+ </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 40f0d6c..789ccd9 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..a031ee1 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 pidfile when daemonized\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[])
@@ -200,6 +204,7 @@ int main(int argc, char *argv[])
}
if (my_args.daemonize) {
+ FILE *pid_fp;
/* do an early check for needed privs, since otherwise the
* user won't see the error */
@@ -208,10 +213,26 @@ int main(int argc, char *argv[])
return err;
}
+ if (my_args.pidfile != NULL) {
+ pid_fp = fopen(my_args.pidfile, "w");
+ if (pid_fp == NULL) {
+ SYSERROR("failed to create '%s'", my_args.name);
+ return err;
+ }
+ }
+
if (daemon(0, 0)) {
SYSERROR("failed to daemonize '%s'", my_args.name);
return err;
}
+
+ if (my_args.pidfile != 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)
@@ -230,6 +251,9 @@ int main(int argc, char *argv[])
err = -1;
}
+ if (my_args.daemonize && my_args.pidfile)
+ unlink(my_args.pidfile);
+
return err;
}
--
1.8.0
|