xref: /illumos-gate/usr/src/lib/libcontract/common/device_dump.c (revision e3ae4b35c024af1196582063ecee3ab79367227d)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/contract/device.h>
27 #include <sys/wait.h>
28 #include <sys/ctfs.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <stdio.h>
35 #include <assert.h>
36 #include <signal.h>
37 #include <libuutil.h>
38 #include <libintl.h>
39 #include <libcontract.h>
40 #include <libcontract_priv.h>
41 #include "libcontract_impl.h"
42 #include "libcontract_priv.h"
43 
44 /*ARGSUSED*/
45 void
46 event_device(FILE *file, ct_evthdl_t ev, int verbose)
47 {
48 	uint_t type;
49 	char *device;
50 	char *s;
51 	ctid_t ctid;
52 	ct_stathdl_t stathdl;
53 	int statfd;
54 
55 	type = ct_event_get_type(ev);
56 	ctid = ct_event_get_ctid(ev);
57 
58 	statfd = contract_open(ctid, "device", "status", O_RDONLY);
59 	if (statfd == -1) {
60 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "[bad contract]\n"));
61 		return;
62 	}
63 
64 	if (ct_status_read(statfd, CTD_ALL, &stathdl) != 0) {
65 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "[status error]\n"));
66 		return;
67 	}
68 
69 	if (ct_dev_status_get_minor(stathdl, &device) != 0) {
70 		(void) fprintf(file, dgettext(TEXT_DOMAIN, "[bad status]\n"));
71 		return;
72 	}
73 
74 
75 	switch (type) {
76 	case CT_DEV_EV_OFFLINE:
77 		s = dgettext(TEXT_DOMAIN, "device %s offlining\n");
78 		break;
79 	case CT_DEV_EV_DEGRADED:
80 		s = dgettext(TEXT_DOMAIN, "device %s degrading\n");
81 		break;
82 	case CT_DEV_EV_ONLINE:
83 		s = dgettext(TEXT_DOMAIN, "device %s online\n");
84 		break;
85 	case CT_EV_NEGEND:
86 		contract_negend_dump(file, ev);
87 		s = NULL;
88 		break;
89 	default:
90 		s = dgettext(TEXT_DOMAIN, "device %s sent an unknown event\n");
91 		break;
92 	}
93 
94 	if (s) {
95 		/*LINTED*/
96 		(void) fprintf(file, s, device);
97 	}
98 
99 	ct_status_free(stathdl);
100 	(void) close(statfd);
101 }
102