19501SRobert.Johnston@Sun.COM /*
29501SRobert.Johnston@Sun.COM * CDDL HEADER START
39501SRobert.Johnston@Sun.COM *
49501SRobert.Johnston@Sun.COM * The contents of this file are subject to the terms of the
59501SRobert.Johnston@Sun.COM * Common Development and Distribution License (the "License").
69501SRobert.Johnston@Sun.COM * You may not use this file except in compliance with the License.
79501SRobert.Johnston@Sun.COM *
89501SRobert.Johnston@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
99501SRobert.Johnston@Sun.COM * or http://www.opensolaris.org/os/licensing.
109501SRobert.Johnston@Sun.COM * See the License for the specific language governing permissions
119501SRobert.Johnston@Sun.COM * and limitations under the License.
129501SRobert.Johnston@Sun.COM *
139501SRobert.Johnston@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
149501SRobert.Johnston@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
159501SRobert.Johnston@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
169501SRobert.Johnston@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
179501SRobert.Johnston@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
189501SRobert.Johnston@Sun.COM *
199501SRobert.Johnston@Sun.COM * CDDL HEADER END
209501SRobert.Johnston@Sun.COM */
219501SRobert.Johnston@Sun.COM
229501SRobert.Johnston@Sun.COM /*
239501SRobert.Johnston@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
249501SRobert.Johnston@Sun.COM * Use is subject to license terms.
259501SRobert.Johnston@Sun.COM */
269501SRobert.Johnston@Sun.COM
279501SRobert.Johnston@Sun.COM #include <sys/types.h>
289501SRobert.Johnston@Sun.COM #include <sys/wait.h>
299501SRobert.Johnston@Sun.COM
309501SRobert.Johnston@Sun.COM #include <sys/fm/protocol.h>
319501SRobert.Johnston@Sun.COM #include <fm/fmd_msg.h>
329501SRobert.Johnston@Sun.COM
339501SRobert.Johnston@Sun.COM #include <unistd.h>
349501SRobert.Johnston@Sun.COM #include <signal.h>
359501SRobert.Johnston@Sun.COM #include <strings.h>
369501SRobert.Johnston@Sun.COM #include <stdlib.h>
379501SRobert.Johnston@Sun.COM #include <stdio.h>
389501SRobert.Johnston@Sun.COM #include <errno.h>
399501SRobert.Johnston@Sun.COM
409501SRobert.Johnston@Sun.COM #define TEST_ARR_SZ 2
419501SRobert.Johnston@Sun.COM
429501SRobert.Johnston@Sun.COM int
main(int argc,char * argv[])439501SRobert.Johnston@Sun.COM main(int argc, char *argv[])
449501SRobert.Johnston@Sun.COM {
459501SRobert.Johnston@Sun.COM fmd_msg_hdl_t *h;
469501SRobert.Johnston@Sun.COM pid_t pid;
479501SRobert.Johnston@Sun.COM int i, err = 0;
489501SRobert.Johnston@Sun.COM char *s;
499501SRobert.Johnston@Sun.COM
509501SRobert.Johnston@Sun.COM nvlist_t *auth, *fmri, *list, *test_arr[TEST_ARR_SZ];
519501SRobert.Johnston@Sun.COM const char *code = "TEST-8000-08";
529501SRobert.Johnston@Sun.COM int64_t tod[] = { 0x9400000, 0 };
539501SRobert.Johnston@Sun.COM
549501SRobert.Johnston@Sun.COM if (argc > 1) {
559501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "Usage: %s\n", argv[0]);
569501SRobert.Johnston@Sun.COM return (2);
579501SRobert.Johnston@Sun.COM }
589501SRobert.Johnston@Sun.COM
599501SRobert.Johnston@Sun.COM /*
609501SRobert.Johnston@Sun.COM * Build up a valid list.suspect event for a fictional diagnosis
619501SRobert.Johnston@Sun.COM * using a diagnosis code from our test dictionary so we can format
629501SRobert.Johnston@Sun.COM * messages.
639501SRobert.Johnston@Sun.COM */
649501SRobert.Johnston@Sun.COM if (nvlist_alloc(&auth, NV_UNIQUE_NAME, 0) != 0 ||
659501SRobert.Johnston@Sun.COM nvlist_alloc(&fmri, NV_UNIQUE_NAME, 0) != 0 ||
669501SRobert.Johnston@Sun.COM nvlist_alloc(&list, NV_UNIQUE_NAME, 0) != 0) {
679501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: nvlist_alloc failed\n", argv[0]);
689501SRobert.Johnston@Sun.COM return (1);
699501SRobert.Johnston@Sun.COM }
709501SRobert.Johnston@Sun.COM
719501SRobert.Johnston@Sun.COM err |= nvlist_add_uint8(auth, FM_VERSION, FM_FMRI_AUTH_VERSION);
729501SRobert.Johnston@Sun.COM err |= nvlist_add_string(auth, FM_FMRI_AUTH_PRODUCT, "product");
73*10462SSean.Ye@Sun.COM err |= nvlist_add_string(auth, FM_FMRI_AUTH_PRODUCT_SN, "product_sn");
749501SRobert.Johnston@Sun.COM err |= nvlist_add_string(auth, FM_FMRI_AUTH_CHASSIS, "chassis");
759501SRobert.Johnston@Sun.COM err |= nvlist_add_string(auth, FM_FMRI_AUTH_DOMAIN, "domain");
769501SRobert.Johnston@Sun.COM err |= nvlist_add_string(auth, FM_FMRI_AUTH_SERVER, "server");
779501SRobert.Johnston@Sun.COM
789501SRobert.Johnston@Sun.COM if (err != 0) {
799501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: failed to build auth nvlist: %s\n",
809501SRobert.Johnston@Sun.COM argv[0], strerror(err));
819501SRobert.Johnston@Sun.COM return (1);
829501SRobert.Johnston@Sun.COM }
839501SRobert.Johnston@Sun.COM
849501SRobert.Johnston@Sun.COM err |= nvlist_add_uint8(fmri, FM_VERSION, FM_FMD_SCHEME_VERSION);
859501SRobert.Johnston@Sun.COM err |= nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_FMD);
869501SRobert.Johnston@Sun.COM err |= nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY, auth);
879501SRobert.Johnston@Sun.COM err |= nvlist_add_string(fmri, FM_FMRI_FMD_NAME, "fmd_msg_test");
889501SRobert.Johnston@Sun.COM err |= nvlist_add_string(fmri, FM_FMRI_FMD_VERSION, "1.0");
899501SRobert.Johnston@Sun.COM
909501SRobert.Johnston@Sun.COM if (err != 0) {
919501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: failed to build fmri nvlist: %s\n",
929501SRobert.Johnston@Sun.COM argv[0], strerror(err));
939501SRobert.Johnston@Sun.COM return (1);
949501SRobert.Johnston@Sun.COM }
959501SRobert.Johnston@Sun.COM
969501SRobert.Johnston@Sun.COM err |= nvlist_add_uint8(list, FM_VERSION, FM_SUSPECT_VERSION);
979501SRobert.Johnston@Sun.COM err |= nvlist_add_string(list, FM_CLASS, FM_LIST_SUSPECT_CLASS);
989501SRobert.Johnston@Sun.COM err |= nvlist_add_string(list, FM_SUSPECT_UUID, "12345678");
999501SRobert.Johnston@Sun.COM err |= nvlist_add_string(list, FM_SUSPECT_DIAG_CODE, code);
1009501SRobert.Johnston@Sun.COM err |= nvlist_add_int64_array(list, FM_SUSPECT_DIAG_TIME, tod, 2);
1019501SRobert.Johnston@Sun.COM err |= nvlist_add_nvlist(list, FM_SUSPECT_DE, fmri);
1029501SRobert.Johnston@Sun.COM err |= nvlist_add_uint32(list, FM_SUSPECT_FAULT_SZ, 0);
1039501SRobert.Johnston@Sun.COM
1049501SRobert.Johnston@Sun.COM /*
1059501SRobert.Johnston@Sun.COM * Add a contrived nvlist array to our list.suspect so that we can
1069501SRobert.Johnston@Sun.COM * exercise the expansion syntax for dereferencing nvlist array members
1079501SRobert.Johnston@Sun.COM */
1089501SRobert.Johnston@Sun.COM for (i = 0; i < TEST_ARR_SZ; i++) {
1099501SRobert.Johnston@Sun.COM if (nvlist_alloc(&test_arr[i], NV_UNIQUE_NAME, 0) != 0) {
1109501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: failed to alloc nvlist "
1119501SRobert.Johnston@Sun.COM "array: %s\n", argv[0], strerror(err));
1129501SRobert.Johnston@Sun.COM return (1);
1139501SRobert.Johnston@Sun.COM }
1149501SRobert.Johnston@Sun.COM err |= nvlist_add_uint8(test_arr[i], "index", i);
1159501SRobert.Johnston@Sun.COM }
1169501SRobert.Johnston@Sun.COM err |= nvlist_add_nvlist_array(list, "test_arr", test_arr, TEST_ARR_SZ);
1179501SRobert.Johnston@Sun.COM
1189501SRobert.Johnston@Sun.COM if (err != 0) {
1199501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: failed to build list nvlist: %s\n",
1209501SRobert.Johnston@Sun.COM argv[0], strerror(err));
1219501SRobert.Johnston@Sun.COM return (1);
1229501SRobert.Johnston@Sun.COM }
1239501SRobert.Johnston@Sun.COM
1249501SRobert.Johnston@Sun.COM /*
1259501SRobert.Johnston@Sun.COM * Now initialize the libfmd_msg library for testing, using the message
1269501SRobert.Johnston@Sun.COM * catalogs found in the proto area of the current workspace.
1279501SRobert.Johnston@Sun.COM */
1289501SRobert.Johnston@Sun.COM if ((h = fmd_msg_init(getenv("ROOT"), FMD_MSG_VERSION)) == NULL) {
1299501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: fmd_msg_init failed: %s\n",
1309501SRobert.Johnston@Sun.COM argv[0], strerror(errno));
1319501SRobert.Johnston@Sun.COM return (1);
1329501SRobert.Johnston@Sun.COM }
1339501SRobert.Johnston@Sun.COM
1349501SRobert.Johnston@Sun.COM /*
1359501SRobert.Johnston@Sun.COM * Test 0: Verify that both fmd_msg_getitem_id and fmd_msg_gettext_id
1369501SRobert.Johnston@Sun.COM * return NULL and EINVAL for an illegal message code, and NULL
1379501SRobert.Johnston@Sun.COM * and ENOENT for a valid but not defined message code.
1389501SRobert.Johnston@Sun.COM */
1399501SRobert.Johnston@Sun.COM s = fmd_msg_getitem_id(h, NULL, "I_AM_NOT_VALID", 0);
1409501SRobert.Johnston@Sun.COM if (s != NULL || errno != EINVAL) {
1419501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: test0 FAIL: illegal code returned "
1429501SRobert.Johnston@Sun.COM "s = %p, errno = %d\n", argv[0], (void *)s, errno);
1439501SRobert.Johnston@Sun.COM return (1);
1449501SRobert.Johnston@Sun.COM }
1459501SRobert.Johnston@Sun.COM
1469501SRobert.Johnston@Sun.COM s = fmd_msg_gettext_id(h, NULL, "I_AM_NOT_VALID");
1479501SRobert.Johnston@Sun.COM if (s != NULL || errno != EINVAL) {
1489501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: test0 FAIL: illegal code returned "
1499501SRobert.Johnston@Sun.COM "s = %p, errno = %d\n", argv[0], (void *)s, errno);
1509501SRobert.Johnston@Sun.COM return (1);
1519501SRobert.Johnston@Sun.COM }
1529501SRobert.Johnston@Sun.COM
1539501SRobert.Johnston@Sun.COM s = fmd_msg_getitem_id(h, NULL, "I_AM_NOT_HERE-0000-0000", 0);
1549501SRobert.Johnston@Sun.COM if (s != NULL || errno != ENOENT) {
1559501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: test0 FAIL: missing code returned "
1569501SRobert.Johnston@Sun.COM "s = %p, errno = %d\n", argv[0], (void *)s, errno);
1579501SRobert.Johnston@Sun.COM return (1);
1589501SRobert.Johnston@Sun.COM }
1599501SRobert.Johnston@Sun.COM
1609501SRobert.Johnston@Sun.COM s = fmd_msg_gettext_id(h, NULL, "I_AM_NOT_HERE-0000-0000");
1619501SRobert.Johnston@Sun.COM if (s != NULL || errno != ENOENT) {
1629501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: test0 FAIL: missing code returned "
1639501SRobert.Johnston@Sun.COM "s = %p, errno = %d\n", argv[0], (void *)s, errno);
1649501SRobert.Johnston@Sun.COM return (1);
1659501SRobert.Johnston@Sun.COM }
1669501SRobert.Johnston@Sun.COM
1679501SRobert.Johnston@Sun.COM /*
1689501SRobert.Johnston@Sun.COM * Test 1: Use fmd_msg_getitem_id to retrieve the item strings for
1699501SRobert.Johnston@Sun.COM * a known message code without having any actual event handle.
1709501SRobert.Johnston@Sun.COM */
1719501SRobert.Johnston@Sun.COM for (i = 0; i < FMD_MSG_ITEM_MAX; i++) {
1729501SRobert.Johnston@Sun.COM if ((s = fmd_msg_getitem_id(h, NULL, code, i)) == NULL) {
1739501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: fmd_msg_getitem_id failed "
1749501SRobert.Johnston@Sun.COM "for %s, item %d: %s\n",
1759501SRobert.Johnston@Sun.COM argv[0], code, i, strerror(errno));
1769501SRobert.Johnston@Sun.COM }
1779501SRobert.Johnston@Sun.COM
1789501SRobert.Johnston@Sun.COM (void) printf("code %s item %d = <<%s>>\n", code, i, s);
1799501SRobert.Johnston@Sun.COM free(s);
1809501SRobert.Johnston@Sun.COM }
1819501SRobert.Johnston@Sun.COM
1829501SRobert.Johnston@Sun.COM /*
1839501SRobert.Johnston@Sun.COM * Test 2: Use fmd_msg_gettext_id to retrieve the complete message for
1849501SRobert.Johnston@Sun.COM * a known message code without having any actual event handle.
1859501SRobert.Johnston@Sun.COM */
1869501SRobert.Johnston@Sun.COM if ((s = fmd_msg_gettext_id(h, NULL, code)) == NULL) {
1879501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: fmd_msg_gettext_id failed for %s: "
1889501SRobert.Johnston@Sun.COM "%s\n", argv[0], code, strerror(errno));
1899501SRobert.Johnston@Sun.COM return (1);
1909501SRobert.Johnston@Sun.COM }
1919501SRobert.Johnston@Sun.COM
1929501SRobert.Johnston@Sun.COM (void) printf("%s\n", s);
1939501SRobert.Johnston@Sun.COM free(s);
1949501SRobert.Johnston@Sun.COM
1959501SRobert.Johnston@Sun.COM /*
1969501SRobert.Johnston@Sun.COM * Test 3: Use fmd_msg_getitem_nv to retrieve the item strings for
1979501SRobert.Johnston@Sun.COM * our list.suspect event handle.
1989501SRobert.Johnston@Sun.COM */
1999501SRobert.Johnston@Sun.COM for (i = 0; i < FMD_MSG_ITEM_MAX; i++) {
2009501SRobert.Johnston@Sun.COM if ((s = fmd_msg_getitem_nv(h, NULL, list, i)) == NULL) {
2019501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: fmd_msg_getitem_nv failed "
2029501SRobert.Johnston@Sun.COM "for %s, item %d: %s\n",
2039501SRobert.Johnston@Sun.COM argv[0], code, i, strerror(errno));
2049501SRobert.Johnston@Sun.COM }
2059501SRobert.Johnston@Sun.COM
2069501SRobert.Johnston@Sun.COM (void) printf("code %s item %d = <<%s>>\n", code, i, s);
2079501SRobert.Johnston@Sun.COM free(s);
2089501SRobert.Johnston@Sun.COM }
2099501SRobert.Johnston@Sun.COM
2109501SRobert.Johnston@Sun.COM /*
2119501SRobert.Johnston@Sun.COM * Test 4: Use fmd_msg_getitem_nv to retrieve the complete message for
2129501SRobert.Johnston@Sun.COM * a known message code using our list.suspect event handle.
2139501SRobert.Johnston@Sun.COM */
2149501SRobert.Johnston@Sun.COM if ((s = fmd_msg_gettext_nv(h, NULL, list)) == NULL) {
2159501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: fmd_msg_gettext_nv failed for %s: "
2169501SRobert.Johnston@Sun.COM "%s\n", argv[0], code, strerror(errno));
2179501SRobert.Johnston@Sun.COM return (1);
2189501SRobert.Johnston@Sun.COM }
2199501SRobert.Johnston@Sun.COM
2209501SRobert.Johnston@Sun.COM (void) printf("%s\n", s);
2219501SRobert.Johnston@Sun.COM free(s);
2229501SRobert.Johnston@Sun.COM
2239501SRobert.Johnston@Sun.COM /*
2249501SRobert.Johnston@Sun.COM * Test 5: Use fmd_msg_getitem_nv to retrieve the complete message for
2259501SRobert.Johnston@Sun.COM * a known message code using our list.suspect event handle, but this
2269501SRobert.Johnston@Sun.COM * time set the URL to our own customized URL. Our contrived message
2279501SRobert.Johnston@Sun.COM * has been designed to exercise the key aspects of the variable
2289501SRobert.Johnston@Sun.COM * expansion syntax.
2299501SRobert.Johnston@Sun.COM */
2309501SRobert.Johnston@Sun.COM if (fmd_msg_url_set(h, "http://foo.bar.com/") != 0) {
2319501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: fmd_msg_url_set failed: %s\n",
2329501SRobert.Johnston@Sun.COM argv[0], strerror(errno));
2339501SRobert.Johnston@Sun.COM }
2349501SRobert.Johnston@Sun.COM
2359501SRobert.Johnston@Sun.COM if ((s = fmd_msg_gettext_nv(h, NULL, list)) == NULL) {
2369501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "%s: fmd_msg_gettext_nv failed for %s: "
2379501SRobert.Johnston@Sun.COM "%s\n", argv[0], code, strerror(errno));
2389501SRobert.Johnston@Sun.COM return (1);
2399501SRobert.Johnston@Sun.COM }
2409501SRobert.Johnston@Sun.COM
2419501SRobert.Johnston@Sun.COM (void) printf("%s\n", s);
2429501SRobert.Johnston@Sun.COM free(s);
2439501SRobert.Johnston@Sun.COM
2449501SRobert.Johnston@Sun.COM for (i = 0; i < TEST_ARR_SZ; i++)
2459501SRobert.Johnston@Sun.COM nvlist_free(test_arr[i]);
2469501SRobert.Johnston@Sun.COM nvlist_free(fmri);
2479501SRobert.Johnston@Sun.COM nvlist_free(auth);
2489501SRobert.Johnston@Sun.COM nvlist_free(list);
2499501SRobert.Johnston@Sun.COM
2509501SRobert.Johnston@Sun.COM fmd_msg_fini(h); /* free library state before dumping core */
2519501SRobert.Johnston@Sun.COM pid = fork(); /* fork into background to not bother make(1) */
2529501SRobert.Johnston@Sun.COM
2539501SRobert.Johnston@Sun.COM switch (pid) {
2549501SRobert.Johnston@Sun.COM case -1:
2559501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "FAIL (failed to fork)\n");
2569501SRobert.Johnston@Sun.COM return (1);
2579501SRobert.Johnston@Sun.COM case 0:
2589501SRobert.Johnston@Sun.COM abort();
2599501SRobert.Johnston@Sun.COM return (1);
2609501SRobert.Johnston@Sun.COM }
2619501SRobert.Johnston@Sun.COM
2629501SRobert.Johnston@Sun.COM if (waitpid(pid, &err, 0) == -1) {
2639501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "FAIL (failed to wait for %d: %s)\n",
2649501SRobert.Johnston@Sun.COM (int)pid, strerror(errno));
2659501SRobert.Johnston@Sun.COM return (1);
2669501SRobert.Johnston@Sun.COM }
2679501SRobert.Johnston@Sun.COM
2689501SRobert.Johnston@Sun.COM if (WIFSIGNALED(err) == 0 || WTERMSIG(err) != SIGABRT) {
2699501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "FAIL (child did not SIGABRT)\n");
2709501SRobert.Johnston@Sun.COM return (1);
2719501SRobert.Johnston@Sun.COM }
2729501SRobert.Johnston@Sun.COM
2739501SRobert.Johnston@Sun.COM if (!WCOREDUMP(err)) {
2749501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "FAIL (no core generated)\n");
2759501SRobert.Johnston@Sun.COM return (1);
2769501SRobert.Johnston@Sun.COM }
2779501SRobert.Johnston@Sun.COM
2789501SRobert.Johnston@Sun.COM (void) fprintf(stderr, "done\n");
2799501SRobert.Johnston@Sun.COM return (0);
2809501SRobert.Johnston@Sun.COM }
281