-
Notifications
You must be signed in to change notification settings - Fork 2
/
riser.cpp
executable file
·133 lines (122 loc) · 3.35 KB
/
riser.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
/*
* riser.cpp
* Created by oceanwavewyt on 13-1-20.
* Copyright (c) 2012-2013, oceanwavewyt <oceanwavewyt at gmail dot com>
* All rights reserved.
*
* This soft wrap a serice for leveldb store, and support multi-threading
* client access it by the memcache extension of php, and telnet. Currently,
* only support put, get and del operation. additionally, it is support bloom
* filter algorithm for key.
*
*/
#include "gent_util.h"
#include "gent_frame.h"
#include "gent_config.h"
#include <sys/resource.h>
#define PATHBUF 100
void daemonize(void) {
int fd;
if (fork() != 0) exit(0); /* parent exits */
setsid(); /* create a new session */
/* Every output goes to /dev/null. If Redis is daemonized but
* * the 'logfile' is set to 'stdout' in the configuration file
* * it will not log at all. */
if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO) close(fd);
}
}
void version() {
printf("1.0\n");
exit(0);
}
void usage() {
fprintf(stderr,"Usage: ./riser-server [options]\n");
fprintf(stderr," ./riser-server -v or --version\n");
fprintf(stderr," ./riser-server -h or --help\n");
fprintf(stderr,"Examples:\n");
fprintf(stderr," ./riser-server (run the server with default conf)\n");
fprintf(stderr," ./riser-server -c conf/riser.conf\n");
fprintf(stderr," ./riser-server -d -c conf/riser.conf\n");
fprintf(stderr," ./riser-server -p 3555\n");
exit(1);
}
bool getpath(char *filepath) {
char *c = filepath;
if(*c == '/') return true;
char buf[PATHBUF] ={0};
if(!getcwd(buf,PATHBUF)) return false;
if(*c=='.') {
c++;
if(*c == '/') c++;
else
c--;
}
char tt[PATHBUF]={0};
sprintf(tt, "%s/%s", buf,c);
memmove(filepath,tt,PATHBUF);
return true;
}
int main(int argc, char **argv)
{
int ch;
bool deamon = false;
//bool is_slave = false;
int port = -1;
char configfile[PATHBUF] = "riser.conf";
struct rlimit rlim;
while((ch = getopt(argc,argv,"c:vhdsp:"))!= -1) {
switch (ch) {
case 'c':
printf("option a:'%s'\n",optarg);
memcpy(configfile, optarg, strlen(optarg));
break;
case 'd':
deamon = true;
break;
case 's':
//is_slave = true;
break;
case 'p':
port = atoi(optarg);
break;
case 'v':
version();
return 1;
case 'h':
usage();
return 1;
default:
break;
}
}
signal(SIGPIPE, SIG_IGN);
if(getrlimit(RLIMIT_NOFILE, &rlim) != 0) {
std::cout << "get rlimit failed\n";
return 1;
}
/*
rlim.rlim_cur = 65535;
rlim.rlim_max = 65535;
if(setrlimit(RLIMIT_NOFILE, &rlim) != 0) {
std::cout << "Set rlimit failed, please try starting as root\n";
return 1;
}
*/
getpath(configfile);
struct riserserver server;
server.configfile = configfile;
/* initialize config file */
if(!GentFrame::Instance()->Init(&server, configfile)){
std::cout << "init fail!\n";
return 1;
}
if(deamon == true) {
daemonize();
}
GentFrame::Instance()->Run(port);
return 0;
}