Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit a9aa2f7

Browse files
authored
Merge pull request #221 from dlespiau/20161011-daemon
"Daemon" mode
2 parents 6e1379b + 0611644 commit a9aa2f7

File tree

7 files changed

+55
-15
lines changed

7 files changed

+55
-15
lines changed

.gitignore

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
.deps
1010
TAGS
1111
tags
12-
init
12+
/src/hyperstart
1313
build/hyper_daemon
1414
build/hyper-initrd.img
15+
data/hyperstart.service
1516
build/cbfs.rom
1617
build/root/*
1718
build/*iso
@@ -28,10 +29,7 @@ cscope.po.out
2829
stamp-h
2930
stamp-h.in
3031
stamp-h1
31-
/config.h
32-
/config.h.in
33-
/config.log
34-
/config.status
32+
/config.*
3533
/configure
3634
/aclocal.m4
3735
/autoscan.log

Makefile.am

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ cbfs-local:
66
@echo finish make cbfs
77
kernel-local:
88
@echo finish make kernel
9+
10+
if SYSTEMD
11+
SYSTEMD_SYS_UNITdir = @SYSTEMD_SYSTEMUNIT@
12+
SYSTEMD_SYS_UNIT_DATA = data/hyperstart.service
13+
endif

build/make-initrd.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mkdir -m 0755 -p /tmp/hyperstart-rootfs/dev \
1111
/tmp/hyperstart-rootfs/bin \
1212
/tmp/hyperstart-rootfs/proc
1313

14-
cp ../src/init /tmp/hyperstart-rootfs/
14+
cp ../src/hyperstart /tmp/hyperstart-rootfs/init
1515
cp busybox /tmp/hyperstart-rootfs/sbin/
1616
cp iptables /tmp/hyperstart-rootfs/sbin/
1717
cp ipvsadm /tmp/hyperstart-rootfs/sbin/

configure.ac

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ AC_CONFIG_HEADERS([config.h])
1111
# Checks for programs.
1212
AC_PROG_CC
1313
AM_PROG_CC_C_O
14+
AC_CHECK_PROG(PKG_CONFIG, [pkg-config], [pkg-config], [:])
15+
1416
# Checks for libraries.
1517

1618
# Checks for header files.
@@ -60,10 +62,22 @@ fi
6062

6163
AM_CONDITIONAL([WITH_VBOX], [test "x$with_vbox" != "xno"])
6264

65+
AC_ARG_WITH([systemdsystemunitdir],
66+
AS_HELP_STRING([--with-systemdsystemunitdir=SYSTEMD_SYSTEM_UNIT_DIR],
67+
[path to install systemd system service]),
68+
[SYSTEMD_SYSTEMUNIT=${withval}],
69+
[SYSTEMD_SYSTEMUNIT="`$PKG_CONFIG --variable=systemdsystemunitdir systemd 2>/dev/null`"])
70+
71+
AS_IF([test -z "$SYSTEMD_SYSTEMUNIT"],[SYSTEMD_SYSTEMUNIT=no])
72+
AS_IF([test "x$enable_daemon" = "xno"],[SYSTEMD_SYSTEMUNIT=no])
73+
AC_SUBST(SYSTEMD_SYSTEMUNIT)
74+
AM_CONDITIONAL(SYSTEMD, test "x${SYSTEMD_SYSTEMUNIT}" != "xno" )
75+
6376
AC_CONFIG_FILES([
6477
Makefile
6578
src/Makefile
6679
build/Makefile
80+
data/hyperstart.service
6781
])
6882
AC_OUTPUT
6983

@@ -79,4 +93,5 @@ AC_MSG_RESULT([
7993
suid cflags: ${SUID_CFLAGS}
8094
ldflags: ${LDFLAGS}
8195
suid ldflags: ${SUID_LDFLAGS}
96+
systemd unit path: ${SYSTEMD_SYSTEMUNIT}
8297
])

data/hyperstart.service.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[Unit]
2+
Description=hyperstart guest daemon
3+
4+
[Service]
5+
ExecStart=@prefix@/bin/hyperstart

src/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
AM_CFLAGS = -Wall -Werror
2-
bin_PROGRAMS=init
3-
init_SOURCES=init.c jsmn.c net.c util.c parse.c parson.c container.c exec.c event.c portmapping.c
2+
bin_PROGRAMS=hyperstart
3+
hyperstart_SOURCES=init.c jsmn.c net.c util.c parse.c parson.c container.c exec.c event.c portmapping.c

src/init.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#define _GNU_SOURCE
2+
#include <stdbool.h>
23
#include <stdio.h>
34
#include <stdlib.h>
45
#include <string.h>
@@ -1341,10 +1342,9 @@ static int hyper_loop(void)
13411342
return 0;
13421343
}
13431344

1344-
int main(int argc, char *argv[])
1345+
static int hyper_setup_init_process(void)
13451346
{
1346-
char *cmdline, *ctl_serial, *tty_serial;
1347-
1347+
/* mount the base file systems */
13481348
if (mount("proc", "/proc", "proc", MS_NOSUID| MS_NODEV| MS_NOEXEC, NULL) == -1) {
13491349
perror("mount proc failed");
13501350
return -1;
@@ -1381,12 +1381,31 @@ int main(int argc, char *argv[])
13811381
return -1;
13821382
}
13831383

1384-
cmdline = read_cmdline();
1385-
1384+
/* become the session leader */
13861385
setsid();
13871386

1387+
/* set the controlling terminal */
13881388
ioctl(STDIN_FILENO, TIOCSCTTY, 1);
13891389

1390+
setenv("PATH", "/bin:/sbin/:/usr/bin/:/usr/sbin/", 1);
1391+
1392+
return 0;
1393+
}
1394+
1395+
int main(int argc, char *argv[])
1396+
{
1397+
char *binary_name, *cmdline, *ctl_serial, *tty_serial;
1398+
bool is_init;
1399+
1400+
binary_name = basename(argv[0]);
1401+
is_init = strncmp(binary_name, "init", 5) == 0;
1402+
1403+
if (is_init && hyper_setup_init_process() < 0) {
1404+
return -1;
1405+
}
1406+
1407+
cmdline = read_cmdline();
1408+
13901409
#ifdef WITH_VBOX
13911410
ctl_serial = "/dev/ttyS0";
13921411
tty_serial = "/dev/ttyS1";
@@ -1401,8 +1420,6 @@ int main(int argc, char *argv[])
14011420
tty_serial = "sh.hyper.channel.1";
14021421
#endif
14031422

1404-
setenv("PATH", "/bin:/sbin/:/usr/bin/:/usr/sbin/", 1);
1405-
14061423
hyper_epoll.ctl.fd = hyper_setup_ctl_channel(ctl_serial);
14071424
if (hyper_epoll.ctl.fd < 0) {
14081425
fprintf(stderr, "fail to setup hyper control serial port\n");

0 commit comments

Comments
 (0)