-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsystem_util.cpp
146 lines (117 loc) · 3 KB
/
system_util.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* system_util.cpp
*
* Created on: 2011-10-14
* Author: Administrator
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/file.h>
#include <stdarg.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <signal.h>
#include "system_util.h"
namespace xlnet
{
int set_thread_title(const char* fmt, ...)
{
char title [16] ={0};
va_list ap;
va_start(ap, fmt);
vsnprintf (title, sizeof (title) , fmt, ap);
va_end (ap);
return prctl(PR_SET_NAME,title) ;
}
void set_process_title(int argc,char* argv[],const char* fmt, ...)
{
char *arg_start = argv[0];
char *arg_end = argv[argc-1] + strlen(argv[argc-1])+1;
char title [256] ={0};
va_list ap;
va_start(ap, fmt);
vsnprintf (title, sizeof (title), fmt, ap);
va_end (ap);
int i=0;
int tlen = strlen (title) + 1;
if (arg_end - arg_start < tlen && environ[0] == arg_end)
{
char *env_next = environ[0];
for(i=0; environ[i]!=NULL; i++)
{
if(env_next == environ[i])
{
env_next = environ[i] + strlen (environ[i]) + 1;
environ[i] = strdup(environ[i]);
}
else
{
break;
}
}
arg_end = env_next;
}
i = arg_end - arg_start;
if (tlen <= i)
{
strcpy(arg_start, title);
memset(arg_start + tlen, 0, i - tlen);
}
else
{
stpncpy(arg_start, title, i - 1)[0] = '\0';
}
}
int lock_file(const char* filename)
{
int fd = open(filename,O_RDONLY|O_CREAT,0755) ;
if ( fd < 0 ) return -1 ;
if( flock(fd,LOCK_EX|LOCK_NB)!=0 ) return -1;
return fd ;
}
int set_open_file_limit(int maxsize)
{
if (maxsize < 0) return -1;
struct rlimit limit = {(size_t)maxsize, (size_t)maxsize} ;
return setrlimit(RLIMIT_NOFILE,&limit) ;
}
int get_open_file_limit()
{
struct rlimit limit = {0} ;
if( getrlimit(RLIMIT_NOFILE,&limit)!=0) return -1 ;
return limit.rlim_max ;
}
int daemon_init(int nochdir,int noclose)
{
// clear file creation mask
umask(0);
pid_t pid = 0 ;
if( (pid = fork() ) < 0 ) return -1 ;
else if(pid != 0 ) _exit(0) ;
setsid() ;
// Ensure future opens won't allocate controlling TTYs
struct sigaction sa ;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
if (sigaction(SIGHUP, &sa, NULL) < 0) return -1 ;
if( (pid = fork() ) < 0 ) return -1 ;
else if(pid != 0 ) _exit(0) ;
if((!nochdir) && (chdir("/")!=0) ) return -1 ;
if(!noclose)
{
int fd = open("/dev/null", O_RDWR);
if(fd < 0 ) return -1 ;
dup2(fd,0) ;
dup2(fd,1) ;
dup2(fd,2) ;
close(fd);
}
return 0 ;
}
}