1 /* -*- mode: c; c-file-style: "openbsd" -*- */
3 * Copyright (c) 2008 Vincent Bernat <bernat@luffy.cx>
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #include <sys/types.h>
27 #include <sys/socket.h>
29 #include <arpa/inet.h>
33 #include <sys/queue.h>
37 #ifdef HAVE___PROGNAME
38 extern const char *__progname
;
40 # define __progname "lldpcli"
43 /* Global for completion */
44 static struct cmd_node
*root
= NULL
;
45 const char *ctlname
= NULL
;
48 is_lldpctl(const char *name
)
50 static int last_result
= -1;
51 if (last_result
== -1 && name
) {
52 char *basec
= strdup(name
);
54 char *bname
= basename(basec
);
55 last_result
= (!strcmp(bname
, "lldpctl"));
58 return (last_result
== -1)?0:last_result
;
64 fprintf(stderr
, "Usage: %s [OPTIONS ...] [COMMAND ...]\n", __progname
);
65 fprintf(stderr
, "Version: %s\n", PACKAGE_STRING
);
67 fprintf(stderr
, "\n");
69 fprintf(stderr
, "-d Enable more debugging information.\n");
70 fprintf(stderr
, "-u socket Specify the Unix-domain socket used for communication with lldpd(8).\n");
71 fprintf(stderr
, "-f format Choose output format (plain, keyvalue, json or xml).\n");
72 if (!is_lldpctl(NULL
))
73 fprintf(stderr
, "-c conf Read the provided configuration file.\n");
75 fprintf(stderr
, "\n");
77 fprintf(stderr
, "see manual page lldpcli(8) for more information\n");
84 /* Check we can access the control socket with read/write
85 * privileges. The `access()` function uses the real UID and real GID,
86 * therefore we don't have to mangle with our identity. */
87 return (ctlname
&& access(ctlname
, R_OK
|W_OK
) == 0);
94 int privileged
= is_privileged();
95 if (isatty(STDIN_FILENO
)) {
97 return "[lldpcli] # ";
98 return "[lldpcli] $ ";
103 static int must_exit
= 0;
105 * Exit the interpreter.
108 cmd_exit(struct lldpctl_conn_t
*conn
, struct writer
*w
,
109 struct cmd_env
*env
, void *arg
)
111 log_info("lldpctl", "quit lldpcli");
117 * Send an "update" request.
120 cmd_update(struct lldpctl_conn_t
*conn
, struct writer
*w
,
121 struct cmd_env
*env
, void *arg
)
123 log_info("lldpctl", "ask for global update");
125 lldpctl_atom_t
*config
= lldpctl_get_configuration(conn
);
126 if (config
== NULL
) {
127 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
128 lldpctl_last_strerror(conn
));
131 if (lldpctl_atom_set_int(config
,
132 lldpctl_k_config_tx_interval
, -1) == NULL
) {
133 log_warnx("lldpctl", "unable to ask lldpd for immediate retransmission. %s",
134 lldpctl_last_strerror(conn
));
135 lldpctl_atom_dec_ref(config
);
138 log_info("lldpctl", "immediate retransmission requested successfuly");
139 lldpctl_atom_dec_ref(config
);
144 * Pause or resume execution of lldpd.
146 * @param conn The connection to lldpd.
147 * @param pause 1 if we want to pause lldpd, 0 otherwise
148 * @return 1 on success, 0 on error
151 cmd_pause_resume(lldpctl_conn_t
*conn
, int pause
)
153 lldpctl_atom_t
*config
= lldpctl_get_configuration(conn
);
154 if (config
== NULL
) {
155 log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
156 lldpctl_last_strerror(conn
));
159 if (lldpctl_atom_get_int(config
, lldpctl_k_config_paused
) == pause
) {
160 log_debug("lldpctl", "lldpd is already %s",
161 pause
?"paused":"resumed");
162 lldpctl_atom_dec_ref(config
);
165 if (lldpctl_atom_set_int(config
,
166 lldpctl_k_config_paused
, pause
) == NULL
) {
167 log_warnx("lldpctl", "unable to ask lldpd to %s operations. %s",
168 pause
?"pause":"resume",
169 lldpctl_last_strerror(conn
));
170 lldpctl_atom_dec_ref(config
);
173 log_info("lldpctl", "lldpd should %s operations",
174 pause
?"pause":"resume");
175 lldpctl_atom_dec_ref(config
);
179 cmd_pause(struct lldpctl_conn_t
*conn
, struct writer
*w
,
180 struct cmd_env
*env
, void *arg
) {
182 return cmd_pause_resume(conn
, 1);
185 cmd_resume(struct lldpctl_conn_t
*conn
, struct writer
*w
,
186 struct cmd_env
*env
, void *arg
) {
188 return cmd_pause_resume(conn
, 0);
192 #ifdef HAVE_LIBREADLINE
194 _cmd_complete(int all
)
199 size_t len
= strlen(rl_line_buffer
);
200 char *line
= malloc(len
+ 2);
201 if (!line
) return -1;
202 strlcpy(line
, rl_line_buffer
, len
+ 2);
203 line
[rl_point
] = 2; /* empty character, will force a word */
204 line
[rl_point
+1] = 0;
206 if (tokenize_line(line
, &argc
, &argv
) != 0)
209 char *compl = commands_complete(root
, argc
, (const char **)argv
, all
, is_privileged());
210 if (compl && strlen(argv
[argc
-1]) < strlen(compl)) {
211 if (rl_insert_text(compl + strlen(argv
[argc
-1])) < 0) {
219 /* No completion or several completion available. */
221 fprintf(stderr
, "\n");
222 rl_forced_update_display();
226 tokenize_free(argc
, argv
);
231 cmd_complete(int count
, int ch
)
233 return _cmd_complete(0);
237 cmd_help(int count
, int ch
)
239 return _cmd_complete(1);
243 readline(const char *p
)
245 static char line
[2048];
246 fprintf(stderr
, "%s", p
);
248 if (fgets(line
, sizeof(line
) - 2, stdin
) == NULL
)
255 * Execute a tokenized command and display its output.
257 * @param conn The connection to lldpd.
258 * @param fmt Output format.
259 * @param argc Number of arguments.
260 * @param argv Array of arguments.
261 * @return 0 if an error occurred, 1 otherwise
264 cmd_exec(lldpctl_conn_t
*conn
, const char *fmt
, int argc
, const char **argv
)
266 /* Init output formatter */
269 if (strcmp(fmt
, "plain") == 0) w
= txt_init(stdout
);
270 else if (strcmp(fmt
, "keyvalue") == 0) w
= kv_init(stdout
);
272 else if (strcmp(fmt
, "xml") == 0) w
= xml_init(stdout
);
275 else if (strcmp(fmt
, "json") == 0) w
= jansson_init(stdout
);
278 else if (strcmp(fmt
, "json") == 0) w
= jsonc_init(stdout
);
280 else w
= txt_init(stdout
);
282 /* Execute command */
283 int rc
= commands_execute(conn
, w
,
284 root
, argc
, argv
, is_privileged());
286 log_info("lldpctl", "an error occurred while executing last command");
295 * Execute a command line and display its output.
297 * @param conn The connection to lldpd.
298 * @param fmt Output format.
299 * @param line Line to execute.
300 * @return -1 if an error occurred, 0 if nothing was executed. 1 otherwise.
303 parse_and_exec(lldpctl_conn_t
*conn
, const char *fmt
, const char *line
)
305 int cargc
= 0; char **cargv
= NULL
;
307 log_debug("lldpctl", "tokenize command line");
308 n
= tokenize_line(line
, &cargc
, &cargv
);
311 log_warnx("lldpctl", "internal error while tokenizing");
314 log_warnx("lldpctl", "unmatched quotes");
318 n
= cmd_exec(conn
, fmt
, cargc
, (const char **)cargv
);
319 tokenize_free(cargc
, cargv
);
320 return (cargc
== 0)?0:
325 static struct cmd_node
*
328 root
= commands_root();
329 register_commands_show(root
);
330 register_commands_watch(root
);
331 commands_privileged(commands_new(
332 commands_new(root
, "update", "Update information and send LLDPU on all ports",
334 NEWLINE
, "Update information and send LLDPU on all ports",
335 NULL
, cmd_update
, NULL
));
336 register_commands_configure(root
);
337 commands_hidden(commands_new(root
, "complete", "Get possible completions from a given command",
338 NULL
, cmd_store_env_and_pop
, "complete"));
339 commands_new(root
, "help", "Get help on a possible command",
340 NULL
, cmd_store_env_and_pop
, "help");
342 commands_new(root
, "pause", "Pause lldpd operations", NULL
, NULL
, NULL
),
343 NEWLINE
, "Pause lldpd operations", NULL
, cmd_pause
, NULL
);
345 commands_new(root
, "resume", "Resume lldpd operations", NULL
, NULL
, NULL
),
346 NEWLINE
, "Resume lldpd operations", NULL
, cmd_resume
, NULL
);
348 commands_new(root
, "exit", "Exit interpreter", NULL
, NULL
, NULL
),
349 NEWLINE
, "Exit interpreter", NULL
, cmd_exit
, NULL
);
354 TAILQ_ENTRY(input
) next
;
357 TAILQ_HEAD(inputs
, input
);
359 filter(const struct dirent
*dir
)
361 if (strlen(dir
->d_name
) < 5) return 0;
362 if (strcmp(dir
->d_name
+ strlen(dir
->d_name
) - 5, ".conf")) return 0;
367 * Append a new input file/directory to the list of inputs.
369 * @param arg Directory or file name to add.
370 * @param inputs List of inputs
371 * @param acceptdir 1 if we accept a directory, 0 otherwise
374 input_append(const char *arg
, struct inputs
*inputs
, int acceptdir
)
377 if (stat(arg
, &statbuf
) == -1) {
378 log_info("lldpctl", "cannot find configuration file/directory %s",
383 if (!S_ISDIR(statbuf
.st_mode
)) {
384 struct input
*input
= malloc(sizeof(struct input
));
386 log_warn("lldpctl", "not enough memory to process %s",
390 log_debug("lldpctl", "input: %s", arg
);
391 input
->name
= strdup(arg
);
392 TAILQ_INSERT_TAIL(inputs
, input
, next
);
396 log_debug("lldpctl", "skip directory %s",
401 struct dirent
**namelist
= NULL
;
402 int n
= scandir(arg
, &namelist
, filter
, alphasort
);
404 log_warnx("lldpctl", "unable to read directory %s",
408 for (int i
=0; i
< n
; i
++) {
410 if (asprintf(&fullname
, "%s/%s", arg
, namelist
[i
]->d_name
) != -1) {
411 input_append(fullname
, inputs
, 0);
420 main(int argc
, char *argv
[])
422 int ch
, debug
= 1, rc
= EXIT_FAILURE
;
423 const char *fmt
= "plain";
424 lldpctl_conn_t
*conn
= NULL
;
425 const char *options
= is_lldpctl(argv
[0])?"hdvf:":"hdsvf:c:u:";
428 struct inputs inputs
;
431 ctlname
= lldpctl_get_default_transport();
433 signal(SIGHUP
, SIG_IGN
);
435 /* Initialize logging */
436 while ((ch
= getopt(argc
, argv
, options
)) != -1) {
438 case 'd': debug
++; break;
439 case 's': debug
--; break;
442 log_init(debug
, __progname
);
444 /* Get and parse command line options */
446 while ((ch
= getopt(argc
, argv
, options
)) != -1) {
457 fprintf(stdout
, "%s\n", PACKAGE_VERSION
);
465 input_append(optarg
, &inputs
, 1);
472 /* Disable SIGPIPE */
473 signal(SIGPIPE
, SIG_IGN
);
475 /* Register commands */
476 root
= register_commands();
478 /* Make a connection */
479 log_debug("lldpctl", "connect to lldpd");
480 conn
= lldpctl_new_name(ctlname
, NULL
, NULL
, NULL
);
481 if (conn
== NULL
) goto end
;
483 /* Process file inputs */
484 while (gotinputs
&& !TAILQ_EMPTY(&inputs
)) {
485 /* coverity[use_after_free]
486 TAILQ_REMOVE does the right thing */
487 struct input
*first
= TAILQ_FIRST(&inputs
);
488 log_debug("lldpctl", "process: %s", first
->name
);
489 FILE *file
= fopen(first
->name
, "r");
493 while ((line
= fgetln(file
, &len
))) {
494 line
= strndup(line
, len
);
495 if (line
[len
- 1] == '\n') {
496 line
[len
- 1] = '\0';
498 parse_and_exec(conn
, fmt
, line
);
504 log_warn("lldpctl", "unable to open %s",
507 TAILQ_REMOVE(&inputs
, first
, next
);
512 /* Process additional arguments. First if we are lldpctl (interfaces) */
513 if (is_lldpctl(NULL
)) {
515 for (int i
= optind
; i
< argc
; i
++) {
517 if (asprintf(&line
, "%s%s%s",
518 prev
?prev
:"show neigh ports ", argv
[i
],
519 (i
== argc
- 1)?" details":",") == -1) {
520 log_warnx("lldpctl", "not enough memory to build list of interfaces");
526 if (line
== NULL
&& (line
= strdup("show neigh details")) == NULL
) {
527 log_warnx("lldpctl", "not enough memory to build command line");
530 log_debug("lldpctl", "execute %s", line
);
531 if (parse_and_exec(conn
, fmt
, line
) != -1)
537 /* Then, if we are regular lldpcli (command line) */
541 cargv
= &((const char **)argv
)[optind
];
542 cargc
= argc
- optind
;
543 if (cmd_exec(conn
, fmt
, cargc
, cargv
) != -1)
553 /* Interactive session */
554 #ifdef HAVE_LIBREADLINE
555 rl_bind_key('?', cmd_help
);
556 rl_bind_key('\t', cmd_complete
);
560 if ((line
= readline(prompt()))) {
561 int n
= parse_and_exec(conn
, fmt
, line
);
563 #ifdef HAVE_READLINE_HISTORY
564 if (n
!= 0) add_history(line
);
567 } while (!must_exit
&& line
!= NULL
);
571 while (!TAILQ_EMPTY(&inputs
)) {
572 /* coverity[use_after_free]
573 TAILQ_REMOVE does the right thing */
574 struct input
*first
= TAILQ_FIRST(&inputs
);
575 TAILQ_REMOVE(&inputs
, first
, next
);
579 if (conn
) lldpctl_release(conn
);
580 if (root
) commands_free(root
);