forked from boytm/mproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
139 lines (117 loc) · 2.91 KB
/
Copy pathutils.c
File metadata and controls
139 lines (117 loc) · 2.91 KB
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
#include "utils.h"
#include <errno.h>
#include <string.h>
#include <stdio.h>
#ifndef _WIN32
# include <fcntl.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <pwd.h>
# include <grp.h>
# include <unistd.h>
#endif
#ifndef _WIN32
int write_pid_file(const char *pid_file)
{
int pidfd = -1;
char pidstr[100] = {0};
int ret = 1;
ssize_t len;
unlink(pid_file);
pidfd = open(pid_file, O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (pidfd < 0) {
LOGE("Open pid file failed: %s", strerror(errno));
goto out;
}
snprintf(pidstr, sizeof(pidstr), "%d\n", getpid());
len = write(pidfd, pidstr, strlen(pidstr));
if (len == -1) {
LOGE("Write pid file failed: %s", strerror(errno));
goto out;
}
ret = 0; // success
out:
if (pidfd >= 0) close(pidfd);
return ret;
}
/*
* getpwnam() and getgrnam() is MT-Unsafe
* return 0 on success
*/
int change_user(const char *userspec)
{
char buf[4096] = { '\0' };
struct passwd *pw = NULL;
struct group *gr = NULL;
gid_t gid;
uid_t uid;
int saved;
int ret = 1;
saved = errno;
strncpy(buf, userspec, sizeof(buf) / sizeof(buf[0]) - 1);
const char *user_name = buf;
const char *group_name = NULL;
char *pos = strchr(buf, ':');
if (pos) {
*pos = '\0';
group_name = pos + 1;
}
errno = 0;
if (NULL == (pw = getpwnam(user_name))) {
LOGE("Cannot find user '%s': %s", user_name, (errno ? strerror(errno) : ""));
goto out;
} else {
uid = pw->pw_uid;
gid = pw->pw_gid;
}
if (group_name && group_name[0]) {
errno = 0;
if (NULL == (gr = getgrnam(group_name))) {
LOGE("Cannot find group '%s': %s", group_name, (errno ? strerror(errno) : ""));
goto out;
} else {
gid = gr->gr_gid;
}
}
if (0 != setgid(gid)) {
LOGE("Set group ID failed %u: %s", (unsigned)gid, strerror(errno));
goto out;
}
if (0 != setuid(uid)) {
LOGE("Set user ID failed %u: %s", (unsigned)uid, strerror(errno));
goto out;
}
ret = 0; // success
out:
errno = saved;
return ret;
}
#endif // !_WIN32
void hexdump(FILE *out, const void *p, int len)
{
const unsigned char *line;
int i;
int thisline;
int offset;
line = (const unsigned char *)p;
offset = 0;
while (offset < len) {
fprintf(out, "%04x ", offset);
thisline = len - offset;
if (thisline > 16) {
thisline = 16;
}
for (i = 0; i < thisline; i++) {
fprintf(out, "%02x ", line[i]);
}
for (; i < 16; i++) {
fprintf(out, " ");
}
for (i = 0; i < thisline; i++) {
fprintf(out, "%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
}
fprintf(out, "\n");
offset += thisline;
line += thisline;
}
}