-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsmc.c
157 lines (142 loc) · 3.45 KB
/
smc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* SMC Tools - Shared Memory Communication Tools
*
* Copyright IBM Corp. 2020
*
* Author(s): Guvenc Gulce <[email protected]>
*
* Userspace program for SMC Information display
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include "smctools_common.h"
#include "libnetlink.h"
#include "util.h"
#include "linkgroup.h"
#include "dev.h"
#include "ueid.h"
#include "seid.h"
#include "info.h"
#include "stats.h"
static int option_detail = 0;
#if defined(SMCD)
char *myname = "smcd";
#elif defined(SMCR)
char *myname = "smcr";
#else
char *myname = "smc";
#endif
static void version(void)
{
fprintf(stderr,
"%s utility, smc-tools-%s\n", myname, RELEASE_STRING);
exit(-1);
}
static void usage(void)
{
fprintf(stderr,
"Usage: %s [ OPTIONS ] OBJECT {COMMAND | help}\n"
#if defined(SMCD)
"where OBJECT := {info | linkgroup | device | stats | ueid | seid}\n"
" OPTIONS := {-v[ersion] | -d[etails] | -a[bsolute]}\n", myname);
#else
"where OBJECT := {info | linkgroup | device | stats | ueid}\n"
" OPTIONS := {-v[ersion] | -d[etails] | -dd[etails] | -a[bsolute]}\n", myname);
#endif
}
static int invoke_help(int argc, char **argv, int k)
{
usage();
return 0;
}
static const struct cmd {
const char *cmd;
int (*func)(int argc, char **argv, int option_detail);
} cmds[] = {
{ "device", invoke_devs },
{ "linkgroup", invoke_lgs },
{ "info", invoke_info },
{ "stats", invoke_stats },
{ "ueid", invoke_ueid },
#if defined(SMCD)
{ "seid", invoke_seid },
#endif
{ "help", invoke_help },
{ 0 }
};
static int run_cmd(const char *argv0, int argc, char **argv)
{
const struct cmd *c;
for (c = cmds; c->cmd; ++c) {
if (contains(argv0, c->cmd) == 0)
return -(c->func(argc-1, argv+1, option_detail));
}
#if defined(SMCR)
/* Special warning for those who mixed up smcd and smcr */
if (contains(argv0, "seid") == 0) {
fprintf(stderr,
"Error: Object \"%s\" is valid for SMC-D only, try \"%s help\".\n",
argv0, myname);
return EXIT_FAILURE;
}
#endif
fprintf(stderr, "Error: Object \"%s\" is unknown, try \"%s help\".\n", argv0, myname);
return EXIT_FAILURE;
}
int main(int argc, char **argv)
{
int rc = 0;
while (argc > 1) {
char *opt = argv[1];
if (strcmp(opt, "--") == 0) {
argc--; argv++;
break;
}
if (opt[0] != '-')
break;
if (opt[1] == '-')
opt++;
if ((strncmp(opt, "-ad", 3) == 0) || (strncmp(opt, "-da", 3) == 0)) {
option_detail = SMC_OPTION_DETAIL_ABS;
} else if (contains(opt, "-absolute") == 0) {
option_detail = SMC_OPTION_ABS;
} else if (contains(opt, "-version") == 0) {
version();
} else if (contains(opt, "-details") == 0) {
option_detail = SMC_DETAIL_LEVEL_V;
} else if (contains(opt, "-ddetails") == 0) {
option_detail = SMC_DETAIL_LEVEL_VV;
} else if (contains(opt, "-help") == 0) {
usage();
goto out;
} else {
fprintf(stderr,
"Error: Option \"%s\" is unknown, try \"%s help\".\n",
opt, myname);
exit(-1);
}
argc--; argv++;
}
if (gen_nl_open(myname))
exit(1);
if (argc > 1) {
rc = run_cmd(argv[1], argc-1, argv+1);
goto out;
}
usage();
out:
gen_nl_close();
return rc;
}