-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquid-init.c
More file actions
171 lines (146 loc) · 4.96 KB
/
Copy pathsquid-init.c
File metadata and controls
171 lines (146 loc) · 4.96 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
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
// squid-init.c
#define _GNU_SOURCE
#include <errno.h>
#include <getopt.h>
#include <grp.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
static int mkdir_p(const char *path, mode_t mode) {
struct stat st;
if (!path || !*path) return -1;
// Optimization: Check if path exists and is a directory first
if (stat(path, &st) == 0) {
if (S_ISDIR(st.st_mode)) return 0;
errno = ENOTDIR;
return -1;
}
// Enforce PATH_MAX limit
size_t len = strnlen(path, PATH_MAX);
if (len >= PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
// Use heap allocation for path manipulation to avoid stack limitations
char *copy = strdup(path);
if (!copy) return -1; // errno set by strdup (ENOMEM)
// Strip trailing slashes
char *p = copy + len - 1;
while (p > copy && *p == '/') {
*p = '\0';
p--;
}
// Iterate over path components
for (p = copy; *p; p++) {
if (*p == '/') {
if (p == copy) continue; // Skip root slash
*p = '\0';
if (mkdir(copy, mode) != 0 && errno != EEXIST) {
int saved_errno = errno;
free(copy);
errno = saved_errno;
return -1;
}
*p = '/';
}
}
// Create the final component
if (mkdir(copy, mode) != 0 && errno != EEXIST) {
int saved_errno = errno;
free(copy);
errno = saved_errno;
return -1;
}
free(copy);
return 0;
}
static int run_squid_z(const char *squid, const char *conf) {
pid_t pid = fork();
if (pid < 0) return -1;
if (pid == 0) {
execl(squid, squid, "-N", "-z", "-f", conf, (char *)NULL);
fprintf(stderr, "execl failed: %s\n", strerror(errno));
_exit(127);
}
int status = 0;
while (waitpid(pid, &status, 0) < 0) {
if (errno != EINTR) return -1;
}
if (WIFEXITED(status)) return WEXITSTATUS(status);
return -1;
}
static void print_help(const char *prog) {
fprintf(stderr, "Usage: %s [options]\n", prog);
fprintf(stderr, "Options:\n");
fprintf(stderr, " -b, --bin <path> Path to squid binary\n");
fprintf(stderr, " -f, --conf <path> Path to squid config\n");
fprintf(stderr, " -c, --cache <path> Path to cache dir\n");
fprintf(stderr, " -p, --pidfile <path> Path to PID file\n");
fprintf(stderr, " -h, --help Show this help\n");
}
int main(int argc, char *argv[]) {
const char *squid = NULL;
const char *conf = NULL;
const char *cache = NULL;
const char *pidfile = NULL;
static struct option long_options[] = {
{"bin", required_argument, 0, 'b'},
{"conf", required_argument, 0, 'f'},
{"cache", required_argument, 0, 'c'},
{"pidfile", required_argument, 0, 'p'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
int opt;
while ((opt = getopt_long(argc, argv, "b:f:c:h", long_options, NULL)) != -1) {
switch (opt) {
case 'b': squid = optarg; break;
case 'f': conf = optarg; break;
case 'c': cache = optarg; break;
case 'p': pidfile = optarg; break;
case 'h': print_help(argv[0]); return 0;
default: print_help(argv[0]); return 1;
}
}
// Precedence: CLI > Environment > Default
if (!squid || !*squid) squid = getenv("SQUID_BIN");
if (!squid || !*squid) squid = "/usr/sbin/squid";
if (!conf || !*conf) conf = getenv("SQUID_CONF");
if (!conf || !*conf) conf = "/etc/squid/squid.conf";
if (!cache || !*cache) cache = getenv("SQUID_CACHE_DIR");
if (!cache || !*cache) cache = "/var/cache/squid";
if (!pidfile || !*pidfile) pidfile = getenv("SQUID_PIDFILE");
if (!pidfile || !*pidfile) pidfile = "/var/run/squid.pid";
fprintf(stderr, "squid-init: Configuration:\n");
fprintf(stderr, " Binary : %s\n", squid);
fprintf(stderr, " Config : %s\n", conf);
fprintf(stderr, " Cache : %s\n", cache);
fprintf(stderr, " PID File : %s\n", pidfile);
if (pidfile && *pidfile) {
unlink(pidfile);
}
if (access(squid, X_OK) != 0) {
fprintf(stderr, "Error: Cannot execute %s: %s\n", squid, strerror(errno));
return 1;
}
if (mkdir_p(cache, 0755) != 0) {
fprintf(stderr, "Error: mkdir_p(%s) failed: %s\n", cache, strerror(errno));
return 1;
}
fprintf(stderr, "squid-init: Initializing cache...\n");
int zrc = run_squid_z(squid, conf);
if (zrc != 0) {
fprintf(stderr, "Error: 'squid -z' failed with code %d\n", zrc);
return zrc < 0 ? 1 : zrc;
}
fprintf(stderr, "squid-init: Starting squid...\n");
execl(squid, squid, "-N", "-f", conf, (char *)NULL);
fprintf(stderr, "Error: exec squid failed: %s\n", strerror(errno));
return 127;
}