1 | /*\ |
---|
2 | * Copyright (C) International Business Machines Corp., 2005 |
---|
3 | * Author(s): Anthony Liguori <aliguori@us.ibm.com> |
---|
4 | * |
---|
5 | * Xen Console Daemon |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or modify |
---|
8 | * it under the terms of the GNU General Public License as published by |
---|
9 | * the Free Software Foundation; under version 2 of the License. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | \*/ |
---|
20 | |
---|
21 | #include <sys/types.h> |
---|
22 | #include <sys/stat.h> |
---|
23 | #include <sys/wait.h> |
---|
24 | #include <unistd.h> |
---|
25 | #include <stdlib.h> |
---|
26 | #include <fcntl.h> |
---|
27 | #include <err.h> |
---|
28 | #include <errno.h> |
---|
29 | #include <stdio.h> |
---|
30 | #include <getopt.h> |
---|
31 | #include <stdbool.h> |
---|
32 | #include <sys/socket.h> |
---|
33 | #include <sys/un.h> |
---|
34 | #include <string.h> |
---|
35 | |
---|
36 | #include "xenctrl.h" |
---|
37 | #include "utils.h" |
---|
38 | |
---|
39 | struct xs_handle *xs; |
---|
40 | int xc; |
---|
41 | |
---|
42 | static void child_exit(int sig) |
---|
43 | { |
---|
44 | while (waitpid(-1, NULL, WNOHANG) > 0); |
---|
45 | } |
---|
46 | |
---|
47 | void daemonize(const char *pidfile) |
---|
48 | { |
---|
49 | pid_t pid; |
---|
50 | int fd; |
---|
51 | int len; |
---|
52 | int i; |
---|
53 | char buf[100]; |
---|
54 | |
---|
55 | if (getppid() == 1) { |
---|
56 | return; |
---|
57 | } |
---|
58 | |
---|
59 | if ((pid = fork()) > 0) { |
---|
60 | exit(0); |
---|
61 | } else if (pid == -1) { |
---|
62 | err(errno, "fork() failed"); |
---|
63 | } |
---|
64 | |
---|
65 | setsid(); |
---|
66 | |
---|
67 | if ((pid = fork()) > 0) { |
---|
68 | exit(0); |
---|
69 | } else if (pid == -1) { |
---|
70 | err(errno, "fork() failed"); |
---|
71 | } |
---|
72 | |
---|
73 | /* redirect fd 0,1,2 to /dev/null */ |
---|
74 | if ((fd = open("/dev/null",O_RDWR)) == -1) { |
---|
75 | exit(1); |
---|
76 | } |
---|
77 | |
---|
78 | for (i = 0; i <= 2; i++) { |
---|
79 | close(i); |
---|
80 | dup2(fd, i); |
---|
81 | } |
---|
82 | |
---|
83 | close(fd); |
---|
84 | |
---|
85 | umask(027); |
---|
86 | if (chdir("/") < 0) |
---|
87 | exit (1); |
---|
88 | |
---|
89 | fd = open(pidfile, O_RDWR | O_CREAT); |
---|
90 | if (fd == -1) { |
---|
91 | exit(1); |
---|
92 | } |
---|
93 | |
---|
94 | if (lockf(fd, F_TLOCK, 0) == -1) { |
---|
95 | exit(1); |
---|
96 | } |
---|
97 | |
---|
98 | len = sprintf(buf, "%ld\n", (long)getpid()); |
---|
99 | if (write(fd, buf, len) < 0) |
---|
100 | exit(1); |
---|
101 | |
---|
102 | signal(SIGCHLD, child_exit); |
---|
103 | signal(SIGTSTP, SIG_IGN); |
---|
104 | signal(SIGTTOU, SIG_IGN); |
---|
105 | signal(SIGTTIN, SIG_IGN); |
---|
106 | } |
---|
107 | |
---|
108 | bool xen_setup(void) |
---|
109 | { |
---|
110 | |
---|
111 | xs = xs_daemon_open(); |
---|
112 | if (xs == NULL) { |
---|
113 | dolog(LOG_ERR, |
---|
114 | "Failed to contact xenstore (%m). Is it running?"); |
---|
115 | goto out; |
---|
116 | } |
---|
117 | |
---|
118 | xc = xc_interface_open(); |
---|
119 | if (xc == -1) { |
---|
120 | dolog(LOG_ERR, "Failed to contact hypervisor (%m)"); |
---|
121 | goto out; |
---|
122 | } |
---|
123 | |
---|
124 | if (!xs_watch(xs, "@introduceDomain", "domlist")) { |
---|
125 | dolog(LOG_ERR, "xenstore watch on @introduceDomain fails."); |
---|
126 | goto out; |
---|
127 | } |
---|
128 | |
---|
129 | if (!xs_watch(xs, "@releaseDomain", "domlist")) { |
---|
130 | dolog(LOG_ERR, "xenstore watch on @releaseDomain fails."); |
---|
131 | goto out; |
---|
132 | } |
---|
133 | |
---|
134 | return true; |
---|
135 | |
---|
136 | out: |
---|
137 | if (xs) |
---|
138 | xs_daemon_close(xs); |
---|
139 | if (xc != -1) |
---|
140 | xc_interface_close(xc); |
---|
141 | return false; |
---|
142 | } |
---|
143 | |
---|