1 /*****************************************************************************\
2 * $Id: ipmidetect.h,v 1.11 2010-02-08 22:02:30 chu11 Exp $
3 *****************************************************************************
4 * Copyright (C) 2007-2015 Lawrence Livermore National Security, LLC.
5 * Copyright (C) 2007 The Regents of the University of California.
6 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
7 * Written by Albert Chu <chu11@llnl.gov>
8 * UCRL-CODE-228523
9 *
10 * This file is part of Ipmidetect, tools and libraries for detecting
11 * IPMI nodes in a cluster. For details, see http://www.llnl.gov/linux/.
12 *
13 * Ipmidetect is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 3 of the License, or (at your
16 * option) any later version.
17 *
18 * Ipmidetect is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with Ipmidetect. If not, see <http://www.gnu.org/licenses/>.
25 \*****************************************************************************/
26
27 #if HAVE_CONFIG_H
28 #include "config.h"
29 #endif /* HAVE_CONFIG_H */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #if STDC_HEADERS
34 #include <string.h>
35 #endif /* STDC_HEADERS */
36 #if HAVE_ARGP_H
37 #include <argp.h>
38 #else /* !HAVE_ARGP_H */
39 #include "freeipmi-argp.h"
40 #endif /* !HAVE_ARGP_H */
41 #include <assert.h>
42 #include <errno.h>
43
44 #include "ipmidetect_.h"
45 #include "ipmidetect-argp.h"
46
47 #include "freeipmi-portability.h"
48 #include "error.h"
49 #include "fd.h"
50 #include "fi_hostlist.h"
51
52 const char *argp_program_version =
53 "ipmidetect - " PACKAGE_VERSION "\n"
54 "Copyright (C) 2007-2015 Lawrence Livermore National Security, LLC.\n"
55 "Copyright (C) 2007 The Regents of the University of California.\n"
56 "This program is free software; you may redistribute it under the terms of\n"
57 "the GNU General Public License. This program has absolutely no warranty.";
58
59 const char *argp_program_bug_address =
60 "<" PACKAGE_BUGREPORT ">";
61
62 static char cmdline_doc[] =
63 "ipmidetect - IPMI node detection client";
64
65 static char cmdline_args_doc[] = "";
66
67 static struct argp_option cmdline_options[] =
68 {
69 { "hostname", IPMIDETECT_HOSTNAME_KEY, "HOST", 0,
70 "Specify alternate server hostname", 1},
71 { "hostname_legacy", IPMIDETECT_LEGACY_HOSTNAME_KEY, "HOST", OPTION_HIDDEN,
72 "Specify alternate server hostname", 2},
73 { "port", IPMIDETECT_PORT_KEY, "PORT", 0,
74 "Specify alternate server port", 3},
75 { "detected", IPMIDETECT_DETECTED_KEY, 0, 0,
76 "Output only detected nodes", 4},
77 { "undetected", IPMIDETECT_UNDETECTED_KEY, 0, 0,
78 "Output only undetected nodes", 5},
79 { "hostrange", IPMIDETECT_HOSTRANGE_KEY, 0, 0,
80 "Output in hostrange format", 6},
81 { "comma", IPMIDETECT_COMMA_KEY, 0, 0,
82 "Output in comma separated format", 7},
83 { "newline", IPMIDETECT_NEWLINE_KEY, 0, 0,
84 "Output in newline separated format", 8},
85 { "space", IPMIDETECT_SPACE_KEY, 0, 0,
86 "Output in space separated format", 9},
87 { NULL, 0, NULL, 0, NULL, 0}
88 };
89
90 static error_t cmdline_parse (int key, char *arg, struct argp_state *state);
91
92 static struct argp cmdline_argp = { cmdline_options,
93 cmdline_parse,
94 cmdline_args_doc,
95 cmdline_doc };
96
97 #define IPMIDETECT_STDIN_BUFFERLEN 65536
98
99 static void
_push_inputted_nodes(struct ipmidetect_arguments * cmd_args,const char * nodes)100 _push_inputted_nodes (struct ipmidetect_arguments *cmd_args,
101 const char *nodes)
102 {
103 assert (cmd_args);
104 assert (nodes);
105
106 /* Error if nodes aren't short hostnames */
107 if (strchr (nodes, '.'))
108 err_exit ("nodes must be listed in short hostname format");
109
110 if (!fi_hostlist_push (cmd_args->inputted_nodes, nodes))
111 err_exit ("nodes improperly formatted");
112 }
113
114 static void
_read_nodes_from_stdin(struct ipmidetect_arguments * cmd_args)115 _read_nodes_from_stdin (struct ipmidetect_arguments *cmd_args)
116 {
117 char buf[IPMIDETECT_STDIN_BUFFERLEN];
118 int n;
119
120 assert (cmd_args);
121
122 memset (buf, '\0', IPMIDETECT_STDIN_BUFFERLEN);
123
124 if ((n = fd_read_n (STDIN_FILENO, buf, IPMIDETECT_STDIN_BUFFERLEN)) < 0)
125 err_exit ("error reading from standard input: %s", strerror (errno));
126
127 if (n == IPMIDETECT_STDIN_BUFFERLEN)
128 err_exit ("overflow in standard input buffer");
129
130 if (n > 0)
131 {
132 char *ptr = strtok (buf, " \t\n\0");
133 while (ptr)
134 {
135 _push_inputted_nodes (cmd_args, ptr);
136 ptr = strtok (NULL, " \t\n\0");
137 }
138 }
139 }
140
141 static error_t
cmdline_parse(int key,char * arg,struct argp_state * state)142 cmdline_parse (int key, char *arg, struct argp_state *state)
143 {
144 struct ipmidetect_arguments *cmd_args;
145 char *endptr;
146
147 assert (state);
148
149 cmd_args = state->input;
150
151 switch (key)
152 {
153 case IPMIDETECT_HOSTNAME_KEY:
154 case IPMIDETECT_LEGACY_HOSTNAME_KEY:
155 if (!(cmd_args->hostname = strdup (arg)))
156 err_exit ("strdup: %s", strerror (errno));
157 break;
158 case IPMIDETECT_PORT_KEY:
159 errno = 0;
160 cmd_args->port = strtol (arg, &endptr, 10);
161 if (errno
162 || endptr[0] != '\0')
163 err_exit ("invalid port specified");
164 break;
165 case IPMIDETECT_DETECTED_KEY:
166 cmd_args->output_type = IPMIDETECT_DETECTED_NODES;
167 break;
168 case IPMIDETECT_UNDETECTED_KEY:
169 cmd_args->output_type = IPMIDETECT_UNDETECTED_NODES;
170 break;
171 case IPMIDETECT_HOSTRANGE_KEY:
172 cmd_args->output_format = 0;
173 break;
174 case IPMIDETECT_COMMA_KEY:
175 cmd_args->output_format = ',';
176 break;
177 case IPMIDETECT_NEWLINE_KEY:
178 cmd_args->output_format = '\n';
179 break;
180 case IPMIDETECT_SPACE_KEY:
181 cmd_args->output_format = ' ';
182 break;
183 case ARGP_KEY_ARG:
184 if (!strcmp (arg, "-"))
185 _read_nodes_from_stdin (cmd_args);
186 else
187 _push_inputted_nodes (cmd_args, arg);
188 fi_hostlist_uniq (cmd_args->inputted_nodes);
189 break;
190 case ARGP_KEY_END:
191 break;
192 default:
193 return (ARGP_ERR_UNKNOWN);
194 }
195
196 return (0);
197 }
198
199 void
ipmidetect_argp_parse(int argc,char ** argv,struct ipmidetect_arguments * cmd_args)200 ipmidetect_argp_parse (int argc, char **argv, struct ipmidetect_arguments *cmd_args)
201 {
202 assert (argc >= 0);
203 assert (argv);
204 assert (cmd_args);
205
206 cmd_args->hostname = NULL;
207 cmd_args->port = 0;
208 cmd_args->output_type = IPMIDETECT_DETECTED_AND_UNDETECTED_NODES;
209 cmd_args->output_format = 0;
210
211 if (!(cmd_args->inputted_nodes = fi_hostlist_create (NULL)))
212 err_exit ("fi_hostlist_create");
213
214 argp_parse (&cmdline_argp,
215 argc,
216 argv,
217 ARGP_IN_ORDER,
218 NULL,
219 cmd_args);
220 }
221