/
ydb_sys.c
93 lines (81 loc) · 2.21 KB
/
ydb_sys.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#define _POSIX_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "ydb_db.h"
#include "ydb_logging.h"
#include "ydb_sys.h"
int sys_pid_exist(int pid)
{
/* Signal 0 has a special meaning. */
int r = kill(pid, 0);
if (r == -1) {
return 0;
} else {
return 1;
}
}
/* http://www.faqs.org/faqs/unix-faq/faq/part3/section-13.html
*
* One more thing -- if you don't want to go through all of this
* trouble, there is a portable way to avoid this problem, although it
* is somewhat less efficient. Your parent process should fork, and
* then wait right there and then for the child process to terminate.
* The child process then forks again, giving you a child and a
* grandchild. The child exits immediately (and hence the parent
* waiting for it notices its death and continues to work), and the
* grandchild does whatever the child was originally supposed to.
* Since its parent died, it is inherited by init, which will do
* whatever waiting is needed. This method is inefficient because it
* requires an extra fork, but is pretty much completely portable.
*/
int sys_fork(fork_callback callback, void *userdata)
{
callback(userdata);
return 0;
int pid = fork();
if (pid == 0) {
/* First child. */
int pid2 = fork();
if (pid == 0) {
/* Grandchild. */
callback(userdata);
_exit(0);
}
_exit(pid2);
}
if (pid < 0) {
return -1;
}
int status;
if (waitpid(pid, &status, 0) == -1) {
return -1;
}
return status; /* Status has a pid of grandchild. */
}
/* Based on redis. */
static int _linux_get_overcommit()
{
FILE *fp = fopen("/proc/sys/vm/overcommit_memory","r");
char buf[64];
if (fp == NULL) return -1;
char *r = fgets(buf, sizeof(buf), fp);
fclose(fp);
if (r) {
return atoi(buf);
}
return -1;
}
void linux_check_overcommit(struct db *db)
{
if (_linux_get_overcommit() == 0) {
log_warn(db, "WARNING overcommit_memory is disabled! Loading "
"large files may fail under low memory condition. To "
"fix this issue add 'vm.overcommit_memory = 1' to "
"/etc/sysctl.conf and then reboot or run the command "
"'sysctl vm.overcommit_memory=1'. %s", "");
}
}