aboutsummaryrefslogtreecommitdiffstats
path: root/tools/hv
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
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')
-rw-r--r--tools/hv/hv_fcopy_daemon.c33
-rw-r--r--tools/hv/hv_kvp_daemon.c34
-rw-r--r--tools/hv/hv_vss_daemon.c33
3 files changed, 94 insertions, 6 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 }
diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c
index 4088b816a3ee..22b076419c80 100644
--- a/tools/hv/hv_kvp_daemon.c
+++ b/tools/hv/hv_kvp_daemon.c
@@ -43,6 +43,7 @@
43#include <fcntl.h> 43#include <fcntl.h>
44#include <dirent.h> 44#include <dirent.h>
45#include <net/if.h> 45#include <net/if.h>
46#include <getopt.h>
46 47
47/* 48/*
48 * KVP protocol: The user mode component first registers with the 49 * KVP protocol: The user mode component first registers with the
@@ -1417,7 +1418,15 @@ netlink_send(int fd, struct cn_msg *msg)
1417 return sendmsg(fd, &message, 0); 1418 return sendmsg(fd, &message, 0);
1418} 1419}
1419 1420
1420int main(void) 1421void print_usage(char *argv[])
1422{
1423 fprintf(stderr, "Usage: %s [options]\n"
1424 "Options are:\n"
1425 " -n, --no-daemon stay in foreground, don't daemonize\n"
1426 " -h, --help print this help\n", argv[0]);
1427}
1428
1429int main(int argc, char *argv[])
1421{ 1430{
1422 int fd, len, nl_group; 1431 int fd, len, nl_group;
1423 int error; 1432 int error;
@@ -1435,9 +1444,30 @@ int main(void)
1435 struct hv_kvp_ipaddr_value *kvp_ip_val; 1444 struct hv_kvp_ipaddr_value *kvp_ip_val;
1436 char *kvp_recv_buffer; 1445 char *kvp_recv_buffer;
1437 size_t kvp_recv_buffer_len; 1446 size_t kvp_recv_buffer_len;
1447 int daemonize = 1, long_index = 0, opt;
1448
1449 static struct option long_options[] = {
1450 {"help", no_argument, 0, 'h' },
1451 {"no-daemon", no_argument, 0, 'n' },
1452 {0, 0, 0, 0 }
1453 };
1454
1455 while ((opt = getopt_long(argc, argv, "hn", long_options,
1456 &long_index)) != -1) {
1457 switch (opt) {
1458 case 'n':
1459 daemonize = 0;
1460 break;
1461 case 'h':
1462 default:
1463 print_usage(argv);
1464 exit(EXIT_FAILURE);
1465 }
1466 }
1438 1467
1439 if (daemon(1, 0)) 1468 if (daemonize && daemon(1, 0))
1440 return 1; 1469 return 1;
1470
1441 openlog("KVP", 0, LOG_USER); 1471 openlog("KVP", 0, LOG_USER);
1442 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid()); 1472 syslog(LOG_INFO, "KVP starting; pid is:%d", getpid());
1443 1473
diff --git a/tools/hv/hv_vss_daemon.c b/tools/hv/hv_vss_daemon.c
index 1db9430d3fe4..b720d8f0f901 100644
--- a/tools/hv/hv_vss_daemon.c
+++ b/tools/hv/hv_vss_daemon.c
@@ -36,6 +36,7 @@
36#include <linux/hyperv.h> 36#include <linux/hyperv.h>
37#include <linux/netlink.h> 37#include <linux/netlink.h>
38#include <syslog.h> 38#include <syslog.h>
39#include <getopt.h>
39 40
40static struct sockaddr_nl addr; 41static struct sockaddr_nl addr;
41 42
@@ -155,7 +156,15 @@ static int netlink_send(int fd, struct cn_msg *msg)
155 return sendmsg(fd, &message, 0); 156 return sendmsg(fd, &message, 0);
156} 157}
157 158
158int main(void) 159void print_usage(char *argv[])
160{
161 fprintf(stderr, "Usage: %s [options]\n"
162 "Options are:\n"
163 " -n, --no-daemon stay in foreground, don't daemonize\n"
164 " -h, --help print this help\n", argv[0]);
165}
166
167int main(int argc, char *argv[])
159{ 168{
160 int fd, len, nl_group; 169 int fd, len, nl_group;
161 int error; 170 int error;
@@ -167,8 +176,28 @@ int main(void)
167 struct hv_vss_msg *vss_msg; 176 struct hv_vss_msg *vss_msg;
168 char *vss_recv_buffer; 177 char *vss_recv_buffer;
169 size_t vss_recv_buffer_len; 178 size_t vss_recv_buffer_len;
179 int daemonize = 1, long_index = 0, opt;
180
181 static struct option long_options[] = {
182 {"help", no_argument, 0, 'h' },
183 {"no-daemon", no_argument, 0, 'n' },
184 {0, 0, 0, 0 }
185 };
186
187 while ((opt = getopt_long(argc, argv, "hn", long_options,
188 &long_index)) != -1) {
189 switch (opt) {
190 case 'n':
191 daemonize = 0;
192 break;
193 case 'h':
194 default:
195 print_usage(argv);
196 exit(EXIT_FAILURE);
197 }
198 }
170 199
171 if (daemon(1, 0)) 200 if (daemonize && daemon(1, 0))
172 return 1; 201 return 1;
173 202
174 openlog("Hyper-V VSS", 0, LOG_USER); 203 openlog("Hyper-V VSS", 0, LOG_USER);