-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path00-cnt.c
78 lines (66 loc) · 1.89 KB
/
00-cnt.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
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include <getopt.h>
/**
* Struct with configuration for child process run by main process.
*/
struct proc_info {
int argc; // Number of args for child process.
char **argv; // Args for child process.
char *hostname; // Hostname of child process.
};
bool choose_hostname(char hostname[256], size_t i);
/**
* Entry point for cnt.c program.
* It runs the process from executable given by user in separated, isolated environment (container).
*/
int main(int argc, char **argv) {
/* Store proc info */
struct proc_info config = {0};
/* Error code to return from main program */
int err = 0;
/* Used to store flag given by user. */
int option = 0;
int lastopt = 0;
while ((option = getopt(argc, argv, "c:"))) {
switch (option) {
// -c <process-to-be-run> (e.g. -c /bin/sh)
case 'c':
config.argc = argc - lastopt - 1;
config.argv = &argv[argc - config.argc];
goto finish_config;
default:
goto usage;
}
}
finish_config:
if (!config.argc) goto usage;
/* Choosing hostname for container. */
fprintf(stdout, "=> choosing hostname for container..\n");
char hostname[10] = {0};
if (choose_hostname(hostname, sizeof(hostname)))
goto err;
config.hostname = hostname;
fprintf(stdout, "=> Hostname: %s\n", config.hostname);
goto cleanup;
usage:
fprintf(stderr, "Usage: %s -c /bin/sh ~\n", argv[0]);
err:
err = 1;
cleanup:
fprintf(stdout, "Done.\n");
return err;
}
/**
* Generates hostname for container.
* @param buff
* @param len
* @return Success.
*/
bool choose_hostname(char *buff, size_t len) {
struct timespec now = {0};
clock_gettime(CLOCK_MONOTONIC, &now);
snprintf(buff, len, "%s-%05lx", "cnt", now.tv_sec);
return 0;
}