forked from netstao/connect-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpFunction.c
executable file
·418 lines (371 loc) · 10.6 KB
/
cpFunction.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
+----------------------------------------------------------------------+
| common con pool |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| original: Tianfeng Han modify:Xinhua Guo <woshiguo35@sina.com> |
+----------------------------------------------------------------------+
*/
#include "php_connect_pool.h"
static char bufr[SW_LOG_BUFFER_SIZE];
static char bufpid[SW_PID_BUFFER_SIZE];
FILE *pid_fn = NULL;
int cp_error;
FILE *cp_log_fn = NULL;
int cpLog_init(char *logfile) {
cp_log_fn = fopen(logfile, "a+");
if (cp_log_fn == NULL)
{
return FAILURE;
}
if (setvbuf(cp_log_fn, bufr, _IOLBF, SW_LOG_BUFFER_SIZE) < 0)
{
return FAILURE;
}
return SUCCESS;
}
int pid_init() {
char pid_name[512] = {0};
sprintf(pid_name, "%s%s.pid", PID_FILE_PATH, CPGC.title);
pid_fn = fopen(pid_name, "w+");
if (pid_fn == NULL)
{
cpLog("create pid file error");
return FAILURE;
}
if (setvbuf(pid_fn, bufpid, _IONBF, SW_PID_BUFFER_SIZE) < 0)
{
return FAILURE;
}
return SUCCESS;
}
int set_pid(int pid) {
fprintf(pid_fn, "%d\n", pid);
return SUCCESS;
}
void swLog_free(void) {
fclose(cp_log_fn);
}
CPINLINE int cpWrite(int fd, void *buf, int count) {
int nwritten = 0, totlen = 0;
while (totlen != count)
{
nwritten = write(fd, buf, count - totlen);
if (nwritten > 0)
{
totlen += nwritten;
buf += nwritten;
} else if (nwritten == 0)
{
return totlen;
} else
{
if (errno == EINTR)
{
continue;
} else if (errno == EAGAIN)
{
usleep(1);
continue;
} else
{
return -1;
}
}
}
return totlen;
}
CPINLINE int cpFifoRead(int pipe_fd_read, void *buf, int len) {
int n, total = 0;
do
{
n = read(pipe_fd_read, buf + total, len);
if (n > 0)
{
total += n;
if (total == len)
{
break;
}
}
// else {
// cpLog("worker fifo recive error %d,len %d\n", errno, n);
// }
} while ((n < 0 && errno == EINTR) || n > 0);
return total;
}
CPINLINE int cpNetRead(int fd, void *buf, int len) {
int n, total = 0;
do
{
n = recv(fd, buf + total, len, MSG_WAITALL);
if (n > 0)
{
total += n;
if (total == len)
{
break;
}
} else if (n == 0)
{
return 0;
}
// else {
// cpLog("worker recive error %d,len %d\n", errno, n);
// }
} while ((n < 0 && errno == EINTR) || n > 0);
return total;
}
void cpSettitle(char *title_name) {
// assert(MAX_TITLE_LENGTH > strlen(title) + 5);
char title[MAX_TITLE_LENGTH + 5] = {0};
strcat(title, "pool_");
strcat(title, title_name);
#if PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 4
zval *name_ptr, name;
name_ptr = &name;
ZVAL_STRING(name_ptr, title, 1);
zval_add_ref(&name_ptr);
zval *retval;
zval **args[1];
args[0] = &name_ptr;
zval *function;
MAKE_STD_ZVAL(function);
ZVAL_STRING(function, "cli_set_process_title", 1);
if (call_user_function_ex(EG(function_table), NULL, function, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
return;
}
zval_ptr_dtor(&function);
if (retval)
{
zval_ptr_dtor(&retval);
}
#else
bzero(sapi_module.executable_location, MAX_TITLE_LENGTH);
memcpy(sapi_module.executable_location, title, strlen(title));
#endif
}
//将套接字设置为非阻塞方式
CPINLINE void swSetNonBlock(int sock) {
int opts, ret;
do
{
opts = fcntl(sock, F_GETFL);
} while (opts < 0 && errno == EINTR);
if (opts < 0)
{
cpLog("fcntl(sock,GETFL) fail");
}
opts = opts | O_NONBLOCK;
do
{
ret = fcntl(sock, F_SETFL, opts);
} while (ret < 0 && errno == EINTR);
if (ret < 0)
{
cpLog("fcntl(sock,SETFL,opts) fail");
}
}
CPINLINE void swSetBlock(int sock) {
int opts, ret;
do
{
opts = fcntl(sock, F_GETFL);
} while (opts < 0 && errno == EINTR);
if (opts < 0)
{
cpLog("fcntl(sock,GETFL) fail");
}
opts = opts & ~O_NONBLOCK;
do
{
ret = fcntl(sock, F_SETFL, opts);
} while (ret < 0 && errno == EINTR);
if (ret < 0)
{
cpLog("fcntl(sock,SETFL,opts) fail");
}
}
CPINLINE int cpSetTimeout(int sock, double timeout) {
int ret;
struct timeval timeo;
timeo.tv_sec = (int) timeout;
timeo.tv_usec = (int) ((timeout - timeo.tv_sec) * 1000 * 1000);
ret = setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (void *) &timeo, sizeof (timeo));
if (ret < 0)
{
return FAILURE;
}
ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *) &timeo, sizeof (timeo));
if (ret < 0)
{
return FAILURE;
}
return SUCCESS;
}
CPINLINE int cpCreateFifo(char *file) {
int pipe_fd;
int res;
umask(0);
if (access(file, F_OK) == -1)
{
res = mkfifo(file, 0666);
if (res != 0 && errno != EEXIST)
{//&&避免 worker和client一起创建导致的exist错误
cpLog("Could not create fifo %s Error: %s[%d]", file, strerror(errno), errno);
return -1;
}
}
pipe_fd = open(file, CP_PIPE_MOD);
if (pipe_fd == -1)
{
cpLog("Could not open fifo %s Error: %s[%d]", file, strerror(errno), errno);
return -1;
}
return pipe_fd;
}
/**
* clear all singal
*/
void swSingalNone() {
sigset_t mask;
sigfillset(&mask);
int ret = pthread_sigmask(SIG_BLOCK, &mask, NULL);
if (ret < 0)
{
cpLog("pthread_sigmask fail: %s", strerror(ret));
}
}
zval * cpGetConfig(char *filename) {
zval fun_name, **args[2], *retval, *file, *section;
ZVAL_STRING(&fun_name, "parse_ini_file", 0);
MAKE_STD_ZVAL(file);
ZVAL_STRING(file, filename, 1);
MAKE_STD_ZVAL(section);
ZVAL_BOOL(section, 1);
args[0] = &file;
args[1] = §ion;
if (call_user_function_ex(CG(function_table), NULL, &fun_name, &retval, 2, args, 0, NULL TSRMLS_CC) != SUCCESS)
{
zval_ptr_dtor(&file);
zval_ptr_dtor(§ion);
return NULL;
}
zval_ptr_dtor(&file);
zval_ptr_dtor(§ion);
return retval;
}
CPINLINE zval * cpMD5(zval *arr) {//pass in array , out md5 zval
smart_str ser_data = {0};
cp_serialize(&ser_data, arr);
zval fun_name, **args[1], *retval, *str;
ZVAL_STRING(&fun_name, "md5", 0);
MAKE_STD_ZVAL(str);
ZVAL_STRINGL(str, ser_data.c, ser_data.len, 1);
args[0] = &str;
if (call_user_function_ex(CG(function_table), NULL, &fun_name, &retval, 1, args, 0, NULL TSRMLS_CC) != SUCCESS)
{
zval_ptr_dtor(&str);
smart_str_free(&ser_data);
return NULL;
}
zval_ptr_dtor(&str);
smart_str_free(&ser_data);
return retval;
}
CPINLINE void cp_serialize(smart_str *ser_data, zval *array) {
// struct timeval start, end;
// gettimeofday(&start, NULL);
php_serialize_data_t var_hash;
PHP_VAR_SERIALIZE_INIT(var_hash);
php_var_serialize(ser_data, &array, &var_hash TSRMLS_CC);
PHP_VAR_SERIALIZE_DESTROY(var_hash);
// gettimeofday(&end, NULL);
// int timeuse = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
// printf("ser time: %d us\n", timeuse);
}
CPINLINE zval * cp_unserialize(char *data, int len) {
zval *unser_value;
ALLOC_INIT_ZVAL(unser_value);
php_unserialize_data_t var_hash;
PHP_VAR_UNSERIALIZE_INIT(var_hash);
if (php_var_unserialize(&unser_value, (const unsigned char **) &data, (unsigned char *) data + len - 1, &var_hash TSRMLS_CC) != 1)
{
// php_error_docref(NULL TSRMLS_CC, E_NOTICE, "unser data is corrupted");
}
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
return unser_value;
}
CPINLINE void cp_ser_and_setpro(zval *arr) {
smart_str ser_data = {0};
cp_serialize(&ser_data, arr);
memcpy(CPGL.ping_mem_addr + CP_PING_MD5_LEN + CP_PING_PID_LEN + CP_PING_DIS_LEN, ser_data.c, ser_data.len);
smart_str_free(&ser_data);
}
CPINLINE void cp_ser_and_setdis(zval *arr) {
smart_str ser_data = {0};
cp_serialize(&ser_data, arr);
memcpy(CPGL.ping_mem_addr + CP_PING_MD5_LEN + CP_PING_PID_LEN, ser_data.c, ser_data.len);
smart_str_free(&ser_data);
}
cpSignalFunc cpSignalSet(int sig, cpSignalFunc func, int restart, int mask) {
struct sigaction act, oact;
act.sa_handler = func;
if (mask)
{
sigfillset(&act.sa_mask);
} else
{
sigemptyset(&act.sa_mask);
}
act.sa_flags = 0;
// act.sa_flags = SA_SIGINFO;
if (sigaction(sig, &act, &oact) < 0)
{
return NULL;
}
return oact.sa_handler;
}
int cpQueueSignalSet(int sig, cpQueueFunc func) {
struct sigaction act, oact;
sigemptyset(&act.sa_mask);
act.sa_sigaction = func;
act.sa_flags = SA_SIGINFO;
if (sigaction(sig, &act, &oact) < 0)
{
cpLog("sigaction error %d", errno);
return FAILURE;
}
sigset_t block;
sigemptyset(&block);
sigaddset(&block, CP_SIG_EVENT);
sigprocmask(SIG_BLOCK, &block, NULL);
return SUCCESS;
}
#ifndef HAVE_CLOCK_GETTIME
#ifdef __MACH__
int clock_gettime(clock_id_t which_clock, struct timespec *t) {
// be more careful in a multithreaded environement
if (!orwl_timestart)
{
mach_timebase_info_data_t tb = {0};
mach_timebase_info(&tb);
orwl_timebase = tb.numer;
orwl_timebase /= tb.denom;
orwl_timestart = mach_absolute_time();
}
double diff = (mach_absolute_time() - orwl_timestart) * orwl_timebase;
t->tv_sec = diff * ORWL_NANO;
t->tv_nsec = diff - (t->tv_sec * ORWL_GIGA);
return 0;
}
#endif
#endif