debuggers.hg

view tools/blktap2/drivers/tapdisk2.c @ 22848:6341fe0f4e5a

Added tag 4.1.0-rc2 for changeset 9dca60d88c63
author Keir Fraser <keir@xen.org>
date Tue Jan 25 14:06:55 2011 +0000 (2011-01-25)
parents 63d0f5348af2
children
line source
1 /*
2 * Copyright (c) 2008, XenSource Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of XenSource Inc. nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
20 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include <stdio.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #ifdef MEMSHR
34 #include <memshr.h>
35 #endif
37 #include "tapdisk.h"
38 #include "tapdisk-utils.h"
39 #include "tapdisk-server.h"
40 #include "tapdisk-control.h"
42 static void
43 usage(const char *app, int err)
44 {
45 fprintf(stderr, "usage: %s <-u uuid> <-c control socket>\n", app);
46 exit(err);
47 }
49 int
50 main(int argc, char *argv[])
51 {
52 char *control;
53 int c, err, nodaemon;
55 control = NULL;
56 nodaemon = 0;
58 while ((c = getopt(argc, argv, "s:Dh")) != -1) {
59 switch (c) {
60 case 'D':
61 nodaemon = 1;
62 break;
63 case 'h':
64 usage(argv[0], 0);
65 break;
66 case 's':
67 #ifdef MEMSHR
68 memshr_set_domid(atoi(optarg));
69 #else
70 fprintf(stderr, "MEMSHR support not compiled in.\n");
71 exit(EXIT_FAILURE);
72 #endif
73 break;
74 default:
75 usage(argv[0], EINVAL);
76 }
77 }
79 if (optind != argc)
80 usage(argv[0], EINVAL);
82 chdir("/");
83 tapdisk_start_logging("tapdisk2");
85 err = tapdisk_server_init();
86 if (err) {
87 DPRINTF("failed to initialize server: %d\n", err);
88 goto out;
89 }
91 if (!nodaemon) {
92 err = daemon(0, 1);
93 if (err) {
94 DPRINTF("failed to daemonize: %d\n", errno);
95 goto out;
96 }
97 }
99 err = tapdisk_control_open(&control);
100 if (err) {
101 DPRINTF("failed to open control socket: %d\n", err);
102 goto out;
103 }
105 fprintf(stdout, "%s\n", control);
106 fflush(stdout);
108 if (!nodaemon) {
109 int fd;
111 fd = open("/dev/null", O_RDWR);
112 if (fd != -1) {
113 dup2(fd, STDIN_FILENO);
114 dup2(fd, STDOUT_FILENO);
115 dup2(fd, STDERR_FILENO);
116 if (fd > 2)
117 close(fd);
118 }
119 }
121 err = tapdisk_server_complete();
122 if (err) {
123 DPRINTF("failed to complete server: %d\n", err);
124 goto out;
125 }
127 err = tapdisk_server_run();
129 out:
130 tapdisk_control_close();
131 tapdisk_stop_logging();
132 return err;
133 }