aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/daemon.cpp
diff options
context:
space:
mode:
authorqwerty <qwerty@yandex-team.ru>2022-02-10 16:49:25 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:49:25 +0300
commitca6b8a0150d5ee4cc8fb804d1285d9c32fb4fe88 (patch)
tree5d5cb817648f650d76cf1076100726fd9b8448e8 /util/system/daemon.cpp
parent34e37ee57f5837a8da2fdfaccaac9bbe6c7f02ba (diff)
downloadydb-ca6b8a0150d5ee4cc8fb804d1285d9c32fb4fe88.tar.gz
Restoring authorship annotation for <qwerty@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util/system/daemon.cpp')
-rw-r--r--util/system/daemon.cpp126
1 files changed, 63 insertions, 63 deletions
diff --git a/util/system/daemon.cpp b/util/system/daemon.cpp
index 8f58813c52..130e6c8f45 100644
--- a/util/system/daemon.cpp
+++ b/util/system/daemon.cpp
@@ -18,7 +18,7 @@
using namespace NDaemonMaker;
static bool Fork(EParent parent) {
- pid_t pid = fork();
+ pid_t pid = fork();
if (pid > 0) {
int status = 0;
@@ -30,11 +30,11 @@ static bool Fork(EParent parent) {
return true;
}
} else if (pid < 0) {
- ythrow TSystemError() << "Cannot fork";
+ ythrow TSystemError() << "Cannot fork";
}
if (setsid() < 0) {
- ythrow TSystemError() << "Cannot setsid";
+ ythrow TSystemError() << "Cannot setsid";
}
pid = fork();
@@ -42,24 +42,24 @@ static bool Fork(EParent parent) {
if (pid > 0) {
_exit(0);
} else if (pid < 0) {
- ythrow TSystemError() << "Cannot second fork";
+ ythrow TSystemError() << "Cannot second fork";
}
return false;
}
-#endif
-
+#endif
+
static void CloseFromToExcept(int from, int to, const int* except) {
(void)from;
(void)to;
(void)except;
-#ifdef _unix_
+#ifdef _unix_
int mfd = NSystemInfo::MaxOpenFiles();
- for (int s = from; s < mfd && (to == -1 || s < to); s++) {
+ for (int s = from; s < mfd && (to == -1 || s < to); s++) {
for (const int* ex = except; *ex >= 0; ++ex) {
if (s == *ex) {
- goto dontclose;
+ goto dontclose;
}
}
while (close(s) == -1) {
@@ -71,98 +71,98 @@ static void CloseFromToExcept(int from, int to, const int* except) {
}
}
dontclose:;
- }
+ }
#endif /* _unix_ */
}
-
+
bool NDaemonMaker::MakeMeDaemon(ECloseDescriptors cd, EStdIoDescriptors iod, EChDir chd, EParent parent) {
(void)cd;
(void)iod;
(void)chd;
-#ifdef _unix_
+#ifdef _unix_
if (Fork(parent)) {
return true;
}
-
+
if (chd == chdirRoot) {
if (chdir("/")) {
ythrow TSystemError() << "chdir(\"/\") failed";
}
}
-
+
int fd[4] = {-1, -1, -1, -1};
switch (iod) {
- case openYandexStd:
- fd[0] = open("yandex.stdin", O_RDONLY);
+ case openYandexStd:
+ fd[0] = open("yandex.stdin", O_RDONLY);
if (fd[0] < 0) {
- ythrow TSystemError() << "Cannot open 'yandex.stdin'";
+ ythrow TSystemError() << "Cannot open 'yandex.stdin'";
}
- fd[1] = open("yandex.stdout", O_WRONLY | O_APPEND | O_CREAT, 660);
+ fd[1] = open("yandex.stdout", O_WRONLY | O_APPEND | O_CREAT, 660);
if (fd[1] < 0) {
- ythrow TSystemError() << "Cannot open 'yandex.stdout'";
+ ythrow TSystemError() << "Cannot open 'yandex.stdout'";
}
- fd[2] = open("yandex.stderr", O_WRONLY | O_APPEND | O_CREAT, 660);
+ fd[2] = open("yandex.stderr", O_WRONLY | O_APPEND | O_CREAT, 660);
if (fd[2] < 0) {
- ythrow TSystemError() << "Cannot open 'yandex.stderr'";
+ ythrow TSystemError() << "Cannot open 'yandex.stderr'";
}
- break;
- case openDevNull:
- fd[0] = open("/dev/null", O_RDWR, 0);
- break;
- case openNone:
- break;
- default:
- ythrow yexception() << "Unknown open descriptors mode: " << (int)iod;
- }
-
- const int except[4] = {
- fd[0],
- fd[1],
- fd[2],
+ break;
+ case openDevNull:
+ fd[0] = open("/dev/null", O_RDWR, 0);
+ break;
+ case openNone:
+ break;
+ default:
+ ythrow yexception() << "Unknown open descriptors mode: " << (int)iod;
+ }
+
+ const int except[4] = {
+ fd[0],
+ fd[1],
+ fd[2],
-1};
if (closeAll == cd) {
- CloseFromToExcept(0, -1, except);
+ CloseFromToExcept(0, -1, except);
} else if (closeStdIoOnly == cd) {
- CloseFromToExcept(0, 3, except);
+ CloseFromToExcept(0, 3, except);
} else {
- ythrow yexception() << "Unknown close descriptors mode: " << (int)cd;
+ ythrow yexception() << "Unknown close descriptors mode: " << (int)cd;
}
-
+
switch (iod) {
- case openYandexStd:
- /* Assuming that open(2) acquires fds in order. */
- dup2(fd[0], STDIN_FILENO);
+ case openYandexStd:
+ /* Assuming that open(2) acquires fds in order. */
+ dup2(fd[0], STDIN_FILENO);
if (fd[0] > 2) {
- close(fd[0]);
+ close(fd[0]);
}
- dup2(fd[1], STDOUT_FILENO);
+ dup2(fd[1], STDOUT_FILENO);
if (fd[1] > 2) {
- close(fd[1]);
+ close(fd[1]);
}
- dup2(fd[2], STDERR_FILENO);
+ dup2(fd[2], STDERR_FILENO);
if (fd[2] > 2) {
- close(fd[2]);
+ close(fd[2]);
}
- break;
- case openDevNull:
- dup2(fd[0], STDIN_FILENO);
- dup2(fd[0], STDOUT_FILENO);
- dup2(fd[0], STDERR_FILENO);
+ break;
+ case openDevNull:
+ dup2(fd[0], STDIN_FILENO);
+ dup2(fd[0], STDOUT_FILENO);
+ dup2(fd[0], STDERR_FILENO);
if (fd[0] > 2) {
- close(fd[0]);
+ close(fd[0]);
}
- break;
- default:
- break;
- }
+ break;
+ default:
+ break;
+ }
return false;
#else
return true;
-#endif
-}
-
-void NDaemonMaker::CloseFrom(int fd) {
+#endif
+}
+
+void NDaemonMaker::CloseFrom(int fd) {
static const int except[1] = {-1};
- CloseFromToExcept(fd, -1, except);
-}
+ CloseFromToExcept(fd, -1, except);
+}