ia64/xen-unstable

view tools/console/daemon/utils.c @ 8335:c8378d3c3af8

Make sure to fork again after setsid() so that child cannot regain CTTY.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
author Anthony Liguori <anthony@codemonkey.ws>
date Mon Dec 12 15:11:13 2005 +0000 (2005-12-12)
parents 06d84bf87159
children 7fba181c8531
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>
36 #include "xenctrl.h"
37 #include "utils.h"
39 struct xs_handle *xs;
40 int xc;
42 bool _read_write_sync(int fd, void *data, size_t size, bool do_read)
43 {
44 size_t offset = 0;
45 ssize_t len;
47 while (offset < size) {
48 if (do_read) {
49 len = read(fd, data + offset, size - offset);
50 } else {
51 len = write(fd, data + offset, size - offset);
52 }
54 if (len < 1) {
55 if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
56 continue;
57 } else {
58 return false;
59 }
60 } else {
61 offset += len;
62 }
63 }
65 return true;
66 }
68 static void child_exit(int sig)
69 {
70 while (waitpid(-1, NULL, WNOHANG) > 0);
71 }
73 void daemonize(const char *pidfile)
74 {
75 pid_t pid;
76 int fd;
77 int len;
78 int i;
79 char buf[100];
81 if (getppid() == 1) {
82 return;
83 }
85 if ((pid = fork()) > 0) {
86 exit(0);
87 } else if (pid == -1) {
88 err(errno, "fork() failed");
89 }
91 setsid();
93 if ((pid = fork()) > 0) {
94 exit(0);
95 } else if (pid == -1) {
96 err(errno, "fork() failed");
97 }
99 /* redirect fd 0,1,2 to /dev/null */
100 if ((fd = open("/dev/null",O_RDWR)) == -1) {
101 exit(1);
102 }
104 for (i = 0; i <= 2; i++) {
105 close(i);
106 dup2(fd, i);
107 }
109 close(fd);
111 umask(027);
112 if (chdir("/") < 0)
113 exit (1);
115 fd = open(pidfile, O_RDWR | O_CREAT);
116 if (fd == -1) {
117 exit(1);
118 }
120 if (lockf(fd, F_TLOCK, 0) == -1) {
121 exit(1);
122 }
124 len = sprintf(buf, "%d\n", getpid());
125 if (write(fd, buf, len) < 0)
126 exit(1);
128 signal(SIGCHLD, child_exit);
129 signal(SIGTSTP, SIG_IGN);
130 signal(SIGTTOU, SIG_IGN);
131 signal(SIGTTIN, SIG_IGN);
132 }
134 bool xen_setup(void)
135 {
137 xs = xs_daemon_open();
138 if (xs == NULL) {
139 dolog(LOG_ERR,
140 "Failed to contact xenstore (%m). Is it running?");
141 goto out;
142 }
144 xc = xc_interface_open();
145 if (xc == -1) {
146 dolog(LOG_ERR, "Failed to contact hypervisor (%m)");
147 goto out;
148 }
150 if (!xs_watch(xs, "@introduceDomain", "domlist")) {
151 dolog(LOG_ERR, "xenstore watch on @introduceDomain fails.");
152 goto out;
153 }
155 if (!xs_watch(xs, "@releaseDomain", "domlist")) {
156 dolog(LOG_ERR, "xenstore watch on @releaseDomain fails.");
157 goto out;
158 }
160 return true;
162 out:
163 if (xs)
164 xs_daemon_close(xs);
165 if (xc != -1)
166 xc_interface_close(xc);
167 return false;
168 }