2
* src/utils.c Utilities
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation version 2.1
9
* Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
13
* @defgroup cli Command Line Interface API
17
* These modules provide an interface for text based applications. The
18
* functions provided are wrappers for their libnl equivalent with
19
* added error handling. The functions check for allocation failures,
20
* invalid input, and unknown types and will print error messages
21
* accordingly via nl_cli_fatal().
24
#include <netlink/cli/utils.h>
27
* Parse a text based 32 bit unsigned integer argument
28
* @arg arg Integer in text form.
30
* Tries to convert the number provided in arg to a uint32_t. Will call
31
* nl_cli_fatal() if the conversion fails.
33
* @return 32bit unsigned integer.
35
uint32_t nl_cli_parse_u32(const char *arg)
40
lval = strtoul(arg, &endptr, 0);
41
if (endptr == arg || lval == ULONG_MAX)
42
nl_cli_fatal(EINVAL, "Unable to parse \"%s\", not a number.",
45
return (uint32_t) lval;
48
void nl_cli_print_version(void)
50
printf("libnl tools version %s\n", LIBNL_VERSION);
52
"Copyright (C) 2003-2010 Thomas Graf <tgraf@redhat.com>\n"
54
"This program comes with ABSOLUTELY NO WARRANTY. This is free \n"
55
"software, and you are welcome to redistribute it under certain\n"
56
"conditions. See the GNU General Public License for details.\n"
63
* Print error message and quit application
64
* @arg err Error code.
65
* @arg fmt Error message.
67
* Prints the formatted error message to stderr and quits the application
68
* using the provided error code.
70
void nl_cli_fatal(int err, const char *fmt, ...)
74
fprintf(stderr, "Error: ");
78
vfprintf(stderr, fmt, ap);
80
fprintf(stderr, "\n");
82
fprintf(stderr, "%s\n", strerror(err));
87
int nl_cli_connect(struct nl_sock *sk, int protocol)
91
if ((err = nl_connect(sk, protocol)) < 0)
92
nl_cli_fatal(err, "Unable to connect netlink socket: %s",
98
struct nl_sock *nl_cli_alloc_socket(void)
100
struct nl_sock *sock;
102
if (!(sock = nl_socket_alloc()))
103
nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket");
108
struct nl_addr *nl_cli_addr_parse(const char *str, int family)
110
struct nl_addr *addr;
113
if ((err = nl_addr_parse(str, family, &addr)) < 0)
114
nl_cli_fatal(err, "Unable to parse address \"%s\": %s",
115
str, nl_geterror(err));
120
int nl_cli_parse_dumptype(const char *str)
122
if (!strcasecmp(str, "brief"))
124
else if (!strcasecmp(str, "details") || !strcasecmp(str, "detailed"))
125
return NL_DUMP_DETAILS;
126
else if (!strcasecmp(str, "stats"))
127
return NL_DUMP_STATS;
129
nl_cli_fatal(EINVAL, "Invalid dump type \"%s\".\n", str);
134
int nl_cli_confirm(struct nl_object *obj, struct nl_dump_params *params,
137
nl_object_dump(obj, params);
140
char buf[32] = { 0 };
143
printf("Delete? (%c/%c) ",
144
default_yes ? 'Y' : 'y',
145
default_yes ? 'n' : 'N');
147
if (!fgets(buf, sizeof(buf), stdin)) {
148
fprintf(stderr, "Error while reading\n.");
152
switch ((answer = tolower(buf[0]))) {
154
answer = default_yes ? 'y' : 'n';
157
return answer == 'y';
160
fprintf(stderr, "Invalid input, try again.\n");
167
struct nl_cache *nl_cli_alloc_cache(struct nl_sock *sock, const char *name,
168
int (*ac)(struct nl_sock *, struct nl_cache **))
170
struct nl_cache *cache;
173
if ((err = ac(sock, &cache)) < 0)
174
nl_cli_fatal(err, "Unable to allocate %s cache: %s",
175
name, nl_geterror(err));
177
nl_cache_mngt_provide(cache);
182
void nl_cli_load_module(const char *prefix, const char *name)
184
char path[FILENAME_MAX+1];
187
snprintf(path, sizeof(path), "%s/%s/%s.so",
188
PKGLIBDIR, prefix, name);
190
if (!(handle = dlopen(path, RTLD_NOW)))
191
nl_cli_fatal(ENOENT, "Unable to load module \"%s\": %s\n",