Commit 43706f0d authored by Daniel De Graaf's avatar Daniel De Graaf

xenstored: support running in minios stubdom

A previous versions of this patch has been sent to xen-devel. See
http://lists.xensource.com/archives/html/xen-devel/2009-03/msg01655.htmlSigned-off-by: default avatarDiego Ongaro <diego.ongaro@citrix.com>
Signed-off-by: default avatarAlex Zeffertt <alex.zeffertt@eu.citrix.com>
Signed-off-by: default avatarDaniel De Graaf <dgdegra@tycho.nsa.gov>
Acked-by: default avatarIan Campbell <ian.campbell@citrix.com>
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Cc: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Committed-by: default avatarIan Jackson <Ian.Jackson@eu.citrix.com>
parent b25272d6
......@@ -13,9 +13,10 @@ CLIENTS += xenstore-write xenstore-ls xenstore-watch
XENSTORED_OBJS = xenstored_core.o xenstored_watch.o xenstored_domain.o xenstored_transaction.o xs_lib.o talloc.o utils.o tdb.o hashtable.o
XENSTORED_OBJS_$(CONFIG_Linux) = xenstored_linux.o
XENSTORED_OBJS_$(CONFIG_SunOS) = xenstored_solaris.o xenstored_probes.o
XENSTORED_OBJS_$(CONFIG_NetBSD) = xenstored_netbsd.o
XENSTORED_OBJS_$(CONFIG_Linux) = xenstored_linux.o xenstored_posix.o
XENSTORED_OBJS_$(CONFIG_SunOS) = xenstored_solaris.o xenstored_posix.o xenstored_probes.o
XENSTORED_OBJS_$(CONFIG_NetBSD) = xenstored_netbsd.o xenstored_posix.o
XENSTORED_OBJS_$(CONFIG_MiniOS) = xenstored_minios.o
XENSTORED_OBJS += $(XENSTORED_OBJS_y)
......@@ -28,6 +29,10 @@ endif
ALL_TARGETS = libxenstore.so libxenstore.a clients xs_tdb_dump xenstored
ifdef CONFIG_STUBDOM
CFLAGS += -DNO_SOCKETS=1
endif
.PHONY: all
all: $(ALL_TARGETS)
......@@ -49,6 +54,9 @@ endif
xenstored: $(XENSTORED_OBJS)
$(CC) $(LDFLAGS) $^ $(LDLIBS_libxenctrl) $(SOCKET_LIBS) -o $@ $(APPEND_LDFLAGS)
xenstored.a: $(XENSTORED_OBJS)
$(AR) cr $@ $^
$(CLIENTS): xenstore
ln -f xenstore $@
......
......@@ -19,7 +19,9 @@ static inline bool strends(const char *a, const char *b)
return streq(a + strlen(a) - strlen(b), b);
}
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#endif
void barf(const char *fmt, ...) __attribute__((noreturn));
void barf_perror(const char *fmt, ...) __attribute__((noreturn));
......
......@@ -224,7 +224,6 @@ static void reopen_log(void)
}
}
static bool write_messages(struct connection *conn)
{
int ret;
......@@ -327,7 +326,8 @@ static int initialize_set(fd_set *inset, fd_set *outset, int sock, int ro_sock,
set_fd(sock, inset, &max);
if (ro_sock != -1)
set_fd(ro_sock, inset, &max);
set_fd(reopen_log_pipe[0], inset, &max);
if (reopen_log_pipe[0] != -1)
set_fd(reopen_log_pipe[0], inset, &max);
if (xce_handle != NULL)
set_fd(xc_evtchn_fd(xce_handle), inset, &max);
......@@ -1664,53 +1664,6 @@ static void corrupt(struct connection *conn, const char *fmt, ...)
}
static void write_pidfile(const char *pidfile)
{
char buf[100];
int len;
int fd;
fd = open(pidfile, O_RDWR | O_CREAT, 0600);
if (fd == -1)
barf_perror("Opening pid file %s", pidfile);
/* We exit silently if daemon already running. */
if (lockf(fd, F_TLOCK, 0) == -1)
exit(0);
len = snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());
if (write(fd, buf, len) != len)
barf_perror("Writing pid file %s", pidfile);
}
/* Stevens. */
static void daemonize(void)
{
pid_t pid;
/* Separate from our parent via fork, so init inherits us. */
if ((pid = fork()) < 0)
barf_perror("Failed to fork daemon");
if (pid != 0)
exit(0);
/* Session leader so ^C doesn't whack us. */
setsid();
/* Let session leader exit so child cannot regain CTTY */
if ((pid = fork()) < 0)
barf_perror("Failed to fork daemon");
if (pid != 0)
exit(0);
/* Move off any mount points we might be in. */
if (chdir("/") == -1)
barf_perror("Failed to chdir");
/* Discard our parent's old-fashioned umask prejudices. */
umask(0);
}
#ifdef NO_SOCKETS
static void init_sockets(int **psock, int **pro_sock)
{
......@@ -1874,20 +1827,10 @@ int main(int argc, char *argv[])
reopen_log();
/* make sure xenstored directory exists */
if (mkdir(xs_daemon_rundir(), 0755)) {
if (errno != EEXIST) {
perror("error: mkdir daemon rundir");
exit(-1);
}
}
if (mkdir(xs_daemon_rootdir(), 0755)) {
if (errno != EEXIST) {
perror("error: mkdir daemon rootdir");
exit(-1);
}
}
/* make sure xenstored directories exist */
/* Errors ignored here, will be reported when we open files */
mkdir(xs_daemon_rundir(), 0755);
mkdir(xs_daemon_rootdir(), 0755);
if (dofork) {
openlog("xenstored", 0, LOG_DAEMON);
......@@ -1904,10 +1847,7 @@ int main(int argc, char *argv[])
signal(SIGPIPE, SIG_IGN);
init_sockets(&sock, &ro_sock);
if (pipe(reopen_log_pipe)) {
barf_perror("pipe");
}
init_pipe(reopen_log_pipe);
/* Setup the database */
setup_structure();
......@@ -1925,16 +1865,8 @@ int main(int argc, char *argv[])
}
/* redirect to /dev/null now we're ready to accept connections */
if (dofork) {
int devnull = open("/dev/null", O_RDWR);
if (devnull == -1)
barf_perror("Could not open /dev/null\n");
dup2(devnull, STDIN_FILENO);
dup2(devnull, STDOUT_FILENO);
dup2(devnull, STDERR_FILENO);
close(devnull);
xprintf = trace;
}
if (dofork)
finish_daemonize();
signal(SIGHUP, trigger_reopen_log);
......@@ -1957,7 +1889,7 @@ int main(int argc, char *argv[])
barf_perror("Select failed");
}
if (FD_ISSET(reopen_log_pipe[0], &inset)) {
if (reopen_log_pipe[0] != -1 && FD_ISSET(reopen_log_pipe[0], &inset)) {
char c;
if (read(reopen_log_pipe[0], &c, 1) != 1)
barf_perror("read failed");
......
......@@ -171,6 +171,7 @@ extern int event_fd;
/* Map the kernel's xenstore page. */
void *xenbus_map(void);
void unmap_xenbus(void *interface);
/* Return the event channel used by xenbus. */
evtchn_port_t xenbus_evtchn(void);
......@@ -178,6 +179,17 @@ evtchn_port_t xenbus_evtchn(void);
/* Tell the kernel xenstored is running. */
void xenbus_notify_running(void);
/* Write out the pidfile */
void write_pidfile(const char *pidfile);
/* Fork but do not close terminal FDs */
void daemonize(void);
/* Close stdin/stdout/stderr to complete daemonize */
void finish_daemonize(void);
/* Open a pipe for signal handling */
void init_pipe(int reopen_log_pipe[2]);
#endif /* _XENSTORED_CORE_H */
/*
......
......@@ -200,7 +200,7 @@ static int destroy_domain(void *_domain)
/* Domain 0 was mapped by dom0_init, so it must be unmapped
using munmap() and not the grant unmap call. */
if (domain->domid == 0)
munmap(domain->interface, getpagesize());
unmap_xenbus(domain->interface);
else
unmap_interface(domain->interface);
}
......
/*
Simple prototype Xen Store Daemon providing simple tree-like database.
Copyright (C) 2005 Rusty Russell IBM Corporation
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; either version 2 of the License, or
(at your option) any later version.
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/mman.h>
#include <xenctrl.h>
#include "xenstored_core.h"
#include <xen/grant_table.h>
void write_pidfile(const char *pidfile)
{
}
void daemonize(void)
{
}
void finish_daemonize(void)
{
}
void init_pipe(int reopen_log_pipe[2])
{
reopen_log_pipe[0] = -1;
reopen_log_pipe[1] = -1;
}
void xenbus_notify_running(void)
{
}
evtchn_port_t xenbus_evtchn(void)
{
return -1;
}
void *xenbus_map(void)
{
return NULL;
}
void unmap_xenbus(void *interface)
{
}
/*
Simple prototype Xen Store Daemon providing simple tree-like database.
Copyright (C) 2005 Rusty Russell IBM Corporation
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; either version 2 of the License, or
(at your option) any later version.
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 <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include "utils.h"
#include "xenstored_core.h"
void write_pidfile(const char *pidfile)
{
char buf[100];
int len;
int fd;
fd = open(pidfile, O_RDWR | O_CREAT, 0600);
if (fd == -1)
barf_perror("Opening pid file %s", pidfile);
/* We exit silently if daemon already running. */
if (lockf(fd, F_TLOCK, 0) == -1)
exit(0);
len = snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());
if (write(fd, buf, len) != len)
barf_perror("Writing pid file %s", pidfile);
}
/* Stevens. */
void daemonize(void)
{
pid_t pid;
/* Separate from our parent via fork, so init inherits us. */
if ((pid = fork()) < 0)
barf_perror("Failed to fork daemon");
if (pid != 0)
exit(0);
/* Session leader so ^C doesn't whack us. */
setsid();
/* Let session leader exit so child cannot regain CTTY */
if ((pid = fork()) < 0)
barf_perror("Failed to fork daemon");
if (pid != 0)
exit(0);
/* Move off any mount points we might be in. */
if (chdir("/") == -1)
barf_perror("Failed to chdir");
/* Discard our parent's old-fashioned umask prejudices. */
umask(0);
}
void finish_daemonize(void)
{
int devnull = open("/dev/null", O_RDWR);
if (devnull == -1)
barf_perror("Could not open /dev/null\n");
dup2(devnull, STDIN_FILENO);
dup2(devnull, STDOUT_FILENO);
dup2(devnull, STDERR_FILENO);
close(devnull);
xprintf = trace;
}
void init_pipe(int reopen_log_pipe[2])
{
if (pipe(reopen_log_pipe)) {
barf_perror("pipe");
}
}
void unmap_xenbus(void *interface)
{
munmap(interface, getpagesize());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment