forked from gozfree/gear-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibmacro.c
227 lines (211 loc) · 6.17 KB
/
libmacro.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
/******************************************************************************
* Copyright (C) 2014-2018 Zhifeng Gong <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libraries; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#define _GNU_SOURCE
#include "libmacro.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
void *memdup(void *src, size_t len)
{
void *dst = calloc(1, len);
if (dst) {
memcpy(dst, src, len);
}
return dst;
}
struct iovec *iovec_create(size_t len)
{
struct iovec *vec = CALLOC(1, struct iovec);
vec->iov_len = len;
vec->iov_base = calloc(1, len);
return vec;
}
void iovec_destroy(struct iovec *vec)
{
if (!vec || !vec->iov_base)
return;
free(vec->iov_base);
free(vec);
}
void *dl_override(const char *name)
{
void *sym = dlsym(RTLD_NEXT, name);
if (!sym) {
printf("dlsym RTLD_NEXT symbol %s: %s\n", name, dlerror());
return NULL;
}
return sym;
}
int system_noblock(char **argv)
{
pid_t pid;
struct sigaction ignore, saveintr, savequit;
sigset_t chldmask, savemask;
if (argv == NULL)
return -1;
ignore.sa_handler = SIG_IGN;
sigemptyset(&ignore.sa_mask);
ignore.sa_flags = 0;
if (sigaction(SIGINT, &ignore, &saveintr) < 0) return -1;
if (sigaction(SIGQUIT, &ignore, &savequit) < 0) return -1;
if (sigaction(SIGCHLD, &ignore, &savequit) < 0) return -1;
sigemptyset(&chldmask);
sigaddset(&chldmask, SIGCHLD);
if (sigprocmask(SIG_BLOCK, &chldmask, &savemask) < 0) return -1;
if ((pid = fork()) < 0) {
perror("fork");
return -1;
} else if (pid == 0) {
sigaction(SIGINT, &saveintr, NULL);
sigaction(SIGQUIT, &savequit, NULL);
sigaction(SIGCHLD, &savequit, NULL);
sigprocmask(SIG_SETMASK, &savemask, NULL);
if (-1 == execvp(argv[0], argv)) {
perror("exec");
}
_exit(127);
} else {
}
if (sigaction(SIGINT, &saveintr, NULL) < 0) return -1;
if (sigaction(SIGQUIT, &savequit, NULL) < 0) return -1;
if (sigaction(SIGCHLD, &ignore, &savequit) < 0) return -1;
if (sigprocmask(SIG_SETMASK, &savemask, NULL) < 0) return -1;
return pid;
}
ssize_t system_noblock_with_result(char **argv, void *buf, size_t count)
{
int len = -1;
int old_fd, new_fd;
int fd[2];
if (pipe(fd)) {
printf("pipe failed\n");
return -1;
}
int rfd = fd[0];
int wfd = fd[1];
if (EOF == fflush(stdout)) {
printf("fflush failed: %s\n", strerror(errno));
return -1;
}
if (-1 == (old_fd = dup(STDOUT_FILENO))) {
printf("dup STDOUT_FILENO failed: %s\n", strerror(errno));
return -1;
}
if (-1 == (new_fd = dup2(wfd, STDOUT_FILENO))) {
//no need to check failed??
//printf("dup2 STDOUT_FILENO failed: %s\n", strerror(errno));
//return -1;
}
if (-1 == system_noblock(argv)) {
printf("system call failed!\n");
return -1;
}
if (-1 == read(rfd, buf, count-1)) {
printf("read buffer failed!\n");
return -1;
}
len = strlen(buf);
*((char *)buf + len - 1) = '\0';
if (-1 == dup2(old_fd, new_fd)) {
printf("dup2 failed: %s\n", strerror(errno));
return -1;
}
return len;
}
ssize_t system_with_result(const char *cmd, void *buf, size_t count)
{
int len = -1;
int old_fd, new_fd;
int fd[2];
if (pipe(fd)) {
printf("pipe failed\n");
return -1;
}
int rfd = fd[0];
int wfd = fd[1];
if (EOF == fflush(stdout)) {
printf("fflush failed: %s\n", strerror(errno));
return -1;
}
if (-1 == (old_fd = dup(STDOUT_FILENO))) {
printf("dup STDOUT_FILENO failed: %s\n", strerror(errno));
return -1;
}
if (-1 == (new_fd = dup2(wfd, STDOUT_FILENO))) {
//no need to check failed??
//printf("dup2 STDOUT_FILENO failed: %s\n", strerror(errno));
//return -1;
}
if (-1 == system(cmd)) {
printf("system call failed!\n");
return -1;
}
if (-1 == read(rfd, buf, count-1)) {
printf("read buffer failed!\n");
return -1;
}
len = strlen(buf);
*((char *)buf + len - 1) = '\0';
if (-1 == dup2(old_fd, new_fd)) {
printf("dup2 failed: %s\n", strerror(errno));
return -1;
}
return len;
}
int is_little_endian(void)
{
unsigned int probe = 0xff;
size_t sz = sizeof(unsigned int);
unsigned char * probe_byte = (unsigned char *)&probe;
if (!(probe_byte[0] == 0xff || probe_byte[sz - 1] == 0xff)) {
printf("%s: something wrong!\n", __func__);
}
return probe_byte[0] == 0xff;
}
bool proc_exist(const char *proc)
{
#define MAX_CMD_BUFSZ 128
FILE* fp;
int count;
char buf[MAX_CMD_BUFSZ];
char cmd[MAX_CMD_BUFSZ];
bool exist = false;
snprintf(cmd, sizeof(cmd), "ps -ef | grep -w %s | grep -v grep | wc -l", proc);
do {
if ((fp = popen(cmd, "r")) == NULL) {
printf("popen failed\n");
break;
}
if ((fgets(buf, MAX_CMD_BUFSZ, fp)) != NULL) {
count = atoi(buf);
if (count <= 0) {
exist = false;
} else if (count == 1) {
exist = true;
} else {
printf("process number may be wrong");
}
}
pclose(fp);
} while (0);
return exist;
}