/[baselayout]/trunk/src/librc-misc.c
Gentoo

Diff of /trunk/src/librc-misc.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2576 Revision 2577
36# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH) 36# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
37#endif 37#endif
38 38
39void *rc_xcalloc (size_t n, size_t size) 39void *rc_xcalloc (size_t n, size_t size)
40{ 40{
41 void *value = calloc (n, size); 41 void *value = calloc (n, size);
42 42
43 if (value) 43 if (value)
44 return value; 44 return value;
45 45
46 ERRX 46 ERRX
47} 47}
48 48
49void *rc_xmalloc (size_t size) 49void *rc_xmalloc (size_t size)
50{ 50{
51 void *value = malloc (size); 51 void *value = malloc (size);
52 52
53 if (value) 53 if (value)
54 return (value); 54 return (value);
55 55
56 ERRX 56 ERRX
57} 57}
58 58
59void *rc_xrealloc (void *ptr, size_t size) 59void *rc_xrealloc (void *ptr, size_t size)
60{ 60{
61 void *value = realloc (ptr, size); 61 void *value = realloc (ptr, size);
62 62
63 if (value) 63 if (value)
64 return (value); 64 return (value);
65 65
66 ERRX 66 ERRX
67} 67}
68 68
69 69
70char *rc_xstrdup (const char *str) 70char *rc_xstrdup (const char *str)
71{ 71{
72 char *value; 72 char *value;
73 73
74 if (! str) 74 if (! str)
75 return (NULL); 75 return (NULL);
76 76
77 value = strdup (str); 77 value = strdup (str);
78 78
79 if (value) 79 if (value)
80 return (value); 80 return (value);
81 81
82 ERRX 82 ERRX
83} 83}
84 84
85bool rc_is_env (const char *var, const char *val) 85bool rc_is_env (const char *var, const char *val)
86{ 86{
87 char *v; 87 char *v;
88 88
89 if (! var) 89 if (! var)
90 return (false); 90 return (false);
91 91
92 v = getenv (var); 92 v = getenv (var);
93 if (! v) 93 if (! v)
94 return (val == NULL ? true : false); 94 return (val == NULL ? true : false);
95 95
96 return (strcasecmp (v, val) == 0 ? true : false); 96 return (strcasecmp (v, val) == 0 ? true : false);
97} 97}
98 98
99char *rc_strcatpaths (const char *path1, const char *paths, ...) 99char *rc_strcatpaths (const char *path1, const char *paths, ...)
100{ 100{
101 va_list ap; 101 va_list ap;
102 int length; 102 int length;
103 int i; 103 int i;
104 char *p; 104 char *p;
105 char *path; 105 char *path;
106 char *pathp; 106 char *pathp;
107 107
108 if (! path1 || ! paths) 108 if (! path1 || ! paths)
109 return (NULL); 109 return (NULL);
110 110
111 length = strlen (path1) + strlen (paths) + 3; 111 length = strlen (path1) + strlen (paths) + 3;
112 i = 0; 112 i = 0;
113 va_start (ap, paths); 113 va_start (ap, paths);
114 while ((p = va_arg (ap, char *)) != NULL) 114 while ((p = va_arg (ap, char *)) != NULL)
115 length += strlen (p) + 1; 115 length += strlen (p) + 1;
116 va_end (ap); 116 va_end (ap);
117 117
118 path = rc_xmalloc (length); 118 path = rc_xmalloc (length);
119 memset (path, 0, length); 119 memset (path, 0, length);
120 memcpy (path, path1, strlen (path1)); 120 memcpy (path, path1, strlen (path1));
121 pathp = path + strlen (path1) - 1; 121 pathp = path + strlen (path1) - 1;
122 if (*pathp != '/') 122 if (*pathp != '/') {
123 { 123 pathp++;
124 pathp++;
125 *pathp++ = '/'; 124 *pathp++ = '/';
126 } 125 }
127 else 126 else
128 pathp++; 127 pathp++;
129 memcpy (pathp, paths, strlen (paths)); 128 memcpy (pathp, paths, strlen (paths));
130 pathp += strlen (paths); 129 pathp += strlen (paths);
131 130
132 va_start (ap, paths); 131 va_start (ap, paths);
133 while ((p = va_arg (ap, char *)) != NULL) 132 while ((p = va_arg (ap, char *)) != NULL) {
134 {
135 if (*pathp != '/') 133 if (*pathp != '/')
136 *pathp++ = '/'; 134 *pathp++ = '/';
137 i = strlen (p); 135 i = strlen (p);
138 memcpy (pathp, p, i); 136 memcpy (pathp, p, i);
139 pathp += i; 137 pathp += i;
140 } 138 }
141 va_end (ap); 139 va_end (ap);
142 140
143 *pathp++ = 0; 141 *pathp++ = 0;
144 142
145 return (path); 143 return (path);
146} 144}
147 145
148bool rc_exists (const char *pathname) 146bool rc_exists (const char *pathname)
149{ 147{
150 struct stat buf; 148 struct stat buf;
151 149
152 if (! pathname) 150 if (! pathname)
153 return (false); 151 return (false);
154 152
155 if (stat (pathname, &buf) == 0) 153 if (stat (pathname, &buf) == 0)
156 return (true); 154 return (true);
157 155
158 errno = 0; 156 errno = 0;
159 return (false); 157 return (false);
160} 158}
161 159
162bool rc_is_file (const char *pathname) 160bool rc_is_file (const char *pathname)
163{ 161{
164 struct stat buf; 162 struct stat buf;
165 163
166 if (! pathname) 164 if (! pathname)
167 return (false); 165 return (false);
168 166
169 if (stat (pathname, &buf) == 0) 167 if (stat (pathname, &buf) == 0)
170 return (S_ISREG (buf.st_mode)); 168 return (S_ISREG (buf.st_mode));
171 169
172 errno = 0; 170 errno = 0;
173 return (false); 171 return (false);
174} 172}
175 173
176bool rc_is_dir (const char *pathname) 174bool rc_is_dir (const char *pathname)
177{ 175{
178 struct stat buf; 176 struct stat buf;
179 177
180 if (! pathname) 178 if (! pathname)
181 return (false); 179 return (false);
182 180
183 if (stat (pathname, &buf) == 0) 181 if (stat (pathname, &buf) == 0)
184 return (S_ISDIR (buf.st_mode)); 182 return (S_ISDIR (buf.st_mode));
185 183
186 errno = 0; 184 errno = 0;
187 return (false); 185 return (false);
188} 186}
189 187
190bool rc_is_link (const char *pathname) 188bool rc_is_link (const char *pathname)
191{ 189{
192 struct stat buf; 190 struct stat buf;
193 191
194 if (! pathname) 192 if (! pathname)
195 return (false); 193 return (false);
196 194
197 if (lstat (pathname, &buf) == 0) 195 if (lstat (pathname, &buf) == 0)
198 return (S_ISLNK (buf.st_mode)); 196 return (S_ISLNK (buf.st_mode));
199 197
200 errno = 0; 198 errno = 0;
201 return (false); 199 return (false);
202} 200}
203 201
204bool rc_is_exec (const char *pathname) 202bool rc_is_exec (const char *pathname)
205{ 203{
206 struct stat buf; 204 struct stat buf;
207 205
208 if (! pathname) 206 if (! pathname)
209 return (false); 207 return (false);
210 208
211 if (lstat (pathname, &buf) == 0) 209 if (lstat (pathname, &buf) == 0)
212 return (buf.st_mode & S_IXUGO); 210 return (buf.st_mode & S_IXUGO);
213 211
214 errno = 0; 212 errno = 0;
215 return (false); 213 return (false);
216} 214}
217 215
218char **rc_ls_dir (char **list, const char *dir, int options) 216char **rc_ls_dir (char **list, const char *dir, int options)
219{ 217{
220 DIR *dp; 218 DIR *dp;
221 struct dirent *d; 219 struct dirent *d;
222 220
223 if (! dir) 221 if (! dir)
224 return (list); 222 return (list);
225 223
226 if ((dp = opendir (dir)) == NULL) 224 if ((dp = opendir (dir)) == NULL) {
227 {
228 eerror ("failed to opendir `%s': %s", dir, strerror (errno)); 225 eerror ("failed to opendir `%s': %s", dir, strerror (errno));
229 return (list); 226 return (list);
230 } 227 }
231 228
232 errno = 0; 229 errno = 0;
233 while (((d = readdir (dp)) != NULL) && errno == 0) 230 while (((d = readdir (dp)) != NULL) && errno == 0) {
234 {
235 if (d->d_name[0] != '.') 231 if (d->d_name[0] != '.') {
236 {
237 if (options & RC_LS_INITD) 232 if (options & RC_LS_INITD) {
238 {
239 int l = strlen (d->d_name); 233 int l = strlen (d->d_name);
240 char *init = rc_strcatpaths (RC_INITDIR, d->d_name, 234 char *init = rc_strcatpaths (RC_INITDIR, d->d_name,
241 (char *) NULL); 235 (char *) NULL);
242 bool ok = rc_exists (init); 236 bool ok = rc_exists (init);
243 free (init); 237 free (init);
244 if (! ok) 238 if (! ok)
245 continue; 239 continue;
246 240
247 /* .sh files are not init scripts */ 241 /* .sh files are not init scripts */
248 if (l > 2 && d->d_name[l - 3] == '.' && 242 if (l > 2 && d->d_name[l - 3] == '.' &&
249 d->d_name[l - 2] == 's' && 243 d->d_name[l - 2] == 's' &&
250 d->d_name[l - 1] == 'h') 244 d->d_name[l - 1] == 'h')
251 continue; 245 continue;
252 } 246 }
253 list = rc_strlist_addsort (list, d->d_name); 247 list = rc_strlist_addsort (list, d->d_name);
254 } 248 }
255 } 249 }
256 closedir (dp); 250 closedir (dp);
257 251
258 if (errno != 0) 252 if (errno != 0) {
259 {
260 eerror ("failed to readdir `%s': %s", dir, strerror (errno)); 253 eerror ("failed to readdir `%s': %s", dir, strerror (errno));
261 rc_strlist_free (list); 254 rc_strlist_free (list);
262 return (NULL); 255 return (NULL);
263 } 256 }
264 257
265 return (list); 258 return (list);
266} 259}
267 260
268bool rc_rm_dir (const char *pathname, bool top) 261bool rc_rm_dir (const char *pathname, bool top)
269{ 262{
270 DIR *dp; 263 DIR *dp;
271 struct dirent *d; 264 struct dirent *d;
272 265
273 if (! pathname) 266 if (! pathname)
274 return (false); 267 return (false);
275 268
276 if ((dp = opendir (pathname)) == NULL) 269 if ((dp = opendir (pathname)) == NULL) {
277 {
278 eerror ("failed to opendir `%s': %s", pathname, strerror (errno)); 270 eerror ("failed to opendir `%s': %s", pathname, strerror (errno));
279 return (false); 271 return (false);
280 } 272 }
281 273
282 errno = 0; 274 errno = 0;
283 while (((d = readdir (dp)) != NULL) && errno == 0) 275 while (((d = readdir (dp)) != NULL) && errno == 0) {
284 {
285 if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) 276 if (strcmp (d->d_name, ".") != 0 && strcmp (d->d_name, "..") != 0) {
286 {
287 char *tmp = rc_strcatpaths (pathname, d->d_name, (char *) NULL); 277 char *tmp = rc_strcatpaths (pathname, d->d_name, (char *) NULL);
288 if (d->d_type == DT_DIR) 278 if (d->d_type == DT_DIR) {
289 {
290 if (! rc_rm_dir (tmp, true)) 279 if (! rc_rm_dir (tmp, true))
291 { 280 {
292 free (tmp); 281 free (tmp);
293 closedir (dp); 282 closedir (dp);
294 return (false); 283 return (false);
295 } 284 }
296 } 285 } else {
297 else 286 if (unlink (tmp)) {
298 {
299 if (unlink (tmp))
300 {
301 eerror ("failed to unlink `%s': %s", tmp, strerror (errno)); 287 eerror ("failed to unlink `%s': %s", tmp, strerror (errno));
302 free (tmp); 288 free (tmp);
303 closedir (dp); 289 closedir (dp);
304 return (false); 290 return (false);
305 } 291 }
306 } 292 }
307 free (tmp); 293 free (tmp);
308 } 294 }
309 } 295 }
310 if (errno != 0) 296 if (errno != 0)
311 eerror ("failed to readdir `%s': %s", pathname, strerror (errno)); 297 eerror ("failed to readdir `%s': %s", pathname, strerror (errno));
312 closedir (dp); 298 closedir (dp);
313 299
314 if (top && rmdir (pathname) != 0) 300 if (top && rmdir (pathname) != 0) {
315 {
316 eerror ("failed to rmdir `%s': %s", pathname, strerror (errno)); 301 eerror ("failed to rmdir `%s': %s", pathname, strerror (errno));
317 return false; 302 return false;
318 } 303 }
319 304
320 return (true); 305 return (true);
321} 306}
322 307
323char **rc_get_config (char **list, const char *file) 308char **rc_get_config (char **list, const char *file)
324{ 309{
325 FILE *fp; 310 FILE *fp;
326 char buffer[RC_LINEBUFFER]; 311 char buffer[RC_LINEBUFFER];
327 char *p; 312 char *p;
328 char *token; 313 char *token;
329 char *line; 314 char *line;
330 char *linep; 315 char *linep;
331 char *linetok; 316 char *linetok;
332 int i = 0; 317 int i = 0;
333 bool replaced; 318 bool replaced;
334 char *entry; 319 char *entry;
335 char *newline; 320 char *newline;
336 321
337 if (! (fp = fopen (file, "r"))) 322 if (! (fp = fopen (file, "r"))) {
338 {
339 ewarn ("load_config_file `%s': %s", file, strerror (errno)); 323 ewarn ("load_config_file `%s': %s", file, strerror (errno));
340 return (list); 324 return (list);
341 } 325 }
342 326
343 while (fgets (buffer, RC_LINEBUFFER, fp)) 327 while (fgets (buffer, RC_LINEBUFFER, fp)) {
344 { 328 p = buffer;
345 p = buffer;
346 329
347 /* Strip leading spaces/tabs */ 330 /* Strip leading spaces/tabs */
348 while ((*p == ' ') || (*p == '\t')) 331 while ((*p == ' ') || (*p == '\t'))
349 p++; 332 p++;
350 333
351 if (! p || strlen (p) < 3 || p[0] == '#') 334 if (! p || strlen (p) < 3 || p[0] == '#')
352 continue; 335 continue;
353 336
354 /* Get entry */ 337 /* Get entry */
355 token = strsep (&p, "="); 338 token = strsep (&p, "=");
356 if (! token) 339 if (! token)
357 continue; 340 continue;
358 341
359 entry = rc_xstrdup (token); 342 entry = rc_xstrdup (token);
360 343
361 do 344 do {
362 {
363 /* Bash variables are usually quoted */ 345 /* Bash variables are usually quoted */
364 token = strsep (&p, "\"\'"); 346 token = strsep (&p, "\"\'");
365 }
366 while ((token) && (strlen (token) == 0)); 347 } while ((token) && (strlen (token) == 0));
367 348
368 /* Drop a newline if that's all we have */ 349 /* Drop a newline if that's all we have */
369 i = strlen (token) - 1; 350 i = strlen (token) - 1;
370 if (token[i] == 10) 351 if (token[i] == 10)
371 token[i] = 0; 352 token[i] = 0;
372 353
373 i = strlen (entry) + strlen (token) + 2; 354 i = strlen (entry) + strlen (token) + 2;
374 newline = rc_xmalloc (i); 355 newline = rc_xmalloc (i);
375 snprintf (newline, i, "%s=%s", entry, token); 356 snprintf (newline, i, "%s=%s", entry, token);
376 357
377 replaced = false; 358 replaced = false;
378 /* In shells the last item takes precedence, so we need to remove 359 /* In shells the last item takes precedence, so we need to remove
379 any prior values we may already have */ 360 any prior values we may already have */
380 STRLIST_FOREACH (list, line, i) 361 STRLIST_FOREACH (list, line, i) {
381 {
382 char *tmp = rc_xstrdup (line); 362 char *tmp = rc_xstrdup (line);
383 linep = tmp; 363 linep = tmp;
384 linetok = strsep (&linep, "="); 364 linetok = strsep (&linep, "=");
385 if (strcmp (linetok, entry) == 0) 365 if (strcmp (linetok, entry) == 0) {
386 {
387 /* We have a match now - to save time we directly replace it */ 366 /* We have a match now - to save time we directly replace it */
388 free (list[i - 1]); 367 free (list[i - 1]);
389 list[i - 1] = newline; 368 list[i - 1] = newline;
390 replaced = true; 369 replaced = true;
391 free (tmp); 370 free (tmp);
392 break; 371 break;
393 } 372 }
394 free (tmp); 373 free (tmp);
395 } 374 }
396 375
397 if (! replaced) 376 if (! replaced) {
398 {
399 list = rc_strlist_addsort (list, newline); 377 list = rc_strlist_addsort (list, newline);
400 free (newline); 378 free (newline);
401 } 379 }
402 free (entry); 380 free (entry);
403 } 381 }
404 fclose (fp); 382 fclose (fp);
405 383
406 return (list); 384 return (list);
407} 385}
408 386
409char *rc_get_config_entry (char **list, const char *entry) 387char *rc_get_config_entry (char **list, const char *entry)
410{ 388{
411 char *line; 389 char *line;
412 int i; 390 int i;
413 char *p; 391 char *p;
414 392
415 STRLIST_FOREACH (list, line, i) 393 STRLIST_FOREACH (list, line, i) {
416 {
417 p = strchr (line, '='); 394 p = strchr (line, '=');
418 if (p && strncmp (entry, line, p - line) == 0) 395 if (p && strncmp (entry, line, p - line) == 0)
419 return (p += 1); 396 return (p += 1);
420 } 397 }
421 398
422 return (NULL); 399 return (NULL);
423} 400}
424 401
425char **rc_get_list (char **list, const char *file) 402char **rc_get_list (char **list, const char *file)
426{ 403{
427 FILE *fp; 404 FILE *fp;
428 char buffer[RC_LINEBUFFER]; 405 char buffer[RC_LINEBUFFER];
429 char *p; 406 char *p;
430 char *token; 407 char *token;
431 408
432 if (! (fp = fopen (file, "r"))) 409 if (! (fp = fopen (file, "r"))) {
433 {
434 ewarn ("rc_get_list `%s': %s", file, strerror (errno)); 410 ewarn ("rc_get_list `%s': %s", file, strerror (errno));
435 return (list); 411 return (list);
436 } 412 }
437 413
438 while (fgets (buffer, RC_LINEBUFFER, fp)) 414 while (fgets (buffer, RC_LINEBUFFER, fp)) {
439 { 415 p = buffer;
440 p = buffer;
441 416
442 /* Strip leading spaces/tabs */ 417 /* Strip leading spaces/tabs */
443 while ((*p == ' ') || (*p == '\t')) 418 while ((*p == ' ') || (*p == '\t'))
444 p++; 419 p++;
445 420
446 /* Get entry - we do not want comments */ 421 /* Get entry - we do not want comments */
447 token = strsep (&p, "#"); 422 token = strsep (&p, "#");
448 if (token && (strlen (token) > 1)) 423 if (token && (strlen (token) > 1)) {
449 {
450 /* Stip the newline if present */ 424 /* Stip the newline if present */
451 if (token[strlen (token) - 1] == '\n') 425 if (token[strlen (token) - 1] == '\n')
452 token[strlen (token) - 1] = 0; 426 token[strlen (token) - 1] = 0;
453 427
454 list = rc_strlist_add (list, token); 428 list = rc_strlist_add (list, token);
455 } 429 }
456 } 430 }
457 fclose (fp); 431 fclose (fp);
458 432
459 return (list); 433 return (list);
460} 434}
461 435
462char **rc_filter_env (void) 436char **rc_filter_env (void)
463{ 437{
464 char **env = NULL; 438 char **env = NULL;
465 char **whitelist = NULL; 439 char **whitelist = NULL;
466 char *env_name = NULL; 440 char *env_name = NULL;
467 char **profile = NULL; 441 char **profile = NULL;
468 int count = 0; 442 int count = 0;
469 bool got_path = false; 443 bool got_path = false;
470 char *env_var; 444 char *env_var;
471 int env_len; 445 int env_len;
472 char *p; 446 char *p;
473 char *token; 447 char *token;
474 char *sep; 448 char *sep;
475 char *e; 449 char *e;
476 int pplen = strlen (PATH_PREFIX); 450 int pplen = strlen (PATH_PREFIX);
477 451
478 whitelist = rc_get_list (whitelist, SYS_WHITELIST); 452 whitelist = rc_get_list (whitelist, SYS_WHITELIST);
479 if (! whitelist) 453 if (! whitelist)
480 ewarn ("system environment whitelist (" SYS_WHITELIST ") missing"); 454 ewarn ("system environment whitelist (" SYS_WHITELIST ") missing");
481 455
482 whitelist = rc_get_list (whitelist, USR_WHITELIST); 456 whitelist = rc_get_list (whitelist, USR_WHITELIST);
483 457
484 if (! whitelist) 458 if (! whitelist)
485 return (NULL); 459 return (NULL);
486 460
487 if (rc_is_file (PROFILE_ENV)) 461 if (rc_is_file (PROFILE_ENV))
488 profile = rc_get_config (profile, PROFILE_ENV); 462 profile = rc_get_config (profile, PROFILE_ENV);
489 463
490 STRLIST_FOREACH (whitelist, env_name, count) 464 STRLIST_FOREACH (whitelist, env_name, count) {
491 {
492 char *space = strchr (env_name, ' '); 465 char *space = strchr (env_name, ' ');
493 if (space) 466 if (space)
494 *space = 0; 467 *space = 0;
495 468
496 env_var = getenv (env_name); 469 env_var = getenv (env_name);
497 470
498 if (! env_var && profile) 471 if (! env_var && profile) {
499 {
500 env_len = strlen (env_name) + strlen ("export ") + 1; 472 env_len = strlen (env_name) + strlen ("export ") + 1;
501 p = rc_xmalloc (sizeof (char *) * env_len); 473 p = rc_xmalloc (sizeof (char *) * env_len);
502 snprintf (p, env_len, "export %s", env_name); 474 snprintf (p, env_len, "export %s", env_name);
503 env_var = rc_get_config_entry (profile, p); 475 env_var = rc_get_config_entry (profile, p);
504 free (p); 476 free (p);
505 } 477 }
506 478
507 if (! env_var) 479 if (! env_var)
508 continue; 480 continue;
509 481
510 /* Ensure our PATH is prefixed with the system locations first 482 /* Ensure our PATH is prefixed with the system locations first
511 for a little extra security */ 483 for a little extra security */
512 if (strcmp (env_name, "PATH") == 0 && 484 if (strcmp (env_name, "PATH") == 0 &&
513 strncmp (PATH_PREFIX, env_var, pplen) != 0) 485 strncmp (PATH_PREFIX, env_var, pplen) != 0)
514 { 486 {
515 got_path = true; 487 got_path = true;
516 env_len = strlen (env_name) + strlen (env_var) + pplen + 2; 488 env_len = strlen (env_name) + strlen (env_var) + pplen + 2;
517 e = p = rc_xmalloc (sizeof (char *) * env_len); 489 e = p = rc_xmalloc (sizeof (char *) * env_len);
518 p += snprintf (e, env_len, "%s=%s", env_name, PATH_PREFIX); 490 p += snprintf (e, env_len, "%s=%s", env_name, PATH_PREFIX);
519 491
520 /* Now go through the env var and only add bits not in our PREFIX */ 492 /* Now go through the env var and only add bits not in our PREFIX */
521 sep = env_var; 493 sep = env_var;
522 while ((token = strsep (&sep, ":"))) 494 while ((token = strsep (&sep, ":"))) {
523 {
524 char *np = strdup (PATH_PREFIX); 495 char *np = strdup (PATH_PREFIX);
525 char *npp = np; 496 char *npp = np;
526 char *tok = NULL; 497 char *tok = NULL;
527 while ((tok = strsep (&npp, ":"))) 498 while ((tok = strsep (&npp, ":")))
528 if (strcmp (tok, token) == 0) 499 if (strcmp (tok, token) == 0)
529 break; 500 break;
530 if (! tok) 501 if (! tok)
531 p += snprintf (p, env_len - (p - e), ":%s", token); 502 p += snprintf (p, env_len - (p - e), ":%s", token);
532 free (np); 503 free (np);
533 } 504 }
534 *p++ = 0; 505 *p++ = 0;
535 } 506 } else {
536 else
537 {
538 env_len = strlen (env_name) + strlen (env_var) + 2; 507 env_len = strlen (env_name) + strlen (env_var) + 2;
539 e = rc_xmalloc (sizeof (char *) * env_len); 508 e = rc_xmalloc (sizeof (char *) * env_len);
540 snprintf (e, env_len, "%s=%s", env_name, env_var); 509 snprintf (e, env_len, "%s=%s", env_name, env_var);
541 } 510 }
542 511
543 env = rc_strlist_add (env, e); 512 env = rc_strlist_add (env, e);
544 free (e); 513 free (e);
545 } 514 }
546 515
547 /* We filtered the env but didn't get a PATH? Very odd. 516 /* We filtered the env but didn't get a PATH? Very odd.
548 However, we do need a path, so use a default. */ 517 However, we do need a path, so use a default. */
549 if (! got_path) 518 if (! got_path) {
550 {
551 env_len = strlen ("PATH=") + strlen (PATH_PREFIX) + 2; 519 env_len = strlen ("PATH=") + strlen (PATH_PREFIX) + 2;
552 p = rc_xmalloc (sizeof (char *) * env_len); 520 p = rc_xmalloc (sizeof (char *) * env_len);
553 snprintf (p, env_len, "PATH=%s", PATH_PREFIX); 521 snprintf (p, env_len, "PATH=%s", PATH_PREFIX);
554 env = rc_strlist_add (env, p); 522 env = rc_strlist_add (env, p);
555 free (p); 523 free (p);
556 } 524 }
557 525
558 rc_strlist_free (whitelist); 526 rc_strlist_free (whitelist);
559 rc_strlist_free (profile); 527 rc_strlist_free (profile);
560 528
561 return (env); 529 return (env);
562} 530}
563 531
564/* Other systems may need this at some point, but for now it's Linux only */ 532/* Other systems may need this at some point, but for now it's Linux only */
565#ifdef __linux__ 533#ifdef __linux__
566static bool file_regex (const char *file, const char *regex) 534static bool file_regex (const char *file, const char *regex)
567{ 535{
568 FILE *fp; 536 FILE *fp;
569 char buffer[RC_LINEBUFFER]; 537 char buffer[RC_LINEBUFFER];
570 regex_t re; 538 regex_t re;
571 bool retval = false; 539 bool retval = false;
572 int result; 540 int result;
573 541
574 if (! rc_exists (file)) 542 if (! rc_exists (file))
575 return (false); 543 return (false);
576 544
577 if (! (fp = fopen (file, "r"))) 545 if (! (fp = fopen (file, "r"))) {
578 {
579 ewarn ("file_regex `%s': %s", file, strerror (errno)); 546 ewarn ("file_regex `%s': %s", file, strerror (errno));
580 return (false); 547 return (false);
581 } 548 }
582 549
583 if ((result = regcomp (&re, regex, REG_EXTENDED | REG_NOSUB)) != 0) 550 if ((result = regcomp (&re, regex, REG_EXTENDED | REG_NOSUB)) != 0) {
584 {
585 fclose (fp); 551 fclose (fp);
586 regerror (result, &re, buffer, sizeof (buffer)); 552 regerror (result, &re, buffer, sizeof (buffer));
587 eerror ("file_regex: %s", buffer); 553 eerror ("file_regex: %s", buffer);
588 return (false); 554 return (false);
589 } 555 }
590 556
591 while (fgets (buffer, RC_LINEBUFFER, fp)) 557 while (fgets (buffer, RC_LINEBUFFER, fp)) {
592 {
593 if (regexec (&re, buffer, 0, NULL, 0) == 0) 558 if (regexec (&re, buffer, 0, NULL, 0) == 0)
594 { 559 {
595 retval = true; 560 retval = true;
596 break; 561 break;
597 } 562 }
598 } 563 }
599 fclose (fp); 564 fclose (fp);
600 regfree (&re); 565 regfree (&re);
601 566
602 return (retval); 567 return (retval);
603} 568}
604#endif 569#endif
605 570
606char **rc_config_env (char **env) 571char **rc_config_env (char **env)
607{ 572{
608 char *line; 573 char *line;
609 int i; 574 int i;
610 char *p; 575 char *p;
611 char **config = rc_get_config (NULL, RC_CONFIG); 576 char **config = rc_get_config (NULL, RC_CONFIG);
612 char *e; 577 char *e;
613 char sys[6]; 578 char sys[6];
614 struct utsname uts; 579 struct utsname uts;
615 bool has_net_fs_list = false; 580 bool has_net_fs_list = false;
616 FILE *fp; 581 FILE *fp;
617 char buffer[PATH_MAX]; 582 char buffer[PATH_MAX];
618 583
619 STRLIST_FOREACH (config, line, i) 584 STRLIST_FOREACH (config, line, i) {
620 {
621 p = strchr (line, '='); 585 p = strchr (line, '=');
622 if (! p) 586 if (! p)
623 continue; 587 continue;
624 588
625 *p = 0; 589 *p = 0;
626 e = getenv (line); 590 e = getenv (line);
627 if (! e) 591 if (! e) {
628 { 592 *p = '=';
629 *p = '=';
630 env = rc_strlist_add (env, line); 593 env = rc_strlist_add (env, line);
631 } 594 } else {
632 else
633 {
634 int len = strlen (line) + strlen (e) + 2; 595 int len = strlen (line) + strlen (e) + 2;
635 char *new = rc_xmalloc (sizeof (char *) * len); 596 char *new = rc_xmalloc (sizeof (char *) * len);
636 snprintf (new, len, "%s=%s", line, e); 597 snprintf (new, len, "%s=%s", line, e);
637 env = rc_strlist_add (env, new); 598 env = rc_strlist_add (env, new);
638 free (new); 599 free (new);
639 } 600 }
640 } 601 }
641 rc_strlist_free (config); 602 rc_strlist_free (config);
642 603
643 /* One char less to drop the trailing / */ 604 /* One char less to drop the trailing / */
644 i = strlen ("RC_LIBDIR=") + strlen (RC_LIBDIR); 605 i = strlen ("RC_LIBDIR=") + strlen (RC_LIBDIR);
645 line = rc_xmalloc (sizeof (char *) * i); 606 line = rc_xmalloc (sizeof (char *) * i);
646 snprintf (line, i, "RC_LIBDIR=" RC_LIBDIR); 607 snprintf (line, i, "RC_LIBDIR=" RC_LIBDIR);
647 env = rc_strlist_add (env, line); 608 env = rc_strlist_add (env, line);
648 free (line); 609 free (line);
649 610
650 /* One char less to drop the trailing / */ 611 /* One char less to drop the trailing / */
651 i = strlen ("RC_SVCDIR=") + strlen (RC_SVCDIR); 612 i = strlen ("RC_SVCDIR=") + strlen (RC_SVCDIR);
652 line = rc_xmalloc (sizeof (char *) * i); 613 line = rc_xmalloc (sizeof (char *) * i);
653 snprintf (line, i, "RC_SVCDIR=" RC_SVCDIR); 614 snprintf (line, i, "RC_SVCDIR=" RC_SVCDIR);
654 env = rc_strlist_add (env, line); 615 env = rc_strlist_add (env, line);
655 free (line); 616 free (line);
656 617
657 env = rc_strlist_add (env, "RC_BOOTLEVEL=" RC_LEVEL_BOOT); 618 env = rc_strlist_add (env, "RC_BOOTLEVEL=" RC_LEVEL_BOOT);
658 619
659 p = rc_get_runlevel (); 620 p = rc_get_runlevel ();
660 i = strlen ("RC_SOFTLEVEL=") + strlen (p) + 1; 621 i = strlen ("RC_SOFTLEVEL=") + strlen (p) + 1;
661 line = rc_xmalloc (sizeof (char *) * i); 622 line = rc_xmalloc (sizeof (char *) * i);
662 snprintf (line, i, "RC_SOFTLEVEL=%s", p); 623 snprintf (line, i, "RC_SOFTLEVEL=%s", p);
663 env = rc_strlist_add (env, line); 624 env = rc_strlist_add (env, line);
664 free (line); 625 free (line);
665 626
666 if (rc_exists (RC_SVCDIR "ksoftlevel")) 627 if (rc_exists (RC_SVCDIR "ksoftlevel")) {
667 {
668 if (! (fp = fopen (RC_SVCDIR "ksoftlevel", "r"))) 628 if (! (fp = fopen (RC_SVCDIR "ksoftlevel", "r")))
669 eerror ("fopen `%s': %s", RC_SVCDIR "ksoftlevel", 629 eerror ("fopen `%s': %s", RC_SVCDIR "ksoftlevel",
670 strerror (errno)); 630 strerror (errno));
671 else 631 else {
672 {
673 memset (buffer, 0, sizeof (buffer)); 632 memset (buffer, 0, sizeof (buffer));
674 if (fgets (buffer, sizeof (buffer), fp)) 633 if (fgets (buffer, sizeof (buffer), fp)) {
675 { 634 i = strlen (buffer) - 1;
676 i = strlen (buffer) - 1; 635 if (buffer[i] == '\n')
677 if (buffer[i] == '\n') 636 buffer[i] = 0;
678 buffer[i] = 0;
679 i += strlen ("RC_DEFAULTLEVEL=") + 2; 637 i += strlen ("RC_DEFAULTLEVEL=") + 2;
680 line = rc_xmalloc (sizeof (char *) * i); 638 line = rc_xmalloc (sizeof (char *) * i);
681 snprintf (line, i, "RC_DEFAULTLEVEL=%s", buffer); 639 snprintf (line, i, "RC_DEFAULTLEVEL=%s", buffer);
682 env = rc_strlist_add (env, line); 640 env = rc_strlist_add (env, line);
683 free (line); 641 free (line);
684 } 642 }
685 fclose (fp); 643 fclose (fp);
686 } 644 }
687 }
688 else 645 } else
689 env = rc_strlist_add (env, "RC_DEFAULTLEVEL=" RC_LEVEL_DEFAULT); 646 env = rc_strlist_add (env, "RC_DEFAULTLEVEL=" RC_LEVEL_DEFAULT);
690 647
691 memset (sys, 0, sizeof (sys)); 648 memset (sys, 0, sizeof (sys));
692 649
693 /* Linux can run some funky stuff like Xen, VServer, UML, etc 650 /* Linux can run some funky stuff like Xen, VServer, UML, etc
694 We store this special system in RC_SYS so our scripts run fast */ 651 We store this special system in RC_SYS so our scripts run fast */
695#ifdef __linux__ 652#ifdef __linux__
696 if (rc_is_dir ("/proc/xen")) 653 if (rc_is_dir ("/proc/xen")) {
697 {
698 fp = fopen ("/proc/xen/capabilities", "r"); 654 fp = fopen ("/proc/xen/capabilities", "r");
699 if (fp) 655 if (fp) {
700 { 656 fclose (fp);
701 fclose (fp);
702 if (file_regex ("/proc/xen/capabilities", "control_d")) 657 if (file_regex ("/proc/xen/capabilities", "control_d"))
703 snprintf (sys, sizeof (sys), "XENU"); 658 snprintf (sys, sizeof (sys), "XENU");
704 } 659 }
705 if (! sys) 660 if (! sys)
706 snprintf (sys, sizeof (sys), "XEN0"); 661 snprintf (sys, sizeof (sys), "XEN0");
707 }
708 else if (file_regex ("/proc/cpuinfo", "UML")) 662 } else if (file_regex ("/proc/cpuinfo", "UML"))
709 snprintf (sys, sizeof (sys), "UML"); 663 snprintf (sys, sizeof (sys), "UML");
710 else if (file_regex ("/proc/self/status", 664 else if (file_regex ("/proc/self/status",
711 "(s_context|VxID|envID):[[:space:]]*[1-9]")) 665 "(s_context|VxID|envID):[[:space:]]*[1-9]"))
712 snprintf (sys, sizeof (sys), "VPS"); 666 snprintf (sys, sizeof (sys), "VPS");
713#endif 667#endif
714 668
715 /* Only add a NET_FS list if not defined */ 669 /* Only add a NET_FS list if not defined */
716 STRLIST_FOREACH (env, line, i) 670 STRLIST_FOREACH (env, line, i)
717 if (strncmp (line, "RC_NET_FS_LIST=", strlen ("RC_NET_FS_LIST=")) == 0) 671 if (strncmp (line, "RC_NET_FS_LIST=", strlen ("RC_NET_FS_LIST=")) == 0) {
718 {
719 has_net_fs_list = true; 672 has_net_fs_list = true;
720 break; 673 break;
721 } 674 }
675
722 if (! has_net_fs_list) 676 if (! has_net_fs_list) {
723 {
724 i = strlen ("RC_NET_FS_LIST=") + strlen (RC_NET_FS_LIST_DEFAULT) + 1; 677 i = strlen ("RC_NET_FS_LIST=") + strlen (RC_NET_FS_LIST_DEFAULT) + 1;
725 line = rc_xmalloc (sizeof (char *) * i); 678 line = rc_xmalloc (sizeof (char *) * i);
726 snprintf (line, i, "RC_NET_FS_LIST=%s", RC_NET_FS_LIST_DEFAULT); 679 snprintf (line, i, "RC_NET_FS_LIST=%s", RC_NET_FS_LIST_DEFAULT);
727 env = rc_strlist_add (env, line); 680 env = rc_strlist_add (env, line);
728 free (line); 681 free (line);
729 } 682 }
730 683
731 if (sys[0]) 684 if (sys[0]) {
732 {
733 i = strlen ("RC_SYS=") + strlen (sys) + 2; 685 i = strlen ("RC_SYS=") + strlen (sys) + 2;
734 line = rc_xmalloc (sizeof (char *) * i); 686 line = rc_xmalloc (sizeof (char *) * i);
735 snprintf (line, i, "RC_SYS=%s", sys); 687 snprintf (line, i, "RC_SYS=%s", sys);
736 env = rc_strlist_add (env, line); 688 env = rc_strlist_add (env, line);
737 free (line); 689 free (line);
738 } 690 }
739 691
740 /* Some scripts may need to take a different code path if Linux/FreeBSD, etc 692 /* Some scripts may need to take a different code path if Linux/FreeBSD, etc
741 To save on calling uname, we store it in an environment variable */ 693 To save on calling uname, we store it in an environment variable */
742 if (uname (&uts) == 0) 694 if (uname (&uts) == 0) {
743 {
744 i = strlen ("RC_UNAME=") + strlen (uts.sysname) + 2; 695 i = strlen ("RC_UNAME=") + strlen (uts.sysname) + 2;
745 line = rc_xmalloc (sizeof (char *) * i); 696 line = rc_xmalloc (sizeof (char *) * i);
746 snprintf (line, i, "RC_UNAME=%s", uts.sysname); 697 snprintf (line, i, "RC_UNAME=%s", uts.sysname);
747 env = rc_strlist_add (env, line); 698 env = rc_strlist_add (env, line);
748 free (line); 699 free (line);
749 } 700 }
750 701
751 return (env); 702 return (env);
752} 703}

Legend:
Removed from v.2576  
changed lines
  Added in v.2577

  ViewVC Help
Powered by ViewVC 1.1.20