Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2016, Citrix Systems, Inc.
3 : : *
4 : : * All rights reserved.
5 : : *
6 : : * Redistribution and use in source and binary forms, with or without
7 : : * modification, are permitted provided that the following conditions are met:
8 : : *
9 : : * 1. Redistributions of source code must retain the above copyright
10 : : * notice, this list of conditions and the following disclaimer.
11 : : * 2. Redistributions in binary form must reproduce the above copyright
12 : : * notice, this list of conditions and the following disclaimer in the
13 : : * documentation and/or other materials provided with the distribution.
14 : : * 3. Neither the name of the copyright holder nor the names of its
15 : : * contributors may be used to endorse or promote products derived from
16 : : * this software without specific prior written permission.
17 : : *
18 : : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 : : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 : : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 : : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22 : : * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 : : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 : : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 : : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 : : * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 : : * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 : : * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 : : */
30 : :
31 : : #ifdef HAVE_CONFIG_H
32 : : #include "config.h"
33 : : #endif
34 : :
35 : : #include <stdio.h>
36 : : #include <errno.h>
37 : : #include <fcntl.h>
38 : : #include <stdlib.h>
39 : : #include <unistd.h>
40 : :
41 : : #include "tapdisk.h"
42 : : #include "tapdisk-utils.h"
43 : : #include "tapdisk-server.h"
44 : : #include "tapdisk-control.h"
45 : : #include "tapdisk-metrics.h"
46 : :
47 : : void tdnbd_fdreceiver_start();
48 : : void tdnbd_fdreceiver_stop();
49 : :
50 : : static void
51 : 0 : usage(const char *app, int err)
52 : : {
53 : 0 : fprintf(stderr, "usage: %s <-u uuid> <-c control socket>\n", app);
54 : 0 : exit(err);
55 : : }
56 : :
57 : : static FILE *
58 : 0 : fdup(FILE *stream, const char *mode)
59 : : {
60 : : int fd, err;
61 : : FILE *f;
62 : :
63 : 0 : fd = dup(STDOUT_FILENO);
64 : 0 : if (fd < 0)
65 : : goto fail;
66 : :
67 : 0 : f = fdopen(fd, mode);
68 : 0 : if (!f)
69 : : goto fail;
70 : :
71 : : return f;
72 : :
73 : : fail:
74 : 0 : err = -errno;
75 : 0 : if (fd >= 0)
76 : 0 : close(fd);
77 : 0 : errno = -err;
78 : :
79 : 0 : return NULL;
80 : : }
81 : :
82 : : int
83 : 0 : main(int argc, char *argv[])
84 : : {
85 : : char *control;
86 : : int c, err, nodaemon;
87 : : FILE *out;
88 : :
89 : 0 : control = NULL;
90 : 0 : nodaemon = 0;
91 : :
92 : 0 : while ((c = getopt(argc, argv, "Dh")) != -1) {
93 : 0 : switch (c) {
94 : : case 'D':
95 : : nodaemon = 1;
96 : : break;
97 : : case 'h':
98 : 0 : usage(argv[0], 0);
99 : : break;
100 : : default:
101 : 0 : usage(argv[0], EINVAL);
102 : : }
103 : : }
104 : :
105 : 0 : if (optind != argc)
106 : 0 : usage(argv[0], EINVAL);
107 : :
108 : 0 : err = tapdisk_server_init();
109 : 0 : if (err) {
110 : : DPRINTF("failed to initialize server: %d\n", err);
111 : : goto out;
112 : : }
113 : :
114 : 0 : out = fdup(stdout, "w");
115 : 0 : if (!out) {
116 : 0 : err = -errno;
117 : : DPRINTF("failed to dup stdout: %d\n", err);
118 : : goto out;
119 : : }
120 : :
121 : 0 : if (!nodaemon) {
122 : 0 : err = daemon(0, 0);
123 : 0 : if (err) {
124 : 0 : DPRINTF("failed to daemonize: %d\n", errno);
125 : : goto out;
126 : : }
127 : : }
128 : :
129 : 0 : tapdisk_start_logging("tapdisk", NULL);
130 : :
131 : 0 : err = tapdisk_control_open(&control);
132 : 0 : if (err) {
133 : : DPRINTF("failed to open control socket: %d\n", err);
134 : : goto out;
135 : : }
136 : :
137 : 0 : err = tapdisk_server_complete();
138 : 0 : if (err) {
139 : : DPRINTF("failed to complete server: %d\n", err);
140 : : goto out;
141 : : }
142 : :
143 : 0 : DPRINTF("Tapdisk running, control on %s\n", control);
144 : :
145 : 0 : fprintf(out, "%s\n", control);
146 : 0 : fclose(out);
147 : :
148 : 0 : err = td_metrics_start();
149 : 0 : if (err) {
150 : : DPRINTF("failed to create metrics folder: %d\n", err);
151 : : goto out;
152 : : }
153 : : /*
154 : : * NB: We're unconditionally starting the FD receiver here - this is
155 : : * for the block-nbd driver. In the future we may want to start this as
156 : : * a response to a tap-ctl message
157 : : */
158 : 0 : tdnbd_fdreceiver_start();
159 : :
160 : 0 : err = tapdisk_server_run();
161 : :
162 : : out:
163 : 0 : if (err) {
164 : : EPRINTF("Tapdisk exiting with error %d\n", err);
165 : : }
166 : 0 : td_metrics_stop();
167 : 0 : tdnbd_fdreceiver_stop();
168 : 0 : tapdisk_control_close();
169 : 0 : tapdisk_stop_logging();
170 : 0 : return -err;
171 : : }
|