1 | /*
|
---|
2 | * Copyright (c) 2015 Michal Koutny
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup sysctl
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file Control system manager.
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <errno.h>
|
---|
36 | #include <stdio.h>
|
---|
37 | #include <stdlib.h>
|
---|
38 | #include <str.h>
|
---|
39 | #include <sysman/ctl.h>
|
---|
40 |
|
---|
41 | #define NAME "sysctl"
|
---|
42 | #define NAME_BUFFER 256
|
---|
43 |
|
---|
44 | typedef struct {
|
---|
45 | const char *name;
|
---|
46 | int args;
|
---|
47 | int (* handler)(int, char **);
|
---|
48 | } command_t;
|
---|
49 |
|
---|
50 | static const char *unit_state(unit_state_t s)
|
---|
51 | {
|
---|
52 | switch (s) {
|
---|
53 | case STATE_EMBRYO:
|
---|
54 | return "embryo";
|
---|
55 | case STATE_STARTING:
|
---|
56 | return "starting";
|
---|
57 | case STATE_STARTED:
|
---|
58 | return "started";
|
---|
59 | case STATE_STOPPED:
|
---|
60 | return "stopped";
|
---|
61 | case STATE_FAILED:
|
---|
62 | return "failed";
|
---|
63 | default:
|
---|
64 | return "<unknown>";
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | static int list_units(int argc, char *argv[])
|
---|
69 | {
|
---|
70 | unit_handle_t *units;
|
---|
71 | size_t unit_cnt;
|
---|
72 |
|
---|
73 | int rc = sysman_get_units(&units, &unit_cnt);
|
---|
74 | if (rc != EOK) {
|
---|
75 | return rc;
|
---|
76 | }
|
---|
77 |
|
---|
78 | for (unit_handle_t *it = units; it - units < (int)unit_cnt; ++it) {
|
---|
79 | char name[NAME_BUFFER];
|
---|
80 | unit_state_t state;
|
---|
81 |
|
---|
82 | rc = sysman_unit_get_name(*it, name, NAME_BUFFER);
|
---|
83 | if (rc != EOK)
|
---|
84 | goto fail;
|
---|
85 |
|
---|
86 | rc = sysman_unit_get_state(*it, &state);
|
---|
87 | if (rc != EOK)
|
---|
88 | goto fail;
|
---|
89 |
|
---|
90 | printf("%-25s\t%s\n", name, unit_state(state));
|
---|
91 | continue;
|
---|
92 | fail:
|
---|
93 | printf(" -- unit skipped due to IPC error (%i) --\n", rc);
|
---|
94 | }
|
---|
95 |
|
---|
96 | return 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | command_t commands[] = {
|
---|
100 | { "list-units", 0, &list_units },
|
---|
101 | { 0 }
|
---|
102 | };
|
---|
103 |
|
---|
104 | static void print_syntax(void)
|
---|
105 | {
|
---|
106 | printf("syntax:\n");
|
---|
107 | printf("\t%s\n", NAME);
|
---|
108 | // TODO update as functionality grows
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | int main(int argc, char *argv[])
|
---|
113 | {
|
---|
114 | if (argc == 1) {
|
---|
115 | print_syntax();
|
---|
116 | return 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 | for (command_t *it = commands; it->name != NULL; ++it) {
|
---|
120 | if (str_cmp(it->name, argv[1])) {
|
---|
121 | continue;
|
---|
122 | }
|
---|
123 |
|
---|
124 | int real_args = argc - 2;
|
---|
125 | if (it->args < real_args) {
|
---|
126 | printf("%s %s: too many arguments\n", NAME, it->name);
|
---|
127 | return 1;
|
---|
128 | } else if (it->args > real_args) {
|
---|
129 | printf("%s %s: too few arguments\n", NAME, it->name);
|
---|
130 | return 1;
|
---|
131 | } else {
|
---|
132 | return it->handler(real_args + 1, argv + 1);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | printf("%s: unknown command '%s'\n", NAME, argv[1]);
|
---|
137 | return 1;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** @}
|
---|
141 | */
|
---|