File: main.cxx

package info (click to toggle)
systemtap 5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,556 kB
  • sloc: cpp: 81,117; ansic: 54,933; xml: 49,795; exp: 43,595; sh: 11,526; python: 5,003; perl: 2,252; tcl: 1,312; makefile: 1,006; javascript: 149; lisp: 105; awk: 101; asm: 91; java: 70; sed: 16
file content (200 lines) | stat: -rw-r--r-- 4,953 bytes parent folder | download
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// systemtap compile-server web api server
// Copyright (C) 2017-2018 Red Hat Inc.
//
// This file is part of systemtap, and is free software.  You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.

#include "server.h"
#include "api.h"
#include <iostream>
#include "../staputil.h"
#include "nss_funcs.h"
#include "utils.h"
#include "../nsscommon.h"

extern "C" {
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/signalfd.h>
#include <getopt.h>
}

server *httpd = NULL;

static void *
signal_thread(void *arg)
{
    int signal_fd = (int)(long)arg;

    while (1) {
	struct signalfd_siginfo si;
	ssize_t s;

	s = read(signal_fd, &si, sizeof(si));
	if (s != sizeof(si)) {
	    server_error(_F("signal fd read error: %s", strerror(errno)));
	    continue;
	}

	// FIXME: we might think about using SIGHUP to aks us to
	// re-read configuration data.
	if (si.ssi_signo == SIGINT || si.ssi_signo == SIGTERM
	    || si.ssi_signo == SIGHUP || si.ssi_signo == SIGQUIT) {

	    // Since we're using signalfd(), we can call code that
	    // isn't signal-safe (like server::stop).
	    if (httpd)
		httpd->stop();
	    break;
	}
	else if (si.ssi_signo == SIGCHLD) {
	    // We just ignore SIGCHLD. We need to keep it enabled for
	    // waitpid() to work properly.
	}
	else {
	    server_error(_F("Got unhandled signal %d", si.ssi_signo));
	}
    }
    close(signal_fd);
    return NULL;
}

static void
setup_main_signals(pthread_t *tid)
{
    static sigset_t s;

    /* Block several signals; other threads created by main() will
     * inherit a copy of the signal mask. */
    sigemptyset(&s);
    sigaddset(&s, SIGINT);
    sigaddset(&s, SIGTERM);
    sigaddset(&s, SIGHUP);
    sigaddset(&s, SIGQUIT);
    sigaddset(&s, SIGCHLD);
    pthread_sigmask(SIG_BLOCK, &s, NULL);

    /* Create a signalfd. This way we can synchronously handle the
     * signals. */
    int signal_fd = signalfd(-1, &s, SFD_CLOEXEC);
    if (signal_fd < 0) {
	server_error(_F("Failed to create signal file descriptor: %s",
			strerror(errno)));
	exit(1);
    }

    /* Let the special signal thread handle signals. */
    if (pthread_create(tid, NULL, signal_thread, (void *)(long)signal_fd) < 0) {
	server_error(_F("Failed to create thread: %s", strerror(errno)));
	exit(1);
    }
}

// FIXME: the default port of 1234 was just chosen at random. A better
// port default needs to be chosen.
static uint16_t port = 1234;
static string cert_db_path;

void
parse_cmdline(int argc, char *const argv[])
{
    enum {
	LONG_OPT_PORT = 256,
	LONG_OPT_SSL,
	LONG_OPT_LOG,
    };
    static struct option long_options[] = {
        { "port", 1, NULL, LONG_OPT_PORT },
        { "ssl", 1, NULL, LONG_OPT_SSL },
        { "log", 1, NULL, LONG_OPT_LOG },
        { NULL, 0, NULL, 0 }
    };
    while (true) {
	int grc = getopt_long(argc, argv, "", long_options, NULL);
	char *num_endptr;
	unsigned long port_tmp;
	if (grc < 0)
	    break;
	switch (grc) {
	  case LONG_OPT_PORT:
	    errno = 0;
	    port_tmp = strtoul(optarg, &num_endptr, 10);
	    if (*num_endptr != '\0') {
		server_error(_F("%s: cannot parse number '--port=%s'", argv[0],
				optarg));
		exit(1);
	    }
	    if (errno != 0 || port_tmp > 65535) {
		server_error(_F("%s: invalid entry: port must be between"
				" 0 and 65535 '--port=%s'", argv[0], optarg));
		exit(1);
	    }
	    port = (uint16_t)port_tmp;
	    break;
	  case LONG_OPT_SSL:
	    cert_db_path = optarg;
	    break;
	  case LONG_OPT_LOG:
	    start_log(optarg, true);
	    break;
	  default:
	    break;
	}
    }
    for (int i = optind; i < argc; i++) {
	server_error(_F("%s: unrecognized argument '%s'", argv[0], argv[i]));
    }
}

int
main(int argc, char *const argv[])
{
    pthread_t tid;
    
    // Get rid of a few standard environment variables (which might
    // cause us to do unintended things).
    if (unsetenv("IFS") || unsetenv("CDPATH") || unsetenv("ENV")
	|| unsetenv("BASH_ENV")) {
	server_error(_F("unsetenv failed: %s", strerror(errno)));
	return 1;
    }

    // Make sure we're not root. Since we connect directly to the
    // internet, we can't (safely) run as root.
    if (geteuid () == 0) {
	server_error("For security reasons, invocation of stap-httpd as"
		     " root is not supported.");
	return 1;
    }

    setup_main_signals(&tid);

    parse_cmdline(argc, argv);

    // Initialize NSS.
    if (nss_init(cert_db_path) != 0)
      return 1;

    // Create the server and ask the api to register its handlers.
    httpd = new server(port, cert_db_path);
    api_add_request_handlers(*httpd);

    // Wait for the server to shut itself down.
    httpd->wait();
    delete httpd;

    // Clean up the signal thread.
    pthread_join(tid, NULL);

    // Ask the api to do its cleanup.
    api_cleanup();

    // Shutdown NSS.
    nss_shutdown(cert_db_path);

    end_log();
    return 0;
}