summaryrefslogtreecommitdiff
path: root/usr/src/cmd/svc/common/notify_params.c
blob: e947b0fa764f3d07f5f3c17e21acf3b517aa83e4 (plain)
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
/*
 * 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) 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <libintl.h>
#include <libnvpair.h>
#include <libscf.h>
#include <libscf_priv.h>
#include <libuutil.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include "notify_params.h"

static struct events {
	const char *s;
	int32_t c;
} smf_st_events[] = {
	{ "to-uninitialized", SCF_TRANS(0, SCF_STATE_UNINIT) },
	{ "from-uninitialized",	SCF_TRANS(SCF_STATE_UNINIT, 0) },
	{ "uninitialized", SCF_TRANS(SCF_STATE_UNINIT, SCF_STATE_UNINIT) },
	{ "to-maintenance", SCF_TRANS(0, SCF_STATE_MAINT) },
	{ "from-maintenance", SCF_TRANS(SCF_STATE_MAINT, 0) },
	{ "maintenance", SCF_TRANS(SCF_STATE_MAINT, SCF_STATE_MAINT) },
	{ "to-offline", SCF_TRANS(0, SCF_STATE_OFFLINE) },
	{ "from-offline", SCF_TRANS(SCF_STATE_OFFLINE, 0) },
	{ "offline", SCF_TRANS(SCF_STATE_OFFLINE, SCF_STATE_OFFLINE) },
	{ "to-disabled", SCF_TRANS(0, SCF_STATE_DISABLED) },
	{ "from-disabled", SCF_TRANS(SCF_STATE_DISABLED, 0) },
	{ "disabled", SCF_TRANS(SCF_STATE_DISABLED, SCF_STATE_DISABLED) },
	{ "to-online", SCF_TRANS(0, SCF_STATE_ONLINE) },
	{ "from-online", SCF_TRANS(SCF_STATE_ONLINE, 0) },
	{ "online", SCF_TRANS(SCF_STATE_ONLINE, SCF_STATE_ONLINE) },
	{ "to-degraded", SCF_TRANS(0, SCF_STATE_DEGRADED) },
	{ "from-degraded", SCF_TRANS(SCF_STATE_DEGRADED, 0) },
	{ "degraded", SCF_TRANS(SCF_STATE_DEGRADED, SCF_STATE_DEGRADED) },
	{ "to-all", SCF_TRANS(0, SCF_STATE_ALL) },
	{ "from-all", SCF_TRANS(SCF_STATE_ALL, 0) },
	{ "all", SCF_TRANS(SCF_STATE_ALL, SCF_STATE_ALL) },
	{ NULL, 0 }
};

static struct fma_tags {
	const char *t;
	const char *s;
} fma_tags[] = {
	{ "problem-diagnosed", "list.suspect" },
	{ "problem-updated", "list.updated" },
	{ "problem-repaired", "list.repaired" },
	{ "problem-resolved", "list.resolved" },
	{ NULL, NULL }
};

static char *fma_classes[] = {
	"list.",
	"ireport.",
	NULL
};

/*
 * get_fma_tag()
 * return a pointer to the fma tag at the passed index. NULL if no entry exist
 * for index
 */
const char *
get_fma_tag(uint32_t index)
{
	if (index > (sizeof (fma_tags) / sizeof (struct fma_tags)))
		return (NULL);

	return (fma_tags[index].t);
}

/*
 * get_fma_class()
 * return a pointer to the fma class at the passed index. NULL if no entry exist
 * for index
 */
const char *
get_fma_class(uint32_t index)
{
	if (index > (sizeof (fma_tags) / sizeof (struct fma_tags)))
		return (NULL);

	return (fma_tags[index].s);
}

/*
 * is_fma_token()
 * check if the parameter is an fma token by comparing with the
 * fma_classes[] and the fma_tags[] arrays.
 */
int
is_fma_token(const char *t)
{
	int i;

	for (i = 0; fma_classes[i]; ++i)
		if (strncmp(t, fma_classes[i], strlen(fma_classes[i])) == 0)
			return (1);

	for (i = 0; fma_tags[i].t; ++i)
		if (strcmp(t, fma_tags[i].t) == 0)
			return (1);

	return (0);
}

/*
 * has_fma_tag()
 * returns 1 if there is an fma tag for the passed class, 0 otherwise
 */
int
has_fma_tag(const char *c)
{
	int i;

	for (i = 0; fma_tags[i].s; ++i)
		if (strcmp(c, fma_tags[i].s) == 0)
			return (1);

	return (0);
}

const char *
de_tag(const char *tag)
{
	int i;

	for (i = 0; fma_tags[i].t; ++i)
		if (strcmp(tag, fma_tags[i].t) == 0)
			return (fma_tags[i].s);

	return (tag);
}

const char *
re_tag(const char *fma_event)
{
	int i;

	for (i = 0; fma_tags[i].s; ++i)
		if (strcmp(fma_event, fma_tags[i].s) == 0)
			return (fma_tags[i].t);

	return (fma_event);
}

int32_t
string_to_tset(const char *str)
{
	int i;

	for (i = 0; smf_st_events[i].s != NULL; ++i) {
		if (strcmp(str, smf_st_events[i].s) == 0)
			return (smf_st_events[i].c);
	}

	return (0);
}

const char *
tset_to_string(int32_t t)
{
	int i;

	for (i = 0; smf_st_events[i].s != NULL; ++i) {
		if (smf_st_events[i].c == t)
			return (smf_st_events[i].s);
	}

	return (NULL);
}

void
safe_printf(const char *fmt, ...)
{
	va_list va;

	va_start(va, fmt);
	if (vprintf(fmt, va) < 0)
		uu_die(gettext("Error writing to stdout"));
	va_end(va);
}

static uint32_t
notify_params_get_version(nvlist_t *nvl)
{
	uint32_t v;

	if (nvl == NULL)
		return (0xFFFFFFFFU);

	if (nvlist_lookup_uint32(nvl, SCF_NOTIFY_NAME_VERSION, &v) != 0)
		return (0xFFFFFFFFU);
	else
		return (v);
}

static void
nvpair_print(nvpair_t *p)
{
	char **v;
	uint_t n;
	int i;

	safe_printf("            %s:", nvpair_name(p));
	(void) nvpair_value_string_array(p, &v, &n);
	for (i = 0; i < n; ++i) {
		safe_printf(" %s", v[i]);
	}
	safe_printf("\n");
}

static void
params_type_print(nvlist_t *p, const char *name, const char *fmri)
{
	nvpair_t *tnvp, *nvp;
	nvlist_t *nvl;
	boolean_t *a;
	uint_t n;
	int has_output = 0;

	/* for each event e print all notification parameters */
	for (tnvp = nvlist_next_nvpair(p, NULL); tnvp != NULL;
	    tnvp = nvlist_next_nvpair(p, tnvp)) {
		/* We only want the NVLIST memebers */
		if (nvpair_type(tnvp) != DATA_TYPE_NVLIST)
			continue;

		if (nvpair_value_nvlist(tnvp, &nvl) != 0)
			uu_die("nvpair_value_nvlist");

		if (!has_output)
			if (fmri == NULL)
				safe_printf(gettext("    Event: %s\n"), name);
			else
				safe_printf(gettext(
				    "    Event: %s (source: %s)\n"),
				    name, fmri);

		has_output = 1;

		safe_printf(gettext("        Notification Type: %s\n"),
		    nvpair_name(tnvp));

		if (nvlist_lookup_boolean_array(nvl, PARAM_ACTIVE, &a, &n) != 0)
			uu_warn(gettext("Missing 'active' property"));
		else
			safe_printf(gettext("            Active: %s\n"),
			    *a ? "true" : "false");

		for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
		    nvp = nvlist_next_nvpair(nvl, nvp)) {
			if (nvpair_type(nvp) != DATA_TYPE_STRING_ARRAY)
				continue;
			nvpair_print(nvp);
		}
		safe_printf("\n");
	}
}

void
listnotify_print(nvlist_t *nvl, const char *event)
{
	char *fmri;
	nvlist_t **params;
	size_t n;
	int32_t tset;
	int i;

	/*
	 * Check the nvl we got is from a version we understand
	 */
	if (nvl != NULL && notify_params_get_version(nvl) !=
	    SCF_NOTIFY_PARAMS_VERSION)
		uu_die(gettext("libscf(3LIB) mismatch\n"));

	if (nvl != NULL && nvlist_lookup_nvlist_array(nvl, SCF_NOTIFY_PARAMS,
	    &params, &n) != 0)
		/* Sanity check. If we get here nvl is bad! */
		uu_die(gettext("nvlist_lookup_nvlist_array\n"));

	if (event == NULL) {
		/* this is an SMF state transition nvlist */
		for (i = 0; i < n; ++i) {
			nvlist_t *p = *(params + i);

			if (nvlist_lookup_string(p,
			    SCF_NOTIFY_PARAMS_SOURCE_NAME, &fmri) != 0)
				fmri = NULL;
			if (nvlist_lookup_int32(p, SCF_NOTIFY_NAME_TSET,
			    &tset) != 0)
				uu_die("nvlist_lookup_int32");
			params_type_print(p, tset_to_string(tset), fmri);
		}
	} else {
		/* this is FMA event nvlist */
		if (nvl == NULL) { /* preferences not found */
			return;
		}
		for (i = 0; i < n; ++i) {
			nvlist_t *p = *(params + i);

			if (nvlist_lookup_string(p,
			    SCF_NOTIFY_PARAMS_SOURCE_NAME, &fmri) != 0)
				fmri = NULL;
			params_type_print(p, event, fmri);
		}
	}
}