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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Logical Domains (LDoms) Agents Daemon
*
* The LDoms agents daemon (ldmad) runs on LDoms domains and provides
* information to the control domain. It is composed of a set of agents
* which can send and receive messages to and from the control domain.
* Each agent is registered as a domain service using the libds library,
* and is able to handle requests coming from the control domain.
*
* The control domain sends requests to an agent as messages on the
* corresponding domain service (identified by the agent name). All requests
* are received by the ldmad daemon which dispatches them to the appropriate
* handler function of the agent depending on the type of the message.
*
* After the request has been processed by the handler, the ldmad daemon sent
* a reply message back to the control domain. The reply is either a result
* message if the request was successfully completed, or an error message
* describing the failure.
*/
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <libds.h>
#include <libgen.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <synch.h>
#include <syslog.h>
#include <thread.h>
#include <unistd.h>
#include <sys/debug.h>
#include <sys/ldoms.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "ldma.h"
#define LDMA_MODULE "ldm-agent-daemon"
#define LDMA_CONTROL_DOMAIN_DHDL 0 /* id of the control domain */
typedef struct ldma_connexion_t {
ds_hdl_t hdl; /* connexion handle */
ds_domain_hdl_t dhdl; /* connexion domain handle */
ds_ver_t ver; /* connexion version */
} ldma_connexion_t;
typedef struct ldma_agent {
ldma_agent_info_t *info; /* agent information */
mutex_t conn_lock; /* connexion table lock */
ldma_connexion_t conn[LDOMS_MAX_DOMAINS]; /* connexions */
} ldma_agent_t;
/* information about existing agents */
extern ldma_agent_info_t ldma_device_info;
extern ldma_agent_info_t ldma_system_info;
extern ldma_agent_info_t ldma_dio_info;
boolean_t ldma_debug = B_FALSE;
boolean_t ldma_daemon = B_FALSE;
static ldma_agent_info_t *ldma_agent_infos[] = {
&ldma_device_info,
&ldma_system_info,
&ldma_dio_info,
NULL
};
static char *cmdname;
static pid_t daemon_pid = 0;
/*
* Lookup connexion in agent connexion table.
*/
static ldma_connexion_t *
ldma_connexion_lookup(ldma_agent_t *agent, ds_hdl_t hdl)
{
ldma_connexion_t *connp;
int i;
ASSERT(MUTEX_HELD(&agent->conn_lock));
for (connp = agent->conn, i = 0; i < LDOMS_MAX_DOMAINS; i++, connp++) {
if (connp->hdl == hdl)
return (connp);
}
return (NULL);
}
/*
* Add connextion to agent connexion table.
*/
static int
ldma_connexion_add(ldma_agent_t *agent, ds_hdl_t hdl, ds_domain_hdl_t dhdl,
ds_ver_t *verp)
{
ldma_connexion_t *connp;
ldma_connexion_t *availp = NULL;
int i;
(void) mutex_lock(&agent->conn_lock);
for (connp = agent->conn, i = 0; i < LDOMS_MAX_DOMAINS; i++, connp++) {
if (connp->hdl == hdl)
break;
if (availp == NULL && connp->hdl == DS_INVALID_HDL)
availp = connp;
}
if (i < LDOMS_MAX_DOMAINS) {
(void) mutex_unlock(&agent->conn_lock);
LDMA_INFO("agent %s hdl %llx already exists", agent->info->name,
hdl);
return (0);
}
if (!availp) {
(void) mutex_unlock(&agent->conn_lock);
LDMA_INFO("agent %s too many connections", agent->info->name);
return (0);
}
LDMA_DBG("agent %s adding connection (%x) %llx, %llx, %d.%d",
agent->info->name, availp, hdl, dhdl, verp->major, verp->minor);
availp->hdl = hdl;
availp->dhdl = dhdl;
availp->ver = *verp;
(void) mutex_unlock(&agent->conn_lock);
return (1);
}
/*
* Delete connexion from agent connexion table.
*/
static int
ldma_connexion_delete(ldma_agent_t *agent, ds_hdl_t hdl)
{
ldma_connexion_t *connp;
(void) mutex_lock(&agent->conn_lock);
if ((connp = ldma_connexion_lookup(agent, hdl)) == NULL) {
(void) mutex_unlock(&agent->conn_lock);
LDMA_INFO("agent %s connection delete failed to find %llx",
agent->info->name, hdl);
return (0);
}
LDMA_DBG("agent %s deleting connection (%x) %llx", agent->info->name,
connp, hdl);
connp->hdl = DS_INVALID_HDL;
connp->dhdl = 0;
connp->ver.major = 0;
connp->ver.minor = 0;
(void) mutex_unlock(&agent->conn_lock);
return (1);
}
/*
* Initialize connexion table.
*/
static void
ldma_connexion_init(ldma_agent_t *agent)
{
ldma_connexion_t *connp;
int i;
for (connp = agent->conn, i = 0; i < LDOMS_MAX_DOMAINS; i++, connp++) {
connp->hdl = DS_INVALID_HDL;
}
}
/*
* Allocate a new message with the specified message number (msg_num),
* message type (msg_type) and message data length (msg_dlen). Return
* NULL if the allocation has failed.
*/
static ldma_message_header_t *
ldma_alloc_msg(uint64_t msg_num, uint32_t msg_type, size_t msg_dlen)
{
ldma_message_header_t *msg;
size_t msg_len;
msg_len = LDMA_MESSAGE_SIZE(msg_dlen);
msg = malloc(msg_len);
if (msg == NULL)
return (NULL);
msg->msg_num = msg_num;
msg->msg_type = msg_type;
msg->msg_info = 0;
return (msg);
}
/*
* Allocate a result message (LDMA_MSG_REQ_RESULT) with the specified message
* data length (msg_dlen). If the request argument is not NULL then the message
* is created with the same message number as the request, otherwise the message
* number is set to 0. Return NULL if the allocation has failed.
*/
ldma_message_header_t *
ldma_alloc_result_msg(ldma_message_header_t *request, size_t msg_dlen)
{
uint64_t msg_num;
msg_num = (request == NULL)? 0 : request->msg_num;
return (ldma_alloc_msg(msg_num, LDMA_MSG_RESULT, msg_dlen));
}
/*
* Agent register callback. This callback is invoked when a client is registered
* for using the service provided by an agent. An agent will only have one
* consumer which is coming from the control domain.
*/
static void
ldma_reg_cb(ds_hdl_t hdl, ds_cb_arg_t arg, ds_ver_t *ver,
ds_domain_hdl_t dhdl)
{
ldma_agent_t *agent = (ldma_agent_t *)arg;
char dname[LDOMS_MAX_NAME_LEN];
if (ds_dom_hdl_to_name(dhdl, dname, LDOMS_MAX_NAME_LEN) != 0) {
(void) strcpy(dname, "<unknown>");
}
LDMA_DBG("%s: REGISTER hdl=%llx, dhdl=%llx (%s) ver=%hd.%hd",
agent->info->name, hdl, dhdl, dname, ver->major, ver->minor);
/*
* Record client information. Access control is done on a
* message-by-message basis upon receipt of the message.
*/
if (!ldma_connexion_add(agent, hdl, dhdl, ver)) {
LDMA_INFO("agent %s failed to add connection from "
"domain %s", agent->info->name, dname);
}
}
/*
* Agent unregister callback. This callback is invoked when a client is
* unregistered and stops using the service provided by an agent.
*/
static void
ldma_unreg_cb(ds_hdl_t hdl, ds_cb_arg_t arg)
{
ldma_agent_t *agent = (ldma_agent_t *)arg;
LDMA_DBG("%s: UNREGISTER hdl=%llx", agent->info->name, hdl);
if (!ldma_connexion_delete(agent, hdl)) {
LDMA_INFO("agent %s failed to unregister handle %llx",
agent->info->name, hdl);
}
}
/*
* Agent data callback. This callback is invoked when an agent receives a new
* message from a client. Any request from a client which is not the control
* domain is immediatly rejected. Otherwise the message is forwarded to the
* appropriate handler function provided by the agent, depending on the message
* type.
*/
static void
ldma_data_cb(ds_hdl_t hdl, ds_cb_arg_t arg, void *buf, size_t len)
{
ldma_agent_t *agent = (ldma_agent_t *)arg;
ldma_msg_handler_t *handler;
ldma_message_header_t *request = buf;
ldma_message_header_t *reply = NULL;
ldma_connexion_t *connp;
ds_ver_t conn_ver;
ds_domain_hdl_t conn_dhdl;
ldma_request_status_t status;
size_t request_dlen, reply_len, reply_dlen = 0;
int i;
/* check the message size */
if (len < LDMA_MESSAGE_HEADER_SIZE) {
LDMA_INFO("agent %s has ignored message with an invalid "
"size of %d bytes", agent->info->name, len);
return;
}
request_dlen = LDMA_MESSAGE_DLEN(len);
LDMA_DBG("%s: DATA hdl=%llx, request num=%llu type=0x%x info=0x%x "
"dlen=%d", agent->info->name, hdl, request->msg_num,
request->msg_type, request->msg_info, request_dlen);
(void) mutex_lock(&agent->conn_lock);
connp = ldma_connexion_lookup(agent, hdl);
if (connp != NULL) {
conn_dhdl = connp->dhdl;
conn_ver = connp->ver;
}
(void) mutex_unlock(&agent->conn_lock);
/* reject any request which is not in the connexion table */
if (connp == NULL) {
LDMA_DBG("%s: DATA hdl=%llx, rejecting request from a "
"distrusted domain", agent->info->name, hdl);
status = LDMA_REQ_DENIED;
goto do_reply;
}
handler = NULL;
for (i = 0; i < agent->info->nhandlers; i++) {
if (agent->info->handlers[i].msg_type == request->msg_type) {
handler = &agent->info->handlers[i];
break;
}
}
if (handler == NULL) {
/* this type of message is not defined by the agent */
LDMA_DBG("%s: DATA hdl=%llx, unknown message type %x",
agent->info->name, hdl, request->msg_type);
status = LDMA_REQ_NOTSUP;
goto do_reply;
}
/* reject any request from a guest which is not allowed */
if ((conn_dhdl != LDMA_CONTROL_DOMAIN_DHDL) &&
(handler->msg_flags & LDMA_MSGFLG_ACCESS_ANY) == 0) {
LDMA_DBG("%s: DATA hdl=%llx, rejecting request from a "
"distrusted domain", agent->info->name, hdl);
status = LDMA_REQ_DENIED;
goto do_reply;
}
if (handler->msg_handler == NULL) {
/*
* This type of message is defined by the agent but it
* has no handler. That means there is no processing to
* do, the message is just ignored, but the request is
* successfully completed.
*/
LDMA_DBG("%s: DATA hdl=%llx, no handler",
agent->info->name, hdl);
status = LDMA_REQ_COMPLETED;
goto do_reply;
}
/* invoke the message handler of the agent */
status = (*handler->msg_handler)(&conn_ver, request, request_dlen,
&reply, &reply_dlen);
LDMA_DBG("%s: DATA hdl=%llx, handler stat=%d reply=%p rlen=%d",
agent->info->name, hdl, status, (void *)reply, reply_dlen);
do_reply:
/*
* If the handler has provided a reply message, we use it directly.
* Otherwise, we build a reply depending on the status of the request.
* In that case, we re-use the request buffer to build the reply
* message.
*/
if (reply == NULL) {
reply = request;
reply_dlen = 0;
if (status == LDMA_REQ_COMPLETED) {
/*
* The request was successful but no result message was
* provided so we send an empty result message.
*/
reply->msg_type = LDMA_MSG_RESULT;
reply->msg_info = 0;
} else {
/*
* The request has failed but no error message was
* provided so we send an error message based on the
* request status.
*/
reply->msg_type = LDMA_MSG_ERROR;
reply->msg_info =
(status == LDMA_REQ_NOTSUP)? LDMA_MSGERR_NOTSUP :
(status == LDMA_REQ_INVALID)? LDMA_MSGERR_INVALID :
(status == LDMA_REQ_DENIED)? LDMA_MSGERR_DENY :
LDMA_MSGERR_FAIL;
}
}
reply_len = LDMA_MESSAGE_SIZE(reply_dlen);
LDMA_DBG("%s: DATA hdl=%llx, reply num=%llu type=0x%x info=0x%x "
"dlen=%d", agent->info->name, hdl, reply->msg_num,
reply->msg_type, reply->msg_info, reply_dlen);
if (ds_send_msg(hdl, reply, reply_len) != 0) {
LDMA_ERR("agent %s has failed to send reply for request %llu",
agent->info->name, request->msg_num);
}
if (reply != request)
free(reply);
}
/*
* Register an agent. Return 0 if the agent was successfully registered.
*/
static int
ldma_register(ldma_agent_info_t *agent_info)
{
ldma_agent_t *agent;
ds_capability_t ds_cap;
ds_ops_t ds_ops;
agent = malloc(sizeof (ldma_agent_t));
if (agent == NULL)
goto register_fail;
agent->info = agent_info;
(void) mutex_init(&agent->conn_lock, USYNC_THREAD, NULL);
ldma_connexion_init(agent);
ds_cap.svc_id = agent_info->name;
ds_cap.vers = agent_info->vers;
ds_cap.nvers = agent_info->nvers;
ds_ops.ds_reg_cb = ldma_reg_cb;
ds_ops.ds_unreg_cb = ldma_unreg_cb;
ds_ops.ds_data_cb = ldma_data_cb;
ds_ops.cb_arg = agent;
if (ds_svc_reg(&ds_cap, &ds_ops) == 0) {
LDMA_INFO("agent %s registered", agent_info->name);
return (0);
}
register_fail:
LDMA_ERR("agent %s has failed to register", agent_info->name);
free(agent);
return (-1);
}
/*
* Register all known agents. Return the number of agents successfully
* registered.
*/
static int
ldma_register_agents()
{
int count = 0;
ldma_agent_info_t **agent_infop;
for (agent_infop = ldma_agent_infos;
*agent_infop != NULL; agent_infop++) {
if (ldma_register(*agent_infop) == 0)
count++;
}
return (count);
}
/*ARGSUSED*/
static void
ldma_sigusr_handler(int sig, siginfo_t *sinfo, void *ucontext)
{
/*
* The child process can send the signal before the fork()
* call has returned in the parent process. So daemon_pid
* may not be set yet, and we don't check the pid in that
* case.
*/
if (sig != SIGUSR1 || sinfo->si_code != SI_USER ||
(daemon_pid > 0 && sinfo->si_pid != daemon_pid))
return;
/*
* The parent process has received a USR1 signal from the child.
* This means that the daemon has correctly started and the parent
* can exit.
*/
exit(0);
}
static void
ldma_start(boolean_t standalone)
{
int stat, rv;
struct sigaction action;
if (!standalone) {
/*
* Some configuration of the daemon has to be done in the
* child, but we want the parent to report if the daemon
* has successfully started or not. So we setup a signal
* handler, and the child will notify the parent using the
* USR1 signal if the setup was successful. Otherwise the
* child will exit.
*/
action.sa_sigaction = ldma_sigusr_handler;
action.sa_flags = SA_SIGINFO;
if (sigemptyset(&action.sa_mask) == -1) {
LDMA_ERR("sigemptyset error (%d)", errno);
exit(1);
}
if (sigaction(SIGUSR1, &action, NULL) == -1) {
LDMA_ERR("sigaction() error (%d)", errno);
exit(1);
}
if (sigrelse(SIGUSR1) == -1) {
LDMA_ERR("sigrelse() error (%d)", errno);
exit(1);
}
if ((daemon_pid = fork()) == -1) {
LDMA_ERR("fork() error (%d)", errno);
exit(1);
}
if (daemon_pid != 0) {
/*
* The parent process waits until the child exits (in
* case of an error) or sends a USR1 signal (if the
* daemon has correctly started).
*/
for (;;) {
rv = waitpid(daemon_pid, &stat, 0);
if ((rv == daemon_pid && WIFEXITED(stat)) ||
(rv == -1 && errno != EINTR)) {
/* child has exited or error */
exit(1);
}
}
}
/*
* Initialize child process
*/
if (sighold(SIGUSR1) == -1) {
LDMA_ERR("sighold error (%d)", errno);
exit(1);
}
if (sigignore(SIGUSR1) == -1) {
LDMA_ERR("sigignore error (%d)", errno);
exit(1);
}
if (setsid() == -1) {
LDMA_ERR("setsid error (%d)", errno);
exit(1);
}
if (chdir("/") == -1) {
LDMA_ERR("chdir error (%d)", errno);
exit(1);
}
(void) umask(0);
/*
* Initialize file descriptors. Do not touch stderr
* which is initialized by SMF to point to the daemon
* specific log file.
*/
(void) close(STDIN_FILENO);
if (open("/dev/null", O_RDWR) == -1) {
LDMA_ERR("open /dev/null error (%d)", errno);
exit(1);
}
if (dup2(STDIN_FILENO, STDOUT_FILENO) == -1) {
LDMA_ERR("dup2 error (%d)", errno);
exit(1);
}
closefrom(STDERR_FILENO + 1);
/* initialize logging */
openlog(cmdname, LOG_CONS | LOG_NDELAY, LOG_DAEMON);
ldma_daemon = B_TRUE;
}
/*
* Register the agents. It would be easier to do this before
* daemonizing so that any start error is directly reported. But
* this can not be done because agents are registered using libds
* and this will subscribe the daemon to some sysevents which is
* a process based subscription. Instead we notify the parent process
* either by exiting, or by sending a SIGUSR1 signal.
*/
if (ldma_register_agents() == 0) {
/* no agent registered */
LDMA_ERR("Unable to register any agent");
exit(1);
}
if (!standalone) {
/* signal parent that startup was successful */
if (kill(getppid(), SIGUSR1) == -1)
exit(1);
}
}
static void
ldma_usage()
{
(void) fprintf(stderr, "usage: %s\n", cmdname);
}
int
main(int argc, char *argv[])
{
int opt;
boolean_t standalone = B_FALSE;
cmdname = basename(argv[0]);
/* disable getopt error messages */
opterr = 0;
while ((opt = getopt(argc, argv, "ds")) != EOF) {
switch (opt) {
case 'd':
ldma_debug = B_TRUE;
break;
case 's':
standalone = B_TRUE;
break;
default:
ldma_usage();
exit(1);
}
}
ldma_start(standalone);
/*
* Loop forever. Any incoming message will be received by libds and
* forwarded to the agent data callback (ldma_data_cb()) where it
* will be processed.
*/
for (;;) {
(void) pause();
}
/*NOTREACHED*/
return (0);
}
|