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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20