-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
95 lines (81 loc) · 2.62 KB
/
main.cpp
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
// Copyright (C) 2019 by Yuri Victorovich. All rights reserved.
#include "args.h"
#include "spec.h"
#include "util.h"
#include "err.h"
#include "misc.h"
#include "commands.h"
#include <rang.hpp>
#include <unistd.h>
#include <iostream>
static int mainGuarded(int argc, char** argv) {
//
// can only run as a privileged user because we need to run chroot(8) and need to create jails
//
if (::geteuid() != 0) {
std::cerr << rang::fg::red << "crate has to run as a regular user setuid to root"
<< " (you ran it just as a regular user with UID=" << ::geteuid() << ")"
<< rang::style::reset << std::endl;
return 1;
}
if (::getuid() == 0) {
std::cerr << rang::fg::red << "crate has to run as a regular user setuid to root"
<< " (you ran it just as root, this isn't yet supported)"
<< rang::style::reset << std::endl;
return 1;
}
//
// Can't run in jail because we need to create jails ourselves
//
if (Util::getSysctlInt("security.jail.jailed") != 0) {
std::cerr << rang::fg::red << "crate can not run in jail" << rang::style::reset << std::endl;
return 1;
}
//
// adjust uid, make it equal to euid
//
Util::ckSyscallError(::setuid(::geteuid()), "setuid", "geteuid()");
//
// create the jails directory if it doesn't yet exist
//
createJailsDirectoryIfNeeded();
//
// parse the arguments
//
unsigned numArgsProcessed = 0;
Args args = parseArguments(argc, argv, numArgsProcessed);
args.validate();
//
// run the requested command
//
bool succ = false;
int returnCode = 0;
switch (args.cmd) {
case CmdCreate: {
auto spec = parseSpec(args.createSpec);
spec.validate();
createCacheDirectoryIfNeeded();
succ = createCrate(args, spec.preprocess());
break;
} case CmdRun: {
succ = runCrate(args, argc - numArgsProcessed, argv + numArgsProcessed, returnCode);
break;
} case CmdNone: {
break; // impossible
}}
return succ ? (returnCode <= 255 ? returnCode : 255) : 1; // not sure why sometimes returnCode=255
}
int main(int argc, char** argv) {
try {
return mainGuarded(argc, argv);
} catch (const Exception &e) {
std::cerr << rang::fg::red << e.what() << rang::style::reset << std::endl;
return 1;
} catch (const std::exception& e) {
std::cerr << "FIXME(EXCEPTION std::exception): " << rang::fg::red << e.what() << rang::style::reset << std::endl;
return 1;
} catch (...) {
std::cerr << rang::fg::red << "XXX UNKNOWN EXCEPTION IS CAUGHT" << rang::style::reset << std::endl;
return 1;
}
}