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, Version 1.0 only
6 * (the "License").  You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23/*
24 * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
25 */
26
27/*
28 * Copyright (c) 1998 by Sun Microsystems, Inc.
29 * All rights reserved.
30 */
31
32#include <stdlib.h>
33#include <stdio.h>
34#include <unistd.h>
35#include <string.h>
36#include <errno.h>
37#include <locale.h>
38#include <sys/cladm.h>
39
40#if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
41#define	TEXT_DOMAIN 	"SYS_TEST"	/* Use this only if it wasn't */
42#endif
43
44static char *cmdname;
45
46static void errmsg(char *);
47static void usage();
48
49int
50main(int argc, char **argv)
51{
52	int c, bootflags;
53	nodeid_t nid, hid;
54	char *cp = "";
55
56	(void) setlocale(LC_ALL, "");
57	(void) textdomain(TEXT_DOMAIN);
58
59	cmdname = argv[0];	/* put actual command name in messages */
60
61	if (_cladm(CL_INITIALIZE, CL_GET_BOOTFLAG, &bootflags) != 0) {
62		errmsg("_cladm(CL_INITIALIZE, CL_GET_BOOTFLAG)");
63		return (1);
64	}
65
66	while ((c = getopt(argc, argv, "bnh")) != EOF) {
67		switch (c) {
68		case 'b':	/* print boot flags */
69			(void) printf("%s%u\n", cp, bootflags);
70			break;
71
72		case 'n':	/* print our node number */
73			if (_cladm(CL_CONFIG, CL_NODEID, &nid) != 0) {
74				errmsg("node is not configured as part of a "
75				    "cluster");
76				return (1);
77			}
78			(void) printf("%s%u\n", cp, nid);
79			break;
80
81		case 'h':	/* print the highest node number */
82			if (_cladm(CL_CONFIG, CL_HIGHEST_NODEID, &hid) != 0) {
83				errmsg("node is not booted as part of a "
84				    "cluster");
85				return (1);
86			}
87			(void) printf("%s%u\n", cp, hid);
88			break;
89
90		default:
91			usage();
92			return (1);
93		}
94		cp = " ";
95	}
96
97	/*
98	 * Return exit status of one (error) if not booted as a cluster.
99	 */
100	return (bootflags & CLUSTER_BOOTED ? 0 : 1);
101}
102
103static void
104errmsg(char *msg)
105{
106	int save_error;
107
108	save_error = errno;		/* in case fprintf changes errno */
109	(void) fprintf(stderr, "%s: ", cmdname);
110	errno = save_error;
111	perror(msg);
112}
113
114static void
115usage()
116{
117	(void) fprintf(stderr, "%s: %s -[bnh]\n", gettext("usage"), cmdname);
118}
119