dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / drd / drd.c
blob39b7e4c2a1b2f0efeb2f4e0342f7d05fd83550cc
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * sun4v DR daemon
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <strings.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <libgen.h>
39 #include <syslog.h>
40 #include <door.h>
41 #include <assert.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
45 #include <sys/drctl_impl.h>
46 #include <sys/drctl.h>
47 #include "drd.h"
49 boolean_t drd_debug = B_FALSE;
50 boolean_t drd_daemonized = B_FALSE;
52 #define DRD_DOOR_FILE "/tmp/drd_door"
53 #define DRD_DOOR_RETURN_ERR() (void) door_return(NULL, 0, NULL, 0)
55 static char *cmdname;
56 static int drctl_fd;
57 static drctl_rsrc_t *drd_result = NULL;
60 * Currently, the only supported backend is for the Reconfiguration
61 * Coordination Manager (RCM). When there are other backends, this
62 * variable should be set dynamically.
64 static drd_backend_t *drd_backend = &drd_rcm_backend;
66 static void drd_daemonize(void);
67 static int drd_init_drctl_dev(boolean_t standalone);
68 static int drd_init_door_server(boolean_t standalone);
69 static void drd_door_server(void *, char *, size_t, door_desc_t *, uint_t);
71 int
72 main(int argc, char **argv)
74 int opt;
75 boolean_t standalone = B_FALSE;
77 cmdname = basename(argv[0]);
80 * Process command line arguments
82 opterr = 0; /* disable getopt error messages */
83 while ((opt = getopt(argc, argv, "ds")) != EOF) {
85 switch (opt) {
86 case 'd':
87 drd_debug = B_TRUE;
88 break;
89 case 's':
90 standalone = B_TRUE;
91 break;
92 default:
93 drd_err("unkown option: -%c", optopt);
94 exit(1);
98 drd_dbg("initializing %s...", cmdname);
100 /* must be root */
101 if (geteuid() != 0) {
102 drd_err("permission denied: must run as root");
103 exit(1);
106 /* open the drctl device */
107 if (drd_init_drctl_dev(standalone) != 0) {
108 drd_err("unable to initialize drctl device");
109 exit(1);
112 /* daemonize */
113 if (!standalone) {
114 drd_daemonize();
117 /* initialize door server */
118 if (drd_init_door_server(standalone) != 0) {
119 drd_err("unable to initialize door server");
120 exit(1);
123 /* initialize the backend */
124 if ((*drd_backend->init)() != 0) {
125 drd_err("unable to initialize backend processor");
126 exit(1);
129 /* loop forever */
130 for (;;) {
131 pause();
134 /*NOTREACHED*/
135 return (0);
138 static void
139 drd_daemonize(void)
141 pid_t pid;
143 if ((pid = fork()) == -1) {
144 drd_err("failed to fork: %s", strerror(errno));
145 exit(1);
148 if (pid != 0) {
149 /* parent */
150 exit(0);
154 * Initialize child process
156 (void) setsid();
157 (void) chdir("/");
158 (void) umask(0);
161 * Initialize file descriptors. Do not touch stderr
162 * which is initialized by SMF to point to the drd
163 * specific log file.
165 assert(drctl_fd == (STDERR_FILENO + 1));
167 (void) close(STDIN_FILENO);
168 (void) open("/dev/null", O_RDWR);
169 (void) dup2(STDIN_FILENO, STDOUT_FILENO);
171 closefrom(drctl_fd + 1);
173 /* initialize logging */
174 openlog(cmdname, LOG_CONS | LOG_NDELAY, LOG_DAEMON);
176 drd_daemonized = B_TRUE;
179 static int
180 drd_init_drctl_dev(boolean_t standalone)
182 void (*drd_output)(char *, ...);
184 drd_output = (standalone) ? drd_info : drd_err;
186 /* open the drctl device */
187 if ((drctl_fd = open(DRCTL_DEV, O_RDWR)) == -1) {
188 drd_output("open %s failed: %s", DRCTL_DEV, strerror(errno));
189 return ((standalone) ? 0 : -1);
192 return (0);
195 static int
196 drd_init_door_server(boolean_t standalone)
198 int door_fd;
199 int dbg_fd;
200 drctl_setup_t setup;
202 assert((drctl_fd != -1) || standalone);
204 /* create the door */
205 if ((door_fd = door_create(drd_door_server, NULL, 0)) == -1) {
206 drd_err("door_create failed: %s", strerror(errno));
207 return (-1);
210 if (drctl_fd != -1) {
212 setup.did = door_fd;
214 /* send the door descriptor to drctl */
215 if (ioctl(drctl_fd, DRCTL_IOCTL_CONNECT_SERVER, &setup) == -1) {
216 drd_err("drctl ioctl failed: %s", strerror(errno));
217 (void) door_revoke(door_fd);
218 return (-1);
221 drd_dbg("connection to drctl established");
223 /* setup is complete in daemon mode */
224 if (!standalone) {
225 return (0);
230 * At this point, the daemon is running in standalone
231 * mode for testing purposes. This allows the daemon
232 * to be controlled directly through a door exported
233 * to the filesystem. No drctl device is required in
234 * this mode.
237 /* create the door file */
238 unlink(DRD_DOOR_FILE);
239 if ((dbg_fd = creat(DRD_DOOR_FILE, 0644)) == -1) {
240 drd_err("failed to create door file '%s': %s",
241 DRD_DOOR_FILE, strerror(errno));
242 (void) door_revoke(door_fd);
243 return (-1);
245 close(dbg_fd);
247 /* attach the door file to the door descriptor */
248 if (fattach(door_fd, DRD_DOOR_FILE) == -1) {
249 drd_err("failed to fattach door file '%s': %s",
250 DRD_DOOR_FILE, strerror(errno));
251 unlink(DRD_DOOR_FILE);
252 (void) door_revoke(door_fd);
253 return (-1);
256 drd_dbg("door server attached to '%s'", DRD_DOOR_FILE);
258 return (0);
261 static size_t
262 drd_pack_response(drctl_rsrc_t *rsrcs, int nrsrc)
264 drctl_rsrc_t *orsrcsp;
265 void *resizep;
266 size_t osize;
267 char *str;
268 size_t offset;
269 char *off;
270 int idx;
271 size_t len;
273 drd_dbg("drd_pack_response...");
276 * Deallocate the global response buffer if it is
277 * in use. This assumes that there will only ever
278 * be one pending operation in the daemon. This is
279 * enforced by the kernel.
281 s_free(drd_result);
283 orsrcsp = calloc(sizeof (*orsrcsp), nrsrc);
284 osize = sizeof (*orsrcsp) * nrsrc;
285 bcopy(rsrcs, orsrcsp, osize);
287 offset = osize;
290 * Loop through all the resources and concatenate
291 * all the error strings to the end of the resource
292 * array. Also, update the offset field of each
293 * resource.
295 for (idx = 0; idx < nrsrc; idx++) {
297 str = (char *)(uintptr_t)rsrcs[idx].offset;
299 /* skip if no error string */
300 if (str == NULL)
301 continue;
303 len = strlen(str) + 1;
305 /* increase the size of the buffer */
306 resizep = realloc(orsrcsp, osize + len);
307 if (resizep == NULL) {
308 drd_err("realloc failed: %s", strerror(errno));
309 s_free(orsrcsp);
311 /* clean up any remaining strings */
312 while (idx < nrsrc) {
313 str = (char *)(uintptr_t)rsrcs[idx++].offset;
314 s_free(str);
316 return (0);
319 orsrcsp = resizep;
321 /* copy the error string into the response */
322 off = (char *)orsrcsp + offset;
323 bcopy(str, off, len);
324 orsrcsp[idx].offset = offset;
327 * Now that the error string has been copied
328 * into the response message, the memory that
329 * was allocated for it is no longer needed.
331 s_free(str);
332 rsrcs[idx].offset = 0;
334 /* update size and offset */
335 offset += len;
336 osize += len;
339 drd_result = orsrcsp;
340 return (osize);
343 /*ARGSUSED*/
344 static void
345 drd_door_server(void *cookie, char *argp, size_t arg_sz, door_desc_t *dp,
346 uint_t n_desc)
348 drd_msg_t *msg = (drd_msg_t *)(uintptr_t)argp;
349 drctl_rsrc_t *rsrcs;
350 size_t osize;
351 int nrsrc;
353 drd_dbg("drd_door_server...");
354 drd_dbg("message received: %d bytes", arg_sz);
356 /* sanity check incoming arg */
357 if ((argp == NULL) || (arg_sz == 0))
358 DRD_DOOR_RETURN_ERR();
360 drd_dbg(" cmd=%d, count=%d, flags=%d", msg->cmd,
361 msg->count, msg->flags);
363 rsrcs = (drctl_rsrc_t *)(uintptr_t)msg->data;
364 nrsrc = msg->count;
366 /* pass off to backend for processing */
367 switch (msg->cmd) {
368 case DRCTL_CPU_CONFIG_REQUEST:
369 (*drd_backend->cpu_config_request)(rsrcs, nrsrc);
370 break;
372 case DRCTL_CPU_CONFIG_NOTIFY:
373 (*drd_backend->cpu_config_notify)(rsrcs, nrsrc);
374 break;
376 case DRCTL_CPU_UNCONFIG_REQUEST:
377 (*drd_backend->cpu_unconfig_request)(rsrcs, nrsrc);
378 break;
380 case DRCTL_CPU_UNCONFIG_NOTIFY:
381 (*drd_backend->cpu_unconfig_notify)(rsrcs, nrsrc);
382 break;
384 case DRCTL_MEM_CONFIG_REQUEST:
385 (*drd_backend->mem_config_request)(rsrcs, nrsrc);
386 break;
388 case DRCTL_MEM_CONFIG_NOTIFY:
389 (*drd_backend->mem_config_notify)(rsrcs, nrsrc);
390 break;
392 case DRCTL_MEM_UNCONFIG_REQUEST:
393 (*drd_backend->mem_unconfig_request)(rsrcs, nrsrc);
394 break;
396 case DRCTL_MEM_UNCONFIG_NOTIFY:
397 (*drd_backend->mem_unconfig_notify)(rsrcs, nrsrc);
398 break;
400 case DRCTL_IO_CONFIG_REQUEST:
401 (*drd_backend->io_config_request)(rsrcs, nrsrc);
402 break;
404 case DRCTL_IO_CONFIG_NOTIFY:
405 (*drd_backend->io_config_notify)(rsrcs, nrsrc);
406 break;
408 case DRCTL_IO_UNCONFIG_REQUEST:
409 (*drd_backend->io_unconfig_request)(rsrcs, nrsrc);
410 break;
412 case DRCTL_IO_UNCONFIG_NOTIFY:
413 (*drd_backend->io_unconfig_notify)(rsrcs, nrsrc);
414 break;
416 default:
417 drd_err("unknown command: %d", msg->cmd);
418 DRD_DOOR_RETURN_ERR();
419 break;
422 osize = drd_pack_response(rsrcs, nrsrc);
423 if (osize == 0)
424 DRD_DOOR_RETURN_ERR();
426 (void) door_return((char *)drd_result, osize, NULL, 0);