8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / avs / ncall / ncalladm.c
blob1214b39c1a0317f971cf1122609085cf0fadeec1
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <strings.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <locale.h>
34 #include <fcntl.h>
35 #include <libgen.h>
37 #include <sys/nsctl/cfg.h>
38 #include <sys/ncall/ncall.h>
40 static CFGFILE *cfg;
41 static int cfg_changed;
42 static char *progname;
43 static ncall_node_t *getnodelist(int, int *, int *);
46 static void
47 usage(int exitstat)
49 (void) fprintf(stderr, gettext("usage:\n"));
50 (void) fprintf(stderr, gettext(" %s -d\n"), progname);
51 (void) fprintf(stderr, gettext(" %s -e\n"), progname);
52 (void) fprintf(stderr, gettext(" %s -h\n"), progname);
53 #ifdef DEBUG
54 (void) fprintf(stderr, gettext(" %s -c [nodeid <nodeid>]\n"),
55 progname);
56 (void) fprintf(stderr, gettext(" %s -i\n"), progname);
57 (void) fprintf(stderr, gettext(" %s -p <host>\n"), progname);
58 #endif
60 (void) fprintf(stderr, gettext("where:\n"));
61 (void) fprintf(stderr, gettext(" -d disable ncall\n"));
62 (void) fprintf(stderr, gettext(" -e enable ncall core\n"));
63 (void) fprintf(stderr, gettext(" -h this help message\n"));
64 #ifdef DEBUG
65 (void) fprintf(stderr,
66 gettext(" -c set or print ncall configuration\n"));
67 (void) fprintf(stderr, gettext(" -i ncall information\n"));
68 (void) fprintf(stderr, gettext(" -p ncall ping <host>\n"));
69 #endif
71 exit(exitstat);
75 static void
76 ncall_cfg_open(CFGLOCK lk)
78 char hostid[32];
80 if (cfg != NULL) {
81 return;
84 if (snprintf(hostid, sizeof (hostid), "%lx", gethostid()) >=
85 sizeof (hostid)) {
86 (void) fprintf(stderr, gettext("%s: hostid %lx too large\n"),
87 progname, gethostid());
88 exit(1);
91 if ((cfg = cfg_open(NULL)) == NULL) {
92 (void) fprintf(stderr,
93 gettext("%s: unable to access the configuration: %s\n"),
94 progname, cfg_error(NULL));
95 exit(1);
98 if (!cfg_lock(cfg, lk)) {
99 (void) fprintf(stderr,
100 gettext("%s: unable to lock the configuration: %s\n"),
101 progname, cfg_error(NULL));
102 exit(1);
105 cfg_resource(cfg, hostid);
109 static void
110 ncall_cfg_close(void)
112 if (cfg_changed && cfg_commit(cfg) < 0) {
113 (void) fprintf(stderr,
114 gettext("%s: unable to update the configuration: %s\n"),
115 progname, cfg_error(NULL));
116 exit(1);
119 cfg_close(cfg);
120 cfg = NULL;
125 * Get config from dscfg.
127 static int
128 get_nodeid_from_cfg(int *nodeid)
130 char buf[CFG_MAX_BUF];
131 int ret = -1;
132 int rc;
134 ncall_cfg_open(CFG_RDLOCK);
136 if (cfg_get_cstring(cfg, "ncallcore.set1", buf, sizeof (buf)) >= 0) {
137 rc = sscanf(buf, "%d", nodeid);
138 if (rc == 1) {
139 ret = 0;
143 ncall_cfg_close();
145 return (ret);
149 static void
150 ncall_print(void)
152 int cfnodeid, clnodeid, rc;
154 clnodeid = cfg_issuncluster();
156 rc = get_nodeid_from_cfg(&cfnodeid);
158 if (rc < 0 && clnodeid > 0) {
159 (void) printf(gettext("%s: ncall is using the SunCluster "
160 "nodeid: %d\n"), progname, clnodeid);
161 } else if (rc < 0) {
162 (void) printf(gettext("%s: ncall is using the default "
163 "nodeid: %d\n"), progname, 0);
164 } else {
165 (void) printf(gettext("%s: current configuration:\n"),
166 progname);
167 /* deliberately not i18n'd - "nodeid" is a keyword */
168 (void) printf("nodeid %d\n", cfnodeid);
173 static void
174 ncall_config(const int nodeid)
176 char buf[CFG_MAX_BUF];
178 ncall_cfg_open(CFG_WRLOCK);
180 if (cfg_get_cstring(cfg, "ncallcore.set1", buf, sizeof (buf)) >= 0) {
181 /* remove old config */
182 if (cfg_put_cstring(cfg, "ncallcore.set1", NULL, 0) < 0) {
183 (void) fprintf(stderr,
184 gettext("%s: unable to update the configuration: "
185 "%s\n"), cfg_error(NULL));
186 exit(1);
190 if (snprintf(buf, sizeof (buf), "%d", nodeid) >= sizeof (buf)) {
191 (void) fprintf(stderr,
192 gettext("%s: unable to update configuration: "
193 "data too long\n"), progname);
194 exit(1);
197 if (cfg_put_cstring(cfg, "ncallcore", buf, sizeof (buf)) < 0) {
198 (void) fprintf(stderr,
199 gettext("%s: unable to update the configuration: %s\n"),
200 cfg_error(NULL));
201 exit(1);
204 cfg_changed = 1;
205 ncall_cfg_close();
207 (void) printf(gettext("%s: configuration set to:\n"), progname);
208 /* deliberately not i18n'd - "nodeid" is a keyword */
209 (void) printf("nodeid %d\n", nodeid);
212 #ifdef lint
214 ncalladm_lintmain(int argc, char *argv[])
215 #else
217 main(int argc, char *argv[])
218 #endif
220 const char *dev = "/dev/ncall";
221 extern int optind, opterr;
222 ncall_node_t nodeinfo, *nodes;
223 int nsize;
224 int i;
225 int cflag, dflag, eflag, iflag, pflag;
226 int rc, fd, opt;
227 int clnodeid, cfnodeid;
228 int up;
229 char *cp, *ping;
230 int mnode; /* mirror nodeid */
232 (void) setlocale(LC_ALL, "");
233 (void) textdomain("ncalladm");
235 opterr = 0;
236 cflag = dflag = eflag = iflag = pflag = 0;
237 ping = NULL;
239 progname = basename(argv[0]);
241 while ((opt = getopt(argc, argv,
242 #ifdef DEBUG
243 "cip:"
244 #endif
245 "deh")) != -1) {
246 switch (opt) {
247 case 'c':
248 cflag = 1;
249 break;
251 case 'd':
252 dflag = 1;
253 break;
255 case 'e':
256 eflag = 1;
257 break;
259 case 'h':
260 usage(0);
261 break;
263 case 'i':
264 iflag = 1;
265 break;
267 case 'p':
268 ping = optarg;
269 pflag = 1;
270 break;
272 default:
273 (void) fprintf(stderr, gettext("%s: unknown option\n"),
274 progname);
275 usage(1);
276 break;
280 if (!(cflag || dflag || eflag || iflag || pflag)) {
281 usage(1);
284 if (argc != optind) {
285 if (!cflag ||
286 (argc - optind) != 2 ||
287 strcmp(argv[optind], "nodeid") != 0) {
288 usage(1);
292 if ((cflag + dflag + eflag + iflag + pflag) > 1) {
293 (void) fprintf(stderr,
294 gettext("%s: multiple options are not supported\n"),
295 progname);
296 usage(1);
299 if (!cflag) {
300 fd = open(dev, O_RDONLY);
301 if (fd < 0) {
302 (void) fprintf(stderr,
303 gettext("%s: unable to open %s: %s\n"),
304 progname, dev, strerror(errno));
305 exit(1);
309 if (dflag) {
310 /* ioctl stop into kernel */
311 if (ioctl(fd, NC_IOC_STOP, 0) < 0) {
312 (void) fprintf(stderr,
313 gettext("%s: unable to disable ncall: %s\n"),
314 progname, strerror(errno));
315 exit(1);
317 } else if (eflag) {
318 bzero(&nodeinfo, sizeof (nodeinfo));
320 clnodeid = cfg_issuncluster();
321 cfnodeid = 0;
323 /* get node info */
324 rc = gethostname(nodeinfo.nc_nodename,
325 sizeof (nodeinfo.nc_nodename));
326 if (rc < 0) {
327 (void) fprintf(stderr,
328 gettext("%s: unable to determine hostname: %s\n"),
329 progname, strerror(errno));
330 exit(1);
333 rc = get_nodeid_from_cfg(&cfnodeid);
335 if (clnodeid > 0 && rc == 0) {
337 * check that the nodeids from the cf file and
338 * cluster match.
340 if (clnodeid != cfnodeid) {
341 (void) fprintf(stderr,
342 gettext("%s: nodeid from configuration "
343 "(%d) != cluster nodeid (%d)\n"),
344 progname, cfnodeid, clnodeid);
345 exit(1);
349 if (rc == 0) {
350 nodeinfo.nc_nodeid = cfnodeid;
351 } else if (clnodeid > 0) {
352 nodeinfo.nc_nodeid = clnodeid;
353 } else {
354 nodeinfo.nc_nodeid = 0;
357 /* ioctl node info into kernel and start ncall */
358 rc = ioctl(fd, NC_IOC_START, &nodeinfo);
359 if (rc < 0) {
360 (void) fprintf(stderr,
361 gettext("%s: unable to enable ncall: %s\n"),
362 progname, strerror(errno));
363 exit(1);
367 if (iflag || pflag) {
368 nodes = getnodelist(fd, &nsize, &mnode);
370 if (nodes == NULL) {
371 (void) fprintf(stderr,
372 gettext("%s: unable to get node info\n"),
373 progname);
374 exit(1);
378 if (iflag) {
379 char *mname;
380 char *pnodestr;
382 (void) printf(gettext("Self Node Name: %s\n"),
383 nodes[0].nc_nodename);
384 (void) printf(gettext("Self Node ID: %d\n"),
385 nodes[0].nc_nodeid);
387 * determine which slot is the mirror node.
389 if (mnode != -1) {
390 for (i = 1; i < nsize; i++) {
391 if (nodes[i].nc_nodeid == mnode) {
392 mname = nodes[i].nc_nodename;
393 break;
397 if ((mnode == -1) || (i >= nsize)) {
398 mname = gettext("unknown");
399 mnode = -1;
402 (void) printf(gettext("Mirror Node Name: %s\n"), mname);
403 (void) printf(gettext("Mirror Node ID: %d\n"), mnode);
405 * See if we need to translate the node strings.
407 if (nsize > 1) {
408 pnodestr = gettext("Node Name: %s\nNode ID: %d\n");
409 for (i = 1; i < nsize; i++) {
411 * Don't print the mirror twice.
413 if (nodes[i].nc_nodeid != mnode) {
414 (void) printf(pnodestr,
415 nodes[i].nc_nodename,
416 nodes[i].nc_nodeid);
422 if (pflag) {
423 if (strlen(ping) >= sizeof (nodeinfo.nc_nodename)) {
424 (void) fprintf(stderr,
425 gettext("%s: hostname '%s' is too long\n"),
426 progname, ping);
427 exit(1);
429 up = 0;
430 if (strcmp(nodes[0].nc_nodename, ping) == 0) {
431 up = 1; /* self */
432 } else {
433 /* not self, so ask kernel */
434 bzero(&nodeinfo, sizeof (nodeinfo));
435 /* strlen(ping) checked above */
436 (void) strcpy(nodeinfo.nc_nodename, ping);
437 up = ioctl(fd, NC_IOC_PING, nodeinfo);
440 /* model the ping messages on ping(1m) */
442 if (up < 0) {
443 (void) fprintf(stderr,
444 gettext("%s: unable to ping host '%s': %s\n"),
445 progname, ping, strerror(errno));
446 exit(1);
447 } else if (up > 0) {
448 (void) printf(gettext("%s is alive\n"), ping);
449 } else {
450 (void) printf(gettext("no answer from %s\n"), ping);
451 exit(1);
455 if (iflag || pflag) {
456 free(nodes);
459 if (cflag) {
460 if (argc == optind) {
461 ncall_print();
462 return (0);
465 cp = NULL;
466 cfnodeid = (int)strtol(argv[optind+1], &cp, 0);
467 if (cp != NULL && *cp != '\0') {
468 (void) fprintf(stderr,
469 gettext("%s: nodeid \"%s\" is not an "
470 "integer number\n"), progname, argv[optind+1]);
471 exit(1);
474 clnodeid = cfg_issuncluster();
475 if (clnodeid > 0 && cfnodeid != clnodeid) {
476 (void) fprintf(stderr,
477 gettext("%s: nodeid from command line "
478 "(%d) != cluster nodeid (%d)\n"),
479 progname, cfnodeid, clnodeid);
480 exit(1);
483 ncall_config(cfnodeid);
486 if (!cflag) {
487 (void) close(fd);
490 return (0);
495 * return a pointer to a list of currently configured
496 * nodes.
497 * Return the number of nodes via the nodesizep pointer.
498 * Return the mirror nodeid via the mirrorp pointer.
499 * Return NULL on errors.
501 static ncall_node_t *
502 getnodelist(int ifd, int *nodesizep, int *mirrorp)
504 int maxsize;
505 int cnt;
506 ncall_node_t *noderet = NULL;
507 ncall_node_t *nodelist;
508 ncall_node_t thisnode;
509 int mirror;
510 int nonet;
513 * Get this host info and mirror nodeid.
515 mirror = ioctl(ifd, NC_IOC_GETNODE, &thisnode);
517 if (mirror < 0) {
518 return (NULL);
522 * See if we need to allocate the buffer.
524 nonet = 0;
525 maxsize = ioctl(ifd, NC_IOC_GETNETNODES, 0);
526 if (maxsize < 1) {
527 maxsize = 1;
528 nonet = 1;
530 nodelist = malloc(sizeof (*nodelist) * maxsize);
531 if (nodelist) {
532 if (nonet == 0) {
534 * fetch the node data.
536 cnt = ioctl(ifd, NC_IOC_GETNETNODES, nodelist);
537 if (cnt > 0) {
538 *nodesizep = cnt;
539 noderet = nodelist;
540 *mirrorp = mirror;
541 } else {
542 *nodesizep = 0;
543 free(nodelist);
545 } else {
546 (void) memcpy(nodelist, &thisnode, sizeof (*nodelist));
547 *nodesizep = 1;
548 noderet = nodelist;
550 * Although we know the mirror nodeid, there
551 * is no point in returning it as we have
552 * no information about any other hosts.
554 *mirrorp = -1;
557 return (noderet);