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