Skip to content

Commit e6fa331

Browse files
committed
Refactor the CLI and split it into multiple files
1 parent fa9853b commit e6fa331

File tree

10 files changed

+742
-701
lines changed

10 files changed

+742
-701
lines changed

Makefile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,16 @@ LIB_RPC_SRCS := $(SRCS_DIR)/driver_rpc.h \
7373
$(SRCS_DIR)/driver_svc.c \
7474
$(SRCS_DIR)/driver_clt.c
7575

76-
BIN_SRCS := $(SRCS_DIR)/nvc_cli.c \
76+
BIN_SRCS := $(SRCS_DIR)/cli/common.c \
77+
$(SRCS_DIR)/cli/configure.c \
78+
$(SRCS_DIR)/cli/dsl.c \
79+
$(SRCS_DIR)/cli/list.c \
80+
$(SRCS_DIR)/cli/main.c \
7781
$(SRCS_DIR)/error_generic.c \
78-
$(SRCS_DIR)/dsl.c \
7982
$(SRCS_DIR)/utils.c
8083

8184
LIB_SCRIPT = $(SRCS_DIR)/$(LIB_NAME).lds
82-
BIN_SCRIPT = $(SRCS_DIR)/$(BIN_NAME).lds
85+
BIN_SCRIPT = $(SRCS_DIR)/cli/$(BIN_NAME).lds
8386

8487
##### Target definitions #####
8588

@@ -147,7 +150,7 @@ LIB_LDLIBS = $(LIB_LDLIBS_STATIC) $(LIB_LDLIBS_SHARED)
147150

148151
# Binary flags (recursively expanded to handle target-specific flags)
149152
BIN_CPPFLAGS = -include $(BUILD_DEFS) $(CPPFLAGS)
150-
BIN_CFLAGS = -fPIE -flto $(CFLAGS)
153+
BIN_CFLAGS = -I$(SRCS_DIR) -fPIE -flto $(CFLAGS)
151154
BIN_LDFLAGS = -L. -pie $(LDFLAGS)
152155
BIN_LDLIBS = -l:$(LIB_SHARED) -lcap $(LDLIBS)
153156

src/cli/cli.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
3+
*/
4+
5+
#ifndef HEADER_CLI_H
6+
#define HEADER_CLI_H
7+
8+
#include <argp.h>
9+
#include <stdbool.h>
10+
#include <unistd.h>
11+
12+
#include "nvc_internal.h"
13+
14+
#include "error_generic.h"
15+
16+
struct context;
17+
18+
struct command {
19+
const char *name;
20+
const struct argp *argp;
21+
int (*func)(const struct context *);
22+
};
23+
24+
struct context {
25+
uid_t uid;
26+
gid_t gid;
27+
pid_t pid;
28+
char *rootfs;
29+
char *devices;
30+
char *reqs[32];
31+
size_t nreqs;
32+
char *ldconfig;
33+
bool load_kmods;
34+
bool list_info;
35+
char *init_flags;
36+
char *driver_flags;
37+
char *device_flags;
38+
char *container_flags;
39+
40+
const struct command *command;
41+
};
42+
43+
int select_devices(struct error *, char *, const struct nvc_device *[],
44+
const struct nvc_device [], size_t);
45+
46+
extern const struct argp list_usage;
47+
extern const struct argp configure_usage;
48+
49+
int list_command(const struct context *);
50+
int configure_command(const struct context *);
51+
52+
#endif /* HEADER_CLI_H */

src/cli/common.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
3+
*/
4+
5+
#include <inttypes.h>
6+
#include <string.h>
7+
8+
#include "cli.h"
9+
10+
int
11+
select_devices(struct error *err, char *devs, const struct nvc_device *selected[],
12+
const struct nvc_device available[], size_t size)
13+
{
14+
char *gpu, *ptr;
15+
size_t i;
16+
uintmax_t n;
17+
18+
while ((gpu = strsep(&devs, ",")) != NULL) {
19+
if (*gpu == '\0')
20+
continue;
21+
if (!strcasecmp(gpu, "all")) {
22+
for (i = 0; i < size; ++i)
23+
selected[i] = &available[i];
24+
break;
25+
}
26+
if (!strncasecmp(gpu, "GPU-", strlen("GPU-")) && strlen(gpu) > strlen("GPU-")) {
27+
for (i = 0; i < size; ++i) {
28+
if (!strncasecmp(available[i].uuid, gpu, strlen(gpu))) {
29+
selected[i] = &available[i];
30+
goto next;
31+
}
32+
}
33+
} else {
34+
n = strtoumax(gpu, &ptr, 10);
35+
if (*ptr == '\0' && n < UINTMAX_MAX && (size_t)n < size) {
36+
selected[n] = &available[n];
37+
goto next;
38+
}
39+
}
40+
error_setx(err, "unknown device id: %s", gpu);
41+
return (-1);
42+
next: ;
43+
}
44+
return (0);
45+
}

0 commit comments

Comments
 (0)