X Tutup
Skip to content

Commit 88325bf

Browse files
yuwatakeszybz
authored andcommitted
cgls: use static destructor and DEFINE_MAIN_FUNCTION() macro
1 parent 2428944 commit 88325bf

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

src/cgls/cgls.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "cgroup-util.h"
1515
#include "fileio.h"
1616
#include "log.h"
17+
#include "main-func.h"
1718
#include "output-mode.h"
1819
#include "pager.h"
1920
#include "path-util.h"
@@ -36,6 +37,8 @@ static char **arg_names = NULL;
3637
static int arg_full = -1;
3738
static const char* arg_machine = NULL;
3839

40+
STATIC_DESTRUCTOR_REGISTER(arg_names, freep); /* don't free the strings */
41+
3942
static int help(void) {
4043
_cleanup_free_ char *link = NULL;
4144
int r;
@@ -160,15 +163,15 @@ static void show_cg_info(const char *controller, const char *path) {
160163
fflush(stdout);
161164
}
162165

163-
int main(int argc, char *argv[]) {
166+
static int run(int argc, char *argv[]) {
164167
int r, output_flags;
165168

166169
log_parse_environment();
167170
log_open();
168171

169172
r = parse_argv(argc, argv);
170173
if (r <= 0)
171-
goto finish;
174+
return r;
172175

173176
r = pager_open(arg_pager_flags);
174177
if (r > 0 && arg_full < 0)
@@ -196,10 +199,8 @@ int main(int argc, char *argv[]) {
196199
r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL,
197200
arg_show_unit == SHOW_UNIT_USER,
198201
&bus);
199-
if (r < 0) {
200-
log_error_errno(r, "Failed to create bus connection: %m");
201-
goto finish;
202-
}
202+
if (r < 0)
203+
return log_error_errno(r, "Failed to create bus connection: %m");
203204
}
204205

205206
q = show_cgroup_get_unit_path_and_warn(bus, *name, &cgroup);
@@ -231,7 +232,7 @@ int main(int argc, char *argv[]) {
231232
/* Query root only if needed, treat error as fatal */
232233
r = show_cgroup_get_path_and_warn(arg_machine, NULL, &root);
233234
if (r < 0)
234-
goto finish;
235+
return log_error_errno(r, "Failed to list cgroup tree: %m");
235236
}
236237

237238
q = cg_split_spec(*name, &c, &p);
@@ -243,10 +244,8 @@ int main(int argc, char *argv[]) {
243244
controller = c ?: SYSTEMD_CGROUP_CONTROLLER;
244245
if (p) {
245246
j = strjoin(root, "/", p);
246-
if (!j) {
247-
r = log_oom();
248-
goto finish;
249-
}
247+
if (!j)
248+
return log_oom();
250249

251250
path_simplify(j, false);
252251
path = j;
@@ -270,10 +269,8 @@ int main(int argc, char *argv[]) {
270269
_cleanup_free_ char *cwd = NULL;
271270

272271
r = safe_getcwd(&cwd);
273-
if (r < 0) {
274-
log_error_errno(r, "Cannot determine current working directory: %m");
275-
goto finish;
276-
}
272+
if (r < 0)
273+
return log_error_errno(r, "Cannot determine current working directory: %m");
277274

278275
if (path_startswith(cwd, "/sys/fs/cgroup")) {
279276
printf("Working directory %s:\n", cwd);
@@ -289,21 +286,18 @@ int main(int argc, char *argv[]) {
289286

290287
r = show_cgroup_get_path_and_warn(arg_machine, NULL, &root);
291288
if (r < 0)
292-
goto finish;
289+
return log_error_errno(r, "Failed to list cgroup tree: %m");
293290

294291
show_cg_info(SYSTEMD_CGROUP_CONTROLLER, root);
295292

296293
printf("-.slice\n");
297294
r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, root, NULL, 0, output_flags);
298295
}
299296
}
300-
301297
if (r < 0)
302-
log_error_errno(r, "Failed to list cgroup tree: %m");
303-
304-
finish:
305-
pager_close();
306-
free(arg_names); /* don't free the strings */
298+
return log_error_errno(r, "Failed to list cgroup tree: %m");
307299

308-
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
300+
return 0;
309301
}
302+
303+
DEFINE_MAIN_FUNCTION(run);

0 commit comments

Comments
 (0)
X Tutup