aboutsummaryrefslogtreecommitdiffstats
path: root/tools/console/daemon/utils.c
blob: aab6f425e8edb3023c650d6c863f22ab8a63102d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*\
 *  Copyright (C) International Business Machines  Corp., 2005
 *  Author(s): Anthony Liguori <aliguori@us.ibm.com>
 *
 *  Xen Console Daemon
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; under version 2 of the License.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
\*/

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <getopt.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <signal.h>

#include "xenctrl.h"
#include "utils.h"

struct xs_handle *xs;
xc_interface *xc;

static void child_exit(int sig)
{
	while (waitpid(-1, NULL, WNOHANG) > 0);
}

void daemonize(const char *pidfile)
{
	pid_t pid;
	int fd;
	int len;
	int i;
	char buf[100];

	if (getppid() == 1) {
		return;
	}

	if ((pid = fork()) > 0) {
		exit(0);
	} else if (pid == -1) {
		err(errno, "fork() failed");
	}

	setsid();

	if ((pid = fork()) > 0) {
		exit(0);
	} else if (pid == -1) {
		err(errno, "fork() failed");
	}

	/* redirect fd 0,1,2 to /dev/null */
	if ((fd = open("/dev/null",O_RDWR)) == -1) {
		exit(1);
	}

	for (i = 0; i <= 2; i++) {
		close(i);
		dup2(fd, i);
	}

	close(fd);

	umask(027);
	if (chdir("/") < 0)
		exit (1);

	fd = open(pidfile, O_RDWR | O_CREAT, S_IRUSR|S_IWUSR);
	if (fd == -1) {
		exit(1);
	}

	if (lockf(fd, F_TLOCK, 0) == -1) {
		exit(1);
	}

	len = snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());
	if (write(fd, buf, len) < 0)
		exit(1);

	signal(SIGCHLD, child_exit);
	signal(SIGTSTP, SIG_IGN);
	signal(SIGTTOU, SIG_IGN);
	signal(SIGTTIN, SIG_IGN);
}

bool xen_setup(void)
{
	
	xs = xs_daemon_open();
	if (xs == NULL) {
		dolog(LOG_ERR,
		      "Failed to contact xenstore (%m).  Is it running?");
		goto out;
	}

	xc = xc_interface_open(0,0,0);
	if (!xc) {
		dolog(LOG_ERR, "Failed to contact hypervisor (%m)");
		goto out;
	}

	if (!xs_watch(xs, "@introduceDomain", "domlist")) {
		dolog(LOG_ERR, "xenstore watch on @introduceDomain fails.");
		goto out;
	}

	if (!xs_watch(xs, "@releaseDomain", "domlist")) {
		dolog(LOG_ERR, "xenstore watch on @releaseDomain fails.");
		goto out;
	}

	return true;

 out:
	if (xs)
		xs_daemon_close(xs);
	if (xc)
		xc_interface_close(xc);
	return false;
}