ia64/xen-unstable

view tools/console/daemon/utils.c @ 6767:d22091179975

Check the return value of chdir(2) and write(2).
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Mon Sep 12 20:24:58 2005 +0000 (2005-09-12)
parents 8db9c5873b9b
children 4d899a738d59 8ca0f98ba8e2
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 "xen/io/domain_controller.h"
39 #include "utils.h"
41 struct xs_handle *xs;
42 int xc;
44 bool _read_write_sync(int fd, void *data, size_t size, bool do_read)
45 {
46 size_t offset = 0;
47 ssize_t len;
49 while (offset < size) {
50 if (do_read) {
51 len = read(fd, data + offset, size - offset);
52 } else {
53 len = write(fd, data + offset, size - offset);
54 }
56 if (len < 1) {
57 if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
58 continue;
59 } else {
60 return false;
61 }
62 } else {
63 offset += len;
64 }
65 }
67 return true;
68 }
70 static void child_exit(int sig)
71 {
72 while (waitpid(-1, NULL, WNOHANG) > 0);
73 }
75 void daemonize(const char *pidfile)
76 {
77 pid_t pid;
78 int fd;
79 int len;
80 int i;
81 char buf[100];
83 if (getppid() == 1) {
84 return;
85 }
87 if ((pid = fork()) > 0) {
88 exit(0);
89 } else if (pid == -1) {
90 err(errno, "fork() failed");
91 }
93 setsid();
95 /* redirect fd 0,1,2 to /dev/null */
96 if ((fd = open("/dev/null",O_RDWR)) == -1) {
97 exit(1);
98 }
100 for (i = 0; i <= 2; i++) {
101 close(i);
102 dup2(fd, i);
103 }
105 close(fd);
107 umask(027);
108 if (chdir("/") < 0)
109 exit (1);
111 fd = open(pidfile, O_RDWR | O_CREAT);
112 if (fd == -1) {
113 exit(1);
114 }
116 if (lockf(fd, F_TLOCK, 0) == -1) {
117 exit(1);
118 }
120 len = sprintf(buf, "%d\n", getpid());
121 if (write(fd, buf, len) < 0)
122 exit(1);
124 signal(SIGCHLD, child_exit);
125 signal(SIGTSTP, SIG_IGN);
126 signal(SIGTTOU, SIG_IGN);
127 signal(SIGTTIN, SIG_IGN);
128 }
130 bool xen_setup(void)
131 {
133 xs = xs_daemon_open();
134 if (xs == NULL) {
135 dolog(LOG_ERR,
136 "Failed to contact xenstore (%m). Is it running?");
137 goto out;
138 }
140 xc = xc_interface_open();
141 if (xc == -1) {
142 dolog(LOG_ERR, "Failed to contact hypervisor (%m)");
143 goto out;
144 }
146 if (!xs_watch(xs, "@introduceDomain", "domlist")) {
147 dolog(LOG_ERR, "xenstore watch on @introduceDomain fails.");
148 goto out;
149 }
151 if (!xs_watch(xs, "@releaseDomain", "domlist")) {
152 dolog(LOG_ERR, "xenstore watch on @releaseDomain fails.");
153 goto out;
154 }
156 return true;
158 out:
159 if (xs)
160 xs_daemon_close(xs);
161 if (xc != -1)
162 xc_interface_close(xc);
163 return false;
164 }