summaryrefslogtreecommitdiff
path: root/src/monitor.c
blob: a7ddbe12b4eb30aafd8d453469f59303eaa799d5 (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
/**
 * @file
 * @section AUTHORS
 *
 *  Authors:
 *       Eamon Walsh <ewalsh@tycho.nsa.gov>
 *
 * @section LICENSE
 *
 * This file is in the public domain.
 *
 * @section DESCRIPTION
 *
 * This is the main routine for the linpicker-monitor program.
 * Nothing much here except initialization, select loop, and teardown.
 */

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>

#include "common.h"
#include "fd.h"
#include "xen_backend.h"
#include "sys-queue.h"

extern int xenfb_monitor_init(void);

static int timetodie;

/*
 * Is called in case of process signals.
 */
static void
monitor_sighandler(int sig)
{
    timetodie = 1;
}

static int
monitor_fork(void)
{
    int pipefd[2];
    pid_t pid;
    char tmp;

    if (chdir("/") < 0)
	exit(1);
    if (pipe(pipefd) < 0)
	exit(1);

    pid = fork();

    if (pid < 0)
	exit(5);
    if (pid > 0) {
	close(pipefd[1]);
	while (read(pipefd[0], &tmp, 1) != 0);
	exit(0);
    }

    close(pipefd[0]);
    return pipefd[1];
}

/* 
 * Main
 */
int main (int argc, char *argv[])
{
    struct sigaction sa = { .sa_handler = monitor_sighandler };
    int writefd;

    /* Fork and wait in the parent */
    writefd = monitor_fork();

    /* Set up sighandlers */
    sigaction(SIGINT, &sa, NULL);
    sigaction(SIGTERM, &sa, NULL);

    /* Open logfile */
    if (fd_open_logfile(MONITOR_LOG) < 0)
	return 1;

    /* Initialize subsystems */
    if (xen_be_init() < 0)
	return 1;
    if (xenfb_monitor_init() < 0)
	return 2;

    /* Signal the parent process to exit */
    close(writefd);

    /* Enter select loop */
    while (!timetodie && fd_run_select() == 0);

    /* Shut down */
    fd_shutdown();
    return 0;
}