debuggers.hg

view tools/console/daemon/utils.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 779c0ef9682c
children
line source
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 \*/
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 #include <signal.h>
37 #include "xenctrl.h"
38 #include "utils.h"
40 struct xs_handle *xs;
41 xc_interface *xc;
43 static void child_exit(int sig)
44 {
45 while (waitpid(-1, NULL, WNOHANG) > 0);
46 }
48 void daemonize(const char *pidfile)
49 {
50 pid_t pid;
51 int fd;
52 int len;
53 int i;
54 char buf[100];
56 if (getppid() == 1) {
57 return;
58 }
60 if ((pid = fork()) > 0) {
61 exit(0);
62 } else if (pid == -1) {
63 err(errno, "fork() failed");
64 }
66 setsid();
68 if ((pid = fork()) > 0) {
69 exit(0);
70 } else if (pid == -1) {
71 err(errno, "fork() failed");
72 }
74 /* redirect fd 0,1,2 to /dev/null */
75 if ((fd = open("/dev/null",O_RDWR)) == -1) {
76 exit(1);
77 }
79 for (i = 0; i <= 2; i++) {
80 close(i);
81 dup2(fd, i);
82 }
84 close(fd);
86 umask(027);
87 if (chdir("/") < 0)
88 exit (1);
90 fd = open(pidfile, O_RDWR | O_CREAT, S_IRUSR|S_IWUSR);
91 if (fd == -1) {
92 exit(1);
93 }
95 if (lockf(fd, F_TLOCK, 0) == -1) {
96 exit(1);
97 }
99 len = snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());
100 if (write(fd, buf, len) < 0)
101 exit(1);
103 signal(SIGCHLD, child_exit);
104 signal(SIGTSTP, SIG_IGN);
105 signal(SIGTTOU, SIG_IGN);
106 signal(SIGTTIN, SIG_IGN);
107 }
109 bool xen_setup(void)
110 {
112 xs = xs_daemon_open();
113 if (xs == NULL) {
114 dolog(LOG_ERR,
115 "Failed to contact xenstore (%m). Is it running?");
116 goto out;
117 }
119 xc = xc_interface_open(0,0,0);
120 if (!xc) {
121 dolog(LOG_ERR, "Failed to contact hypervisor (%m)");
122 goto out;
123 }
125 if (!xs_watch(xs, "@introduceDomain", "domlist")) {
126 dolog(LOG_ERR, "xenstore watch on @introduceDomain fails.");
127 goto out;
128 }
130 if (!xs_watch(xs, "@releaseDomain", "domlist")) {
131 dolog(LOG_ERR, "xenstore watch on @releaseDomain fails.");
132 goto out;
133 }
135 return true;
137 out:
138 if (xs)
139 xs_daemon_close(xs);
140 if (xc)
141 xc_interface_close(xc);
142 return false;
143 }