aboutsummaryrefslogtreecommitdiffstats
path: root/tools/hv/hv_fcopy_daemon.c
diff options
context:
space:
mode:
authorVitaly Kuznetsov <vkuznets@redhat.com>2014-10-22 12:07:11 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2014-11-07 13:21:44 -0500
commit170f4bea2008054e5098f99359e29823a7b4b1b9 (patch)
treef4033adefc27a924e824c2348132a2b4866e40d1 /tools/hv/hv_fcopy_daemon.c
parent4f689190bb55d171d2f6614f8a6cbd4b868e48bd (diff)
tools: hv: introduce -n/--no-daemon option
All tools/hv daemons do mandatory daemon() on startup. However, no pidfile is created, this make it difficult for an init system to track such daemons. Modern linux distros use systemd as their init system. It can handle the daemonizing by itself, however, it requires a daemon to stay in foreground for that. Some distros already carry distro-specific patch for hv tools which switches off daemon(). Introduce -n/--no-daemon option for all 3 daemons in hv/tools. Parse options with getopt() to make this part easily expandable. Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com> Signed-off-by: K. Y. Srinivasan <kys@microsoft.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'tools/hv/hv_fcopy_daemon.c')
-rw-r--r--tools/hv/hv_fcopy_daemon.c33
1 files changed, 31 insertions, 2 deletions
diff --git a/tools/hv/hv_fcopy_daemon.c b/tools/hv/hv_fcopy_daemon.c
index 8f96b3ee0724..f437d739f37d 100644
--- a/tools/hv/hv_fcopy_daemon.c
+++ b/tools/hv/hv_fcopy_daemon.c
@@ -33,6 +33,7 @@
33#include <sys/stat.h> 33#include <sys/stat.h>
34#include <fcntl.h> 34#include <fcntl.h>
35#include <dirent.h> 35#include <dirent.h>
36#include <getopt.h>
36 37
37static int target_fd; 38static int target_fd;
38static char target_fname[W_MAX_PATH]; 39static char target_fname[W_MAX_PATH];
@@ -126,15 +127,43 @@ static int hv_copy_cancel(void)
126 127
127} 128}
128 129
129int main(void) 130void print_usage(char *argv[])
131{
132 fprintf(stderr, "Usage: %s [options]\n"
133 "Options are:\n"
134 " -n, --no-daemon stay in foreground, don't daemonize\n"
135 " -h, --help print this help\n", argv[0]);
136}
137
138int main(int argc, char *argv[])
130{ 139{
131 int fd, fcopy_fd, len; 140 int fd, fcopy_fd, len;
132 int error; 141 int error;
142 int daemonize = 1, long_index = 0, opt;
133 int version = FCOPY_CURRENT_VERSION; 143 int version = FCOPY_CURRENT_VERSION;
134 char *buffer[4096 * 2]; 144 char *buffer[4096 * 2];
135 struct hv_fcopy_hdr *in_msg; 145 struct hv_fcopy_hdr *in_msg;
136 146
137 if (daemon(1, 0)) { 147 static struct option long_options[] = {
148 {"help", no_argument, 0, 'h' },
149 {"no-daemon", no_argument, 0, 'n' },
150 {0, 0, 0, 0 }
151 };
152
153 while ((opt = getopt_long(argc, argv, "hn", long_options,
154 &long_index)) != -1) {
155 switch (opt) {
156 case 'n':
157 daemonize = 0;
158 break;
159 case 'h':
160 default:
161 print_usage(argv);
162 exit(EXIT_FAILURE);
163 }
164 }
165
166 if (daemonize && daemon(1, 0)) {
138 syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno)); 167 syslog(LOG_ERR, "daemon() failed; error: %s", strerror(errno));
139 exit(EXIT_FAILURE); 168 exit(EXIT_FAILURE);
140 } 169 }