xref: /illumos-gate/usr/src/cmd/drd/drd.c (revision 0173c38a73f34277e0c97a19fedfd25d81ba8380)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * sun4v DR daemon
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <libgen.h>
41 #include <syslog.h>
42 #include <door.h>
43 #include <assert.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 
47 #include <sys/drctl_impl.h>
48 #include <sys/drctl.h>
49 #include "drd.h"
50 
51 boolean_t drd_debug = B_FALSE;
52 boolean_t drd_daemonized = B_FALSE;
53 
54 #define	DRD_DOOR_FILE		"/tmp/drd_door"
55 #define	DRD_DOOR_RETURN_ERR()	(void) door_return(NULL, 0, NULL, 0)
56 
57 static char *cmdname;
58 static int drctl_fd;
59 static drctl_rsrc_t *drd_result = NULL;
60 
61 /*
62  * Currently, the only supported backend is for the Reconfiguration
63  * Coordination Manager (RCM). When there are other backends, this
64  * variable should be set dynamically.
65  */
66 static drd_backend_t *drd_backend = &drd_rcm_backend;
67 
68 static void drd_daemonize(void);
69 static int drd_init_drctl_dev(boolean_t standalone);
70 static int drd_init_door_server(boolean_t standalone);
71 static void drd_door_server(void *, char *, size_t, door_desc_t *, uint_t);
72 
73 int
74 main(int argc, char **argv)
75 {
76 	int		opt;
77 	boolean_t	standalone = 0;
78 
79 	cmdname = basename(argv[0]);
80 
81 	/*
82 	 * Process command line arguments
83 	 */
84 	opterr = 0;	/* disable getopt error messages */
85 	while ((opt = getopt(argc, argv, "ds")) != EOF) {
86 
87 		switch (opt) {
88 		case 'd':
89 			drd_debug = B_TRUE;
90 			break;
91 		case 's':
92 			standalone = B_TRUE;
93 			break;
94 		default:
95 			drd_err("unkown option: -%c", optopt);
96 			exit(1);
97 		}
98 	}
99 
100 	drd_dbg("initializing %s...", cmdname);
101 
102 	/* must be root */
103 	if (geteuid() != 0) {
104 		drd_err("permission denied: must run as root");
105 		exit(1);
106 	}
107 
108 	/* open the drctl device */
109 	if (drd_init_drctl_dev(standalone) != 0) {
110 		drd_err("unable to initialize drctl device");
111 		exit(1);
112 	}
113 
114 	/* daemonize */
115 	if (!standalone) {
116 		drd_daemonize();
117 	}
118 
119 	/* initialize door server */
120 	if (drd_init_door_server(standalone) != 0) {
121 		drd_err("unable to initialize door server");
122 		exit(1);
123 	}
124 
125 	/* initialize the backend */
126 	if ((*drd_backend->init)() != 0) {
127 		drd_err("unable to initialize backend processor");
128 		exit(1);
129 	}
130 
131 	/* loop forever */
132 	for (;;) {
133 		pause();
134 	}
135 
136 	/*NOTREACHED*/
137 	return (0);
138 }
139 
140 static void
141 drd_daemonize(void)
142 {
143 	pid_t	pid;
144 
145 	if ((pid = fork()) == -1) {
146 		drd_err("failed to fork: %s", strerror(errno));
147 		exit(1);
148 	}
149 
150 	if (pid != 0) {
151 		/* parent */
152 		exit(0);
153 	}
154 
155 	/*
156 	 * Initialize child process
157 	 */
158 	(void) setsid();
159 	(void) chdir("/");
160 	(void) umask(0);
161 
162 	/*
163 	 * Initialize file descriptors
164 	 */
165 	assert(drctl_fd == (STDERR_FILENO + 1));
166 
167 	(void) close(STDIN_FILENO);
168 	(void) open("/dev/null", O_RDWR);
169 	(void) dup2(STDIN_FILENO, STDOUT_FILENO);
170 	(void) dup2(STDIN_FILENO, STDERR_FILENO);
171 
172 	closefrom(drctl_fd + 1);
173 
174 	/* initialize logging */
175 	openlog(cmdname, LOG_CONS | LOG_NDELAY, LOG_DAEMON);
176 
177 	drd_daemonized = B_TRUE;
178 }
179 
180 static int
181 drd_init_drctl_dev(boolean_t standalone)
182 {
183 	void (*drd_output)(char *, ...);
184 
185 	drd_output = (standalone) ? drd_info : drd_err;
186 
187 	/* open the drctl device */
188 	if ((drctl_fd = open(DRCTL_DEV, O_RDWR)) == -1) {
189 		drd_output("open %s failed: %s", DRCTL_DEV, strerror(errno));
190 		return ((standalone) ? 0 : -1);
191 	}
192 
193 	return (0);
194 }
195 
196 static int
197 drd_init_door_server(boolean_t standalone)
198 {
199 	int		door_fd;
200 	int		dbg_fd;
201 	drctl_setup_t	setup;
202 
203 	assert((drctl_fd != -1) || standalone);
204 
205 	/* create the door */
206 	if ((door_fd = door_create(drd_door_server, NULL, 0)) == -1) {
207 		drd_err("door_create failed: %s", strerror(errno));
208 		return (-1);
209 	}
210 
211 	if (drctl_fd != -1) {
212 
213 		setup.did = door_fd;
214 
215 		/* send the door descriptor to drctl */
216 		if (ioctl(drctl_fd, DRCTL_IOCTL_CONNECT_SERVER, &setup) == -1) {
217 			drd_err("drctl ioctl failed: %s", strerror(errno));
218 			(void) door_revoke(door_fd);
219 			return (-1);
220 		}
221 
222 		drd_dbg("connection to drctl established");
223 
224 		/* setup is complete in daemon mode */
225 		if (!standalone) {
226 			return (0);
227 		}
228 	}
229 
230 	/*
231 	 * At this point, the daemon is running in standalone
232 	 * mode for testing purposes. This allows the daemon
233 	 * to be controlled directly through a door exported
234 	 * to the filesystem. No drctl device is required in
235 	 * this mode.
236 	 */
237 
238 	/* create the door file */
239 	unlink(DRD_DOOR_FILE);
240 	if ((dbg_fd = creat(DRD_DOOR_FILE, 0644)) == -1) {
241 		drd_err("failed to create door file '%s': %s",
242 		    DRD_DOOR_FILE, strerror(errno));
243 		(void) door_revoke(door_fd);
244 		return (-1);
245 	}
246 	close(dbg_fd);
247 
248 	/* attach the door file to the door descriptor */
249 	if (fattach(door_fd, DRD_DOOR_FILE) == -1) {
250 		drd_err("failed to fattach door file '%s': %s",
251 		    DRD_DOOR_FILE, strerror(errno));
252 		unlink(DRD_DOOR_FILE);
253 		(void) door_revoke(door_fd);
254 		return (-1);
255 	}
256 
257 	drd_dbg("door server attached to '%s'", DRD_DOOR_FILE);
258 
259 	return (0);
260 }
261 
262 static size_t
263 drd_pack_response(drctl_rsrc_t *rsrcs, int nrsrc)
264 {
265 	drctl_rsrc_t	*orsrcsp;
266 	void		*resizep;
267 	size_t		osize;
268 	char		*str;
269 	size_t		offset;
270 	char		*off;
271 	int		idx;
272 	size_t		len;
273 
274 	drd_dbg("drd_pack_response...");
275 
276 	/*
277 	 * Deallocate the global response buffer if it is
278 	 * in use. This assumes that there will only ever
279 	 * be one pending operation in the daemon. This is
280 	 * enforced by the kernel.
281 	 */
282 	s_free(drd_result);
283 
284 	orsrcsp = calloc(sizeof (*orsrcsp), nrsrc);
285 	osize = sizeof (*orsrcsp) * nrsrc;
286 	bcopy(rsrcs, orsrcsp, osize);
287 
288 	offset = osize;
289 
290 	/*
291 	 * Loop through all the resources and concatenate
292 	 * all the error strings to the end of the resource
293 	 * array. Also, update the offset field of each
294 	 * resource.
295 	 */
296 	for (idx = 0; idx < nrsrc; idx++) {
297 
298 		str = (char *)(uintptr_t)rsrcs[idx].offset;
299 
300 		/* skip if no error string */
301 		if (str == NULL)
302 			continue;
303 
304 		len = strlen(str) + 1;
305 
306 		/* increase the size of the buffer */
307 		resizep = realloc(orsrcsp, osize + len);
308 		if (resizep == NULL) {
309 			drd_err("realloc failed: %s", strerror(errno));
310 			s_free(orsrcsp);
311 
312 			/* clean up any remaining strings */
313 			while (idx < nrsrc) {
314 				str = (char *)(uintptr_t)rsrcs[idx++].offset;
315 				s_free(str);
316 			}
317 			return (0);
318 		}
319 
320 		orsrcsp = resizep;
321 
322 		/* copy the error string into the response */
323 		off = (char *)orsrcsp + offset;
324 		bcopy(str, off, len);
325 		orsrcsp[idx].offset = offset;
326 
327 		/*
328 		 * Now that the error string has been copied
329 		 * into the response message, the memory that
330 		 * was allocated for it is no longer needed.
331 		 */
332 		s_free(str);
333 		rsrcs[idx].offset = 0;
334 
335 		/* update size and offset */
336 		offset += len;
337 		osize += len;
338 	}
339 
340 	drd_result = orsrcsp;
341 	return (osize);
342 }
343 
344 /*ARGSUSED*/
345 static void
346 drd_door_server(void *cookie, char *argp, size_t arg_sz, door_desc_t *dp,
347     uint_t n_desc)
348 {
349 	drd_msg_t	*msg = (drd_msg_t *)(uintptr_t)argp;
350 	drctl_rsrc_t	*rsrcs;
351 	size_t		osize;
352 	int		nrsrc;
353 
354 	drd_dbg("drd_door_server...");
355 	drd_dbg("message received: %d bytes", arg_sz);
356 
357 	/* sanity check incoming arg */
358 	if ((argp == NULL) || (arg_sz == 0))
359 		DRD_DOOR_RETURN_ERR();
360 
361 	drd_dbg("  cmd=%d, count=%d, flags=%d", msg->cmd,
362 	    msg->count, msg->flags);
363 
364 	rsrcs = (drctl_rsrc_t *)(uintptr_t)msg->data;
365 	nrsrc = msg->count;
366 
367 	/* pass off to backend for processing */
368 	switch (msg->cmd) {
369 	case DRCTL_CPU_CONFIG_REQUEST:
370 		(*drd_backend->cpu_config_request)(rsrcs, nrsrc);
371 		break;
372 
373 	case DRCTL_CPU_CONFIG_NOTIFY:
374 		(*drd_backend->cpu_config_notify)(rsrcs, nrsrc);
375 		break;
376 
377 	case DRCTL_CPU_UNCONFIG_REQUEST:
378 		(*drd_backend->cpu_unconfig_request)(rsrcs, nrsrc);
379 		break;
380 
381 	case DRCTL_CPU_UNCONFIG_NOTIFY:
382 		(*drd_backend->cpu_unconfig_notify)(rsrcs, nrsrc);
383 		break;
384 
385 	case DRCTL_MEM_CONFIG_REQUEST:
386 	case DRCTL_MEM_CONFIG_NOTIFY:
387 	case DRCTL_MEM_UNCONFIG_REQUEST:
388 	case DRCTL_MEM_UNCONFIG_NOTIFY:
389 		drd_err("memory DR operations not supported yet");
390 		DRD_DOOR_RETURN_ERR();
391 		break;
392 
393 	case DRCTL_IO_CONFIG_REQUEST:
394 	case DRCTL_IO_CONFIG_NOTIFY:
395 	case DRCTL_IO_UNCONFIG_REQUEST:
396 	case DRCTL_IO_UNCONFIG_NOTIFY:
397 		drd_err("I/O DR operations not supported yet");
398 		DRD_DOOR_RETURN_ERR();
399 		break;
400 
401 	default:
402 		drd_err("unknown command: %d", msg->cmd);
403 		DRD_DOOR_RETURN_ERR();
404 		break;
405 	}
406 
407 	osize = drd_pack_response(rsrcs, nrsrc);
408 	if (osize == 0)
409 		DRD_DOOR_RETURN_ERR();
410 
411 	(void) door_return((char *)drd_result, osize, NULL, 0);
412 }
413