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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
42 #include <sys/types.h>
45 #include <sys/drctl_impl.h>
46 #include <sys/drctl.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)
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
);
72 main(int argc
, char **argv
)
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
) {
93 drd_err("unkown option: -%c", optopt
);
98 drd_dbg("initializing %s...", cmdname
);
101 if (geteuid() != 0) {
102 drd_err("permission denied: must run as root");
106 /* open the drctl device */
107 if (drd_init_drctl_dev(standalone
) != 0) {
108 drd_err("unable to initialize drctl device");
117 /* initialize door server */
118 if (drd_init_door_server(standalone
) != 0) {
119 drd_err("unable to initialize door server");
123 /* initialize the backend */
124 if ((*drd_backend
->init
)() != 0) {
125 drd_err("unable to initialize backend processor");
143 if ((pid
= fork()) == -1) {
144 drd_err("failed to fork: %s", strerror(errno
));
154 * Initialize child process
161 * Initialize file descriptors. Do not touch stderr
162 * which is initialized by SMF to point to the drd
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
;
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);
196 drd_init_door_server(boolean_t standalone
)
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
));
210 if (drctl_fd
!= -1) {
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
);
221 drd_dbg("connection to drctl established");
223 /* setup is complete in daemon mode */
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
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
);
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
);
256 drd_dbg("door server attached to '%s'", DRD_DOOR_FILE
);
262 drd_pack_response(drctl_rsrc_t
*rsrcs
, int nrsrc
)
264 drctl_rsrc_t
*orsrcsp
;
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.
283 orsrcsp
= calloc(sizeof (*orsrcsp
), nrsrc
);
284 osize
= sizeof (*orsrcsp
) * nrsrc
;
285 bcopy(rsrcs
, orsrcsp
, 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
295 for (idx
= 0; idx
< nrsrc
; idx
++) {
297 str
= (char *)(uintptr_t)rsrcs
[idx
].offset
;
299 /* skip if no error string */
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
));
311 /* clean up any remaining strings */
312 while (idx
< nrsrc
) {
313 str
= (char *)(uintptr_t)rsrcs
[idx
++].offset
;
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.
332 rsrcs
[idx
].offset
= 0;
334 /* update size and offset */
339 drd_result
= orsrcsp
;
345 drd_door_server(void *cookie
, char *argp
, size_t arg_sz
, door_desc_t
*dp
,
348 drd_msg_t
*msg
= (drd_msg_t
*)(uintptr_t)argp
;
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
;
366 /* pass off to backend for processing */
368 case DRCTL_CPU_CONFIG_REQUEST
:
369 (*drd_backend
->cpu_config_request
)(rsrcs
, nrsrc
);
372 case DRCTL_CPU_CONFIG_NOTIFY
:
373 (*drd_backend
->cpu_config_notify
)(rsrcs
, nrsrc
);
376 case DRCTL_CPU_UNCONFIG_REQUEST
:
377 (*drd_backend
->cpu_unconfig_request
)(rsrcs
, nrsrc
);
380 case DRCTL_CPU_UNCONFIG_NOTIFY
:
381 (*drd_backend
->cpu_unconfig_notify
)(rsrcs
, nrsrc
);
384 case DRCTL_MEM_CONFIG_REQUEST
:
385 (*drd_backend
->mem_config_request
)(rsrcs
, nrsrc
);
388 case DRCTL_MEM_CONFIG_NOTIFY
:
389 (*drd_backend
->mem_config_notify
)(rsrcs
, nrsrc
);
392 case DRCTL_MEM_UNCONFIG_REQUEST
:
393 (*drd_backend
->mem_unconfig_request
)(rsrcs
, nrsrc
);
396 case DRCTL_MEM_UNCONFIG_NOTIFY
:
397 (*drd_backend
->mem_unconfig_notify
)(rsrcs
, nrsrc
);
400 case DRCTL_IO_CONFIG_REQUEST
:
401 (*drd_backend
->io_config_request
)(rsrcs
, nrsrc
);
404 case DRCTL_IO_CONFIG_NOTIFY
:
405 (*drd_backend
->io_config_notify
)(rsrcs
, nrsrc
);
408 case DRCTL_IO_UNCONFIG_REQUEST
:
409 (*drd_backend
->io_unconfig_request
)(rsrcs
, nrsrc
);
412 case DRCTL_IO_UNCONFIG_NOTIFY
:
413 (*drd_backend
->io_unconfig_notify
)(rsrcs
, nrsrc
);
417 drd_err("unknown command: %d", msg
->cmd
);
418 DRD_DOOR_RETURN_ERR();
422 osize
= drd_pack_response(rsrcs
, nrsrc
);
424 DRD_DOOR_RETURN_ERR();
426 (void) door_return((char *)drd_result
, osize
, NULL
, 0);