1 /*\
2  *  Copyright (C) International Business Machines  Corp., 2005
3  *  Author(s): Anthony Liguori <aliguori@us.ibm.com>
4  *
5  *  Xen Console Daemon
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; under version 2 of the License.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; If not, see <http://www.gnu.org/licenses/>.
18 \*/
19 
20 #include <getopt.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include <signal.h>
27 #include <sys/types.h>
28 #include <sys/resource.h>
29 
30 #include "xenctrl.h"
31 
32 #include "utils.h"
33 #include "io.h"
34 #include "_paths.h"
35 
36 int log_reload = 0;
37 int log_guest = 0;
38 int log_hv = 0;
39 int log_time_hv = 0;
40 int log_time_guest = 0;
41 char *log_dir = NULL;
42 int discard_overflowed_data = 1;
43 
handle_hup(int sig)44 static void handle_hup(int sig)
45 {
46         log_reload = 1;
47 }
48 
usage(char * name)49 static void usage(char *name)
50 {
51 	printf("Usage: %s [-h] [-V] [-v] [-i] [--log=none|guest|hv|all] [--log-dir=DIR] [--pid-file=PATH] [-t, --timestamp=none|guest|hv|all] [-o, --overflow-data=discard|keep]\n", name);
52 }
53 
version(char * name)54 static void version(char *name)
55 {
56 	printf("Xen Console Daemon 3.0\n");
57 }
58 
increase_fd_limit(void)59 static void increase_fd_limit(void)
60 {
61 	/*
62 	 * We require many file descriptors:
63 	 * - per domain: pty master, pty slave, logfile and evtchn
64 	 * - misc extra: hypervisor log, privcmd, gntdev, std...
65 	 *
66 	 * Allow a generous 1000 for misc, and calculate the maximum possible
67 	 * number of fds which could be used.
68 	 */
69 	unsigned min_fds = (DOMID_FIRST_RESERVED * 4) + 1000;
70 	struct rlimit lim, new = { min_fds, min_fds };
71 
72 	if (getrlimit(RLIMIT_NOFILE, &lim) < 0) {
73 		fprintf(stderr, "Failed to obtain fd limit: %s\n",
74 			strerror(errno));
75 		exit(1);
76 	}
77 
78 	/* Do we already have sufficient? Great! */
79 	if (lim.rlim_cur >= min_fds)
80 		return;
81 
82 	/* Try to increase our limit. */
83 	if (setrlimit(RLIMIT_NOFILE, &new) < 0)
84 		syslog(LOG_WARNING,
85 		       "Unable to increase fd limit from {%llu, %llu} to "
86 		       "{%llu, %llu}: (%s) - May run out with lots of domains",
87 		       (unsigned long long)lim.rlim_cur,
88 		       (unsigned long long)lim.rlim_max,
89 		       (unsigned long long)new.rlim_cur,
90 		       (unsigned long long)new.rlim_max,
91 		       strerror(errno));
92 }
93 
main(int argc,char ** argv)94 int main(int argc, char **argv)
95 {
96 	const char *sopts = "hVvit:o:";
97 	struct option lopts[] = {
98 		{ "help", 0, 0, 'h' },
99 		{ "version", 0, 0, 'V' },
100 		{ "verbose", 0, 0, 'v' },
101 		{ "interactive", 0, 0, 'i' },
102 		{ "log", 1, 0, 'l' },
103 		{ "log-dir", 1, 0, 'r' },
104 		{ "pid-file", 1, 0, 'p' },
105 		{ "timestamp", 1, 0, 't' },
106 		{ "overflow-data", 1, 0, 'o'},
107 		{ 0 },
108 	};
109 	bool is_interactive = false;
110 	int ch;
111 	int syslog_option = LOG_CONS;
112 	int syslog_mask = LOG_MASK(LOG_WARNING)|LOG_MASK(LOG_ERR)|LOG_MASK(LOG_CRIT)|\
113 		          LOG_MASK(LOG_ALERT)|LOG_MASK(LOG_EMERG);
114 	int opt_ind = 0;
115 	char *pidfile = NULL;
116 
117 	while ((ch = getopt_long(argc, argv, sopts, lopts, &opt_ind)) != -1) {
118 		switch (ch) {
119 		case 'h':
120 			usage(argv[0]);
121 			exit(0);
122 		case 'V':
123 			version(argv[0]);
124 			exit(0);
125 		case 'v':
126 #ifndef __sun__
127 			syslog_option |= LOG_PERROR;
128 #endif
129 			syslog_mask |= LOG_MASK(LOG_NOTICE)|LOG_MASK(LOG_INFO)| \
130 				      LOG_MASK(LOG_DEBUG);
131 			break;
132 		case 'i':
133 			is_interactive = true;
134 			break;
135 		case 'l':
136 		        if (!strcmp(optarg, "all")) {
137 			      log_hv = 1;
138 			      log_guest = 1;
139 			} else if (!strcmp(optarg, "hv")) {
140 			      log_hv = 1;
141 			} else if (!strcmp(optarg, "guest")) {
142 			      log_guest = 1;
143 			}
144 			break;
145 		case 'r':
146 		        log_dir = strdup(optarg);
147 			break;
148 		case 'p':
149 		        pidfile = strdup(optarg);
150 			break;
151 		case 't':
152 			if (!strcmp(optarg, "all")) {
153 				log_time_hv = 1;
154 				log_time_guest = 1;
155 			} else if (!strcmp(optarg, "hv")) {
156 				log_time_hv = 1;
157 			} else if (!strcmp(optarg, "guest")) {
158 				log_time_guest = 1;
159 			} else if (!strcmp(optarg, "none")) {
160 				log_time_guest = 0;
161 				log_time_hv = 0;
162 			}
163 			break;
164 		case 'o':
165 			if (!strcmp(optarg, "keep")) {
166 				discard_overflowed_data = 0;
167 			} else if (!strcmp(optarg, "discard")) {
168 				discard_overflowed_data = 1;
169 			}
170 			break;
171 		case '?':
172 			fprintf(stderr,
173 				"Try `%s --help' for more information\n",
174 				argv[0]);
175 			exit(EINVAL);
176 		}
177 	}
178 
179 	if (!log_dir) {
180 		log_dir = strdup(XEN_LOG_DIR "/console");
181 	}
182 
183 	if (geteuid() != 0) {
184 		fprintf(stderr, "%s requires root to run.\n", argv[0]);
185 		exit(EPERM);
186 	}
187 
188 	signal(SIGHUP, handle_hup);
189 
190 	openlog("xenconsoled", syslog_option, LOG_DAEMON);
191 	setlogmask(syslog_mask);
192 
193 	increase_fd_limit();
194 
195 	if (!is_interactive) {
196 		daemonize(pidfile ? pidfile : XEN_RUN_DIR "/xenconsoled.pid");
197 	}
198 
199 	if (!xen_setup())
200 		exit(1);
201 
202 	handle_io();
203 
204 	closelog();
205 	free(log_dir);
206 	free(pidfile);
207 
208 	return 0;
209 }
210 
211 /*
212  * Local variables:
213  *  c-file-style: "linux"
214  *  indent-tabs-mode: t
215  *  c-indent-level: 8
216  *  c-basic-offset: 8
217  *  tab-width: 8
218  * End:
219  */
220