diff options
author | Christian Heim <phreak@gentoo.org> | 2005-12-09 15:08:55 +0000 |
---|---|---|
committer | Christian Heim <phreak@gentoo.org> | 2005-12-09 15:08:55 +0000 |
commit | e9bfd1e41c9da755fe0797fdff82f22104462919 (patch) | |
tree | 36433397e8756954d82c45abf739642d4ee78e0c /src | |
parent | Fixing last ChangeLog.vserver entry (goddammit it was rev 1733 and not 1773) (diff) | |
download | baselayout-vserver-e9bfd1e41c9da755fe0797fdff82f22104462919.tar.gz baselayout-vserver-e9bfd1e41c9da755fe0797fdff82f22104462919.tar.bz2 baselayout-vserver-e9bfd1e41c9da755fe0797fdff82f22104462919.zip |
Importing latest baselayout/trunk changes. This is a merge with 1752.
svn path=/baselayout-vserver/trunk/; revision=164
Diffstat (limited to 'src')
-rw-r--r-- | src/core/librcscripts/api/debug.h | 10 | ||||
-rw-r--r-- | src/core/librcscripts/api/dynbuf.h | 15 | ||||
-rw-r--r-- | src/core/librcscripts/api/parse.h | 6 | ||||
-rw-r--r-- | src/core/librcscripts/api/runlevels.h | 2 | ||||
-rw-r--r-- | src/core/librcscripts/depend.c | 18 | ||||
-rw-r--r-- | src/core/librcscripts/dynbuf.c | 105 | ||||
-rw-r--r-- | src/core/librcscripts/misc.c | 86 | ||||
-rw-r--r-- | src/core/librcscripts/parse.c | 177 | ||||
-rw-r--r-- | src/core/librcscripts/runlevels.c | 2 | ||||
-rw-r--r-- | src/core/librcscripts/scripts.c | 32 | ||||
-rw-r--r-- | src/core/librcscripts/simple-regex.c | 38 |
11 files changed, 276 insertions, 215 deletions
diff --git a/src/core/librcscripts/api/debug.h b/src/core/librcscripts/api/debug.h index a0b798f..14e3dc6 100644 --- a/src/core/librcscripts/api/debug.h +++ b/src/core/librcscripts/api/debug.h @@ -87,15 +87,15 @@ inline bool check_fp (FILE * fp); */ inline bool __check_arg_ptr (const void *ptr, const char *file, const char *func, - size_t line); + size_t line); inline bool __check_arg_str (const char *str, const char *file, const char *func, - size_t line); + size_t line); inline bool __check_arg_strv (char **str, const char *file, const char *func, - size_t line); + size_t line); inline bool __check_arg_fd (int fd, const char *file, const char *func, - size_t line); + size_t line); inline bool __check_arg_fp (FILE * fp, const char *file, const char *func, - size_t line); + size_t line); #define check_arg_ptr(_ptr) \ __check_arg_ptr (_ptr, __FILE__, __FUNCTION__, __LINE__) diff --git a/src/core/librcscripts/api/dynbuf.h b/src/core/librcscripts/api/dynbuf.h index 3b0049e..5c937b6 100644 --- a/src/core/librcscripts/api/dynbuf.h +++ b/src/core/librcscripts/api/dynbuf.h @@ -33,21 +33,24 @@ typedef struct size_t length; /* Length of data block */ size_t rd_index; /* Current read index */ size_t wr_index; /* Current write index */ + bool file_map; /* File mapped as dynbuf */ } dyn_buf_t; dyn_buf_t *new_dyn_buf (void); -void free_dyn_buf (dyn_buf_t * dynbuf); +dyn_buf_t *new_dyn_buf_mmap_file (const char *name); -int write_dyn_buf (dyn_buf_t * dynbuf, const char *buf, size_t length); +void free_dyn_buf (dyn_buf_t *dynbuf); -int write_dyn_buf_from_fd (int fd, dyn_buf_t * dynbuf, size_t length); +int write_dyn_buf (dyn_buf_t *dynbuf, const char *buf, size_t length); -int sprintf_dyn_buf (dyn_buf_t * dynbuf, const char *format, ...); +int write_dyn_buf_from_fd (int fd, dyn_buf_t *dynbuf, size_t length); -int read_dyn_buf (dyn_buf_t * dynbuf, char *buf, size_t length); +int sprintf_dyn_buf (dyn_buf_t *dynbuf, const char *format, ...); -int read_dyn_buf_to_fd (int fd, dyn_buf_t * dynbuf, size_t length); +int read_dyn_buf (dyn_buf_t *dynbuf, char *buf, size_t length); + +int read_dyn_buf_to_fd (int fd, dyn_buf_t *dynbuf, size_t length); char *read_line_dyn_buf (dyn_buf_t *dynbuf); diff --git a/src/core/librcscripts/api/parse.h b/src/core/librcscripts/api/parse.h index 911793c..b5e07fa 100644 --- a/src/core/librcscripts/api/parse.h +++ b/src/core/librcscripts/api/parse.h @@ -35,15 +35,15 @@ #define FIELD_PROVIDE "PROVIDE" #define FIELD_FAILED "FAILED" -size_t generate_stage1 (dyn_buf_t * data); -size_t generate_stage2 (dyn_buf_t * data); +size_t generate_stage1 (dyn_buf_t *data); +size_t generate_stage2 (dyn_buf_t *data); size_t read_stage2 (char **data); int write_stage2 (FILE * outfile); size_t generate_stage3 (char **data); size_t read_stage3 (char **data); int write_stage3 (FILE * outfile); int write_legacy_stage3 (FILE * output); -int parse_cache (const dyn_buf_t * data); +int parse_cache (const dyn_buf_t *data); /* * get_rcscripts() diff --git a/src/core/librcscripts/api/runlevels.h b/src/core/librcscripts/api/runlevels.h index 59906af..ccc9ed0 100644 --- a/src/core/librcscripts/api/runlevels.h +++ b/src/core/librcscripts/api/runlevels.h @@ -37,7 +37,7 @@ struct list_head runlevel_list; int get_runlevels (void); -runlevel_info_t * get_runlevel_info (const char *runlevel); +runlevel_info_t *get_runlevel_info (const char *runlevel); bool is_runlevel (const char *runlevel); diff --git a/src/core/librcscripts/depend.c b/src/core/librcscripts/depend.c index c3a41c8..a5a2629 100644 --- a/src/core/librcscripts/depend.c +++ b/src/core/librcscripts/depend.c @@ -46,9 +46,9 @@ char *service_type_names[] = { NULL }; -static char * -service_is_recursive_dependency (char *servicename, char *dependency, - bool checkuse); +static char *service_is_recursive_dependency (char *servicename, + char *dependency, + bool checkuse); static int __service_resolve_dependency (char *servicename, char *dependency, service_type_t type); @@ -196,7 +196,7 @@ service_add_dependency (char *servicename, char *dependency, service_type_t type) { service_info_t *info; - char *tmp_buf; + char *buf; if ((!check_arg_str (servicename)) || (!check_arg_str (dependency))) return -1; @@ -210,11 +210,11 @@ service_add_dependency (char *servicename, char *dependency, DBG_MSG ("Adding dependency '%s' of service '%s', type '%s'.\n", dependency, servicename, service_type_names[type]); - tmp_buf = xstrndup (dependency, strlen (dependency)); - if (NULL == tmp_buf) + buf = xstrndup (dependency, strlen (dependency)); + if (NULL == buf) return -1; - str_list_add_item_sorted (info->depend_info[type], tmp_buf, error); + str_list_add_item_sorted (info->depend_info[type], buf, error); } else { @@ -462,7 +462,7 @@ __service_resolve_dependency (char *servicename, char *dependency, if (type == BEFORE) { char *depend; - + /* NEED and USE override BEFORE * ('servicename' BEFORE 'dependency') */ if ((0 == service_is_dependency (servicename, dependency, NEED)) @@ -489,7 +489,7 @@ __service_resolve_dependency (char *servicename, char *dependency, if (type == AFTER) { char *depend; - + /* NEED and USE override AFTER * ('servicename' AFTER 'dependency') */ if ((0 == service_is_dependency (dependency, servicename, NEED)) diff --git a/src/core/librcscripts/dynbuf.c b/src/core/librcscripts/dynbuf.c index 65e8a43..5fc156e 100644 --- a/src/core/librcscripts/dynbuf.c +++ b/src/core/librcscripts/dynbuf.c @@ -30,7 +30,7 @@ #include "rcscripts.h" -static dyn_buf_t *reallocate_dyn_buf (dyn_buf_t * dynbuf, size_t needed); +static dyn_buf_t *reallocate_dyn_buf (dyn_buf_t *dynbuf, size_t needed); dyn_buf_t * new_dyn_buf (void) @@ -51,18 +51,51 @@ new_dyn_buf (void) dynbuf->length = DYNAMIC_BUFFER_SIZE; dynbuf->rd_index = 0; dynbuf->wr_index = 0; + dynbuf->file_map = FALSE; return dynbuf; } dyn_buf_t * -reallocate_dyn_buf (dyn_buf_t * dynbuf, size_t needed) +new_dyn_buf_mmap_file (const char *name) +{ + dyn_buf_t *dynbuf = NULL; + + dynbuf = xmalloc (sizeof (dyn_buf_t)); + if (NULL == dynbuf) + return NULL; + + if (-1 == file_map (name, &dynbuf->data, &dynbuf->length)) + { + DBG_MSG ("Failed to mmap file '%s'\n", name); + free (dynbuf); + + return NULL; + } + + dynbuf->wr_index = dynbuf->length; + dynbuf->rd_index = 0; + dynbuf->file_map = TRUE; + + return dynbuf; +} + +dyn_buf_t * +reallocate_dyn_buf (dyn_buf_t *dynbuf, size_t needed) { int len; if (!check_arg_dyn_buf (dynbuf)) return NULL; + if (dynbuf->file_map) + { + errno = EPERM; + DBG_MSG ("Cannot reallocate mmap()'d file!\n"); + + return NULL; + } + len = sizeof (char) * (dynbuf->wr_index + needed + 1); if (dynbuf->length < len) @@ -85,15 +118,24 @@ reallocate_dyn_buf (dyn_buf_t * dynbuf, size_t needed) } void -free_dyn_buf (dyn_buf_t * dynbuf) +free_dyn_buf (dyn_buf_t *dynbuf) { if (NULL == dynbuf) return; - if (NULL != dynbuf->data) + if (!dynbuf->file_map) + { + if (NULL != dynbuf->data) + { + free (dynbuf->data); + dynbuf->data = NULL; + } + } + else { - free (dynbuf->data); - dynbuf->data = NULL; + save_errno (); + file_unmap (dynbuf->data, dynbuf->length); + restore_errno (); } dynbuf->length = 0; @@ -105,16 +147,24 @@ free_dyn_buf (dyn_buf_t * dynbuf) } int -write_dyn_buf (dyn_buf_t * dynbuf, const char *buf, size_t length) +write_dyn_buf (dyn_buf_t *dynbuf, const char *buf, size_t length) { int len; if (!check_arg_dyn_buf (dynbuf)) return -1; - if (!check_arg_ptr ((char *) buf)) + if (!check_arg_str (buf)) return -1; + if (dynbuf->file_map) + { + errno = EPERM; + DBG_MSG ("Cannot write to readonly mmap()'d file!\n"); + + return -1; + } + if (NULL == reallocate_dyn_buf (dynbuf, length)) { DBG_MSG ("Could not reallocate dynamic buffer!\n"); @@ -137,7 +187,7 @@ write_dyn_buf (dyn_buf_t * dynbuf, const char *buf, size_t length) return length; } -int write_dyn_buf_from_fd (int fd, dyn_buf_t * dynbuf, size_t length) +int write_dyn_buf_from_fd (int fd, dyn_buf_t *dynbuf, size_t length) { int len = length; @@ -147,6 +197,14 @@ int write_dyn_buf_from_fd (int fd, dyn_buf_t * dynbuf, size_t length) if (!check_arg_fd (fd)) return -1; + if (dynbuf->file_map) + { + errno = EPERM; + DBG_MSG ("Cannot write to readonly mmap()'d file!\n"); + + return -1; + } + if (NULL == reallocate_dyn_buf (dynbuf, length)) { DBG_MSG ("Could not reallocate dynamic buffer!\n"); @@ -170,7 +228,7 @@ int write_dyn_buf_from_fd (int fd, dyn_buf_t * dynbuf, size_t length) } int -sprintf_dyn_buf (dyn_buf_t * dynbuf, const char *format, ...) +sprintf_dyn_buf (dyn_buf_t *dynbuf, const char *format, ...) { va_list arg1, arg2; char test_str[10]; @@ -179,6 +237,17 @@ sprintf_dyn_buf (dyn_buf_t * dynbuf, const char *format, ...) if (!check_arg_dyn_buf (dynbuf)) return -1; + if (!check_arg_str (format)) + return -1; + + if (dynbuf->file_map) + { + errno = EPERM; + DBG_MSG ("Cannot write to readonly mmap()'d file!\n"); + + return -1; + } + va_start (arg1, format); va_copy (arg2, arg1); @@ -206,7 +275,7 @@ sprintf_dyn_buf (dyn_buf_t * dynbuf, const char *format, ...) } int -read_dyn_buf (dyn_buf_t * dynbuf, char *buf, size_t length) +read_dyn_buf (dyn_buf_t *dynbuf, char *buf, size_t length) { int len = length; @@ -239,7 +308,7 @@ read_dyn_buf (dyn_buf_t * dynbuf, char *buf, size_t length) } int -read_dyn_buf_to_fd (int fd, dyn_buf_t * dynbuf, size_t length) +read_dyn_buf_to_fd (int fd, dyn_buf_t *dynbuf, size_t length) { int len = length; @@ -277,17 +346,23 @@ read_line_dyn_buf (dyn_buf_t *dynbuf) if (!check_arg_dyn_buf (dynbuf)) return NULL; + if (dynbuf->rd_index == dynbuf->wr_index) + return NULL; + for (count = dynbuf->rd_index; count < dynbuf->wr_index && dynbuf->data[count] != '\n'; count++); - if (count > dynbuf->rd_index) + if (count <= dynbuf->wr_index) { buf = xstrndup ((dynbuf->data + dynbuf->rd_index), (count - dynbuf->rd_index)); if (NULL == buf) return NULL; + dynbuf->rd_index = count; + /* Also skip the '\n' .. */ - dynbuf->rd_index = count + 1; + if (dynbuf->rd_index < dynbuf->wr_index) + dynbuf->rd_index++; } return buf; @@ -316,7 +391,7 @@ check_dyn_buf (dyn_buf_t *dynbuf) inline bool __check_arg_dyn_buf (dyn_buf_t *dynbuf, const char *file, const char *func, - size_t line) + size_t line) { if (!check_dyn_buf (dynbuf)) { diff --git a/src/core/librcscripts/misc.c b/src/core/librcscripts/misc.c index b08a3a6..08546ee 100644 --- a/src/core/librcscripts/misc.c +++ b/src/core/librcscripts/misc.c @@ -86,7 +86,9 @@ strndup (const char *str, size_t size) char *new_str = NULL; size_t len; - if (!check_arg_str (str)) + /* We cannot check if its a valid string here, as it might + * not be '\0' terminated ... */ + if (!check_arg_ptr (str)) return NULL; /* Check lenght of str without breaching the size limit */ @@ -406,20 +408,20 @@ ls_dir (const char *pathname, int hidden) && (hidden ? 1 : dir_entry->d_name[0] != '.')) { char *d_name = dir_entry->d_name; - char *tmp_p; + char *str_ptr; /* Do not list current or parent entries */ if ((0 == strcmp (d_name, ".")) || (0 == strcmp (d_name, ".."))) continue; - tmp_p = strcatpaths (pathname, d_name); - if (NULL == tmp_p) + str_ptr = strcatpaths (pathname, d_name); + if (NULL == str_ptr) { DBG_MSG ("Failed to allocate buffer!\n"); goto error; } - str_list_add_item (dirlist, tmp_p, error); + str_list_add_item (dirlist, str_ptr, error); } } while (NULL != dir_entry); @@ -457,14 +459,11 @@ error: char * get_cnf_entry (const char *pathname, const char *entry) { + dyn_buf_t *dynbuf = NULL; char *buf = NULL; - char *tmp_buf = NULL; - char *tmp_p; + char *str_ptr; char *value = NULL; char *token; - size_t lenght; - int count; - int current = 0; if ((!check_arg_str (pathname)) || (!check_arg_str (entry))) @@ -478,28 +477,23 @@ get_cnf_entry (const char *pathname, const char *entry) return NULL; } - if (-1 == file_map (pathname, &buf, &lenght)) + dynbuf = new_dyn_buf_mmap_file (pathname); + if (NULL == dynbuf) { DBG_MSG ("Could not open config file for reading!\n"); return NULL; } - while (current < lenght) + while (NULL != (buf = read_line_dyn_buf (dynbuf))) { - count = buf_get_line (buf, lenght, current); - - tmp_buf = xstrndup (&buf[current], count); - if (NULL == tmp_buf) - goto error; - - tmp_p = tmp_buf; + str_ptr = buf; /* Strip leading spaces/tabs */ - while ((tmp_p[0] == ' ') || (tmp_p[0] == '\t')) - tmp_p++; + while ((str_ptr[0] == ' ') || (str_ptr[0] == '\t')) + str_ptr++; /* Get entry and value */ - token = strsep (&tmp_p, "="); + token = strsep (&str_ptr, "="); /* Bogus entry or value */ if (NULL == token) goto _continue; @@ -511,7 +505,7 @@ get_cnf_entry (const char *pathname, const char *entry) do { /* Bash variables are usually quoted */ - token = strsep (&tmp_p, "\"\'"); + token = strsep (&str_ptr, "\"\'"); /* If quoted, the first match will be "" */ } while ((NULL != token) && (0 == strlen (token))); @@ -537,7 +531,12 @@ get_cnf_entry (const char *pathname, const char *entry) value = xstrndup (token, strlen (token)); if (NULL == value) - goto error; + { + free_dyn_buf (dynbuf); + free (buf); + + return NULL; + } /* We do not break, as there might be more than one entry * defined, and as bash uses the last, so should we */ @@ -545,34 +544,27 @@ get_cnf_entry (const char *pathname, const char *entry) } _continue: - current += count + 1; - free (tmp_buf); - /* Set to NULL in case we error out above and have - * to free below */ - tmp_buf = NULL; + free (buf); + } + + /* read_line_dyn_buf() returned NULL with errno set */ + if ((NULL == buf) && (0 != errno)) + { + DBG_MSG ("Failed to read line from dynamic buffer!\n"); + free_dyn_buf (dynbuf); + if (NULL != value) + free (value); + + return NULL; } if (NULL == value) DBG_MSG ("Failed to get value for config entry '%s'!\n", entry); - file_unmap (buf, lenght); + free_dyn_buf (dynbuf); return value; - -error: - free (tmp_buf); - free (value); - - if (NULL != buf) - { - save_errno (); - file_unmap (buf, lenght); - /* unmmap() might have changed it */ - restore_errno (); - } - - return NULL; } @@ -614,7 +606,7 @@ file_map (const char *filename, char **buf, size_t * bufsize) if (fstat (fd, &stats) < 0) { DBG_MSG ("Failed to stat file!\n"); - + save_errno (); close (fd); restore_errno (); @@ -626,7 +618,7 @@ file_map (const char *filename, char **buf, size_t * bufsize) { errno = EINVAL; DBG_MSG ("Failed to mmap file with 0 size!\n"); - + save_errno (); close (fd); restore_errno (); @@ -638,7 +630,7 @@ file_map (const char *filename, char **buf, size_t * bufsize) if (*buf == MAP_FAILED) { DBG_MSG ("Failed to mmap file!\n"); - + save_errno (); close (fd); restore_errno (); diff --git a/src/core/librcscripts/parse.c b/src/core/librcscripts/parse.c index caeba5a..2711c3b 100644 --- a/src/core/librcscripts/parse.c +++ b/src/core/librcscripts/parse.c @@ -53,24 +53,21 @@ #define PARSE_BUFFER_SIZE 256 -static size_t parse_rcscript (char *scriptname, dyn_buf_t * data); +static size_t parse_rcscript (char *scriptname, dyn_buf_t *data); -static size_t parse_print_start (dyn_buf_t * data); -static size_t parse_print_header (char *scriptname, dyn_buf_t * data); -static size_t parse_print_body (char *scriptname, dyn_buf_t * data); +static size_t parse_print_start (dyn_buf_t *data); +static size_t parse_print_header (char *scriptname, dyn_buf_t *data); +static size_t parse_print_body (char *scriptname, dyn_buf_t *data); /* Return count on success, -1 on error. If it was critical, errno will be set. */ size_t -parse_rcscript (char *scriptname, dyn_buf_t * data) +parse_rcscript (char *scriptname, dyn_buf_t *data) { regex_data_t tmp_data; + dyn_buf_t *dynbuf = NULL; char *buf = NULL; - char *tmp_buf = NULL; size_t write_count = 0; size_t tmp_count; - size_t lenght; - int count; - int current = 0; if (!check_arg_dyn_buf (data)) return -1; @@ -78,43 +75,35 @@ parse_rcscript (char *scriptname, dyn_buf_t * data) if (!check_arg_str (scriptname)) return -1; - if (-1 == file_map (scriptname, &buf, &lenght)) + dynbuf = new_dyn_buf_mmap_file (scriptname); + if (NULL == dynbuf) { DBG_MSG ("Could not open '%s' for reading!\n", gbasename (scriptname)); return -1; } - while (current < lenght) - { - count = buf_get_line (buf, lenght, current); - - tmp_buf = xstrndup (&(buf[current]), count); - if (NULL == tmp_buf) - goto error; - - if (0 == current) - { - DBG_MSG ("Parsing '%s'.\n", gbasename (scriptname)); - - tmp_count = parse_print_header (gbasename (scriptname), data); - if (-1 == tmp_count) - { - DBG_MSG ("Failed to call parse_print_header()!\n"); - goto error; - } - - write_count += tmp_count; + DBG_MSG ("Parsing '%s'.\n", gbasename (scriptname)); - goto _continue; - } + tmp_count = parse_print_header (gbasename (scriptname), data); + if (-1 == tmp_count) + { + DBG_MSG ("Failed to call parse_print_header()!\n"); + goto error; + } + write_count += tmp_count; + while (NULL != (buf = read_line_dyn_buf(dynbuf))) + { /* Check for lines with comments, and skip them */ - DO_REGEX (tmp_data, tmp_buf, "^[ \t]*#", error); + DO_REGEX (tmp_data, buf, "^[ \t]*#", error); if (REGEX_MATCH (tmp_data)) - goto _continue; + { + free (buf); + continue; + } - /* If the line contains 'depend()', set 'got_depend' */ - DO_REGEX (tmp_data, tmp_buf, "depend[ \t]*()[ \t]*{?", error); + /* If the line contains 'depend()', call parse_print_body () and break */ + DO_REGEX (tmp_data, buf, "depend[ \t]*()[ \t]*{?", error); if (REGEX_MATCH (tmp_data)) { DBG_MSG ("Got 'depend()' function.\n"); @@ -128,36 +117,39 @@ parse_rcscript (char *scriptname, dyn_buf_t * data) write_count += tmp_count; - /* Make sure this is the last loop */ - current += lenght; - goto _continue; + /* This is the last loop */ + free (buf); + break; } -_continue: - current += count + 1; - free (tmp_buf); + free (buf); + } + + /* read_line_dyn_buf() returned NULL with errno set */ + if ((NULL == buf) && (0 != errno)) + { + DBG_MSG ("Failed to read line from dynamic buffer!\n"); + free_dyn_buf (dynbuf); + + return -1; } - file_unmap (buf, lenght); + free_dyn_buf (dynbuf); return write_count; error: - free (tmp_buf); if (NULL != buf) - { - save_errno (); - file_unmap (buf, lenght); - /* file_unmap() might have changed it */ - restore_errno (); - } + free (buf); + if (NULL != dynbuf) + free_dyn_buf (dynbuf); return -1; } size_t -generate_stage1 (dyn_buf_t * data) +generate_stage1 (dyn_buf_t *data) { rcscript_info_t *info; size_t write_count = 0; @@ -202,7 +194,7 @@ sig_handler (int signum) /* Returns data's lenght on success, else -1 on error. */ size_t -generate_stage2 (dyn_buf_t * data) +generate_stage2 (dyn_buf_t *data) { int pipe_fds[2][2] = { {0, 0}, {0, 0} }; pid_t child_pid; @@ -315,7 +307,7 @@ generate_stage2 (dyn_buf_t * data) #if 0 int tmp_fd = open ("bar", O_CREAT | O_TRUNC | O_RDWR, 0600); - write (tmp_fd, stage1_data->data, stage1_write_count); + write (tmp_fd, stage1_data->data, stage1_data->wr_index); close (tmp_fd); #endif @@ -433,7 +425,7 @@ failed: /* FIXME: better errno ? */ errno = ECANCELED; DBG_MSG ("Bash failed with status 0x%x!\n", status); - + return -1; } } @@ -552,14 +544,14 @@ write_legacy_stage3 (FILE * output) } int -parse_cache (const dyn_buf_t * data) +parse_cache (const dyn_buf_t *data) { service_info_t *info; service_type_t type = ALL_SERVICE_TYPE_T; rcscript_info_t *rs_info; - char *tmp_buf = NULL; + char *buf = NULL; char *rc_name = NULL; - char *tmp_p; + char *str_ptr; char *token; char *field; int retval; @@ -567,43 +559,44 @@ parse_cache (const dyn_buf_t * data) if (!check_arg_dyn_buf ((dyn_buf_t *) data)) goto error; - while (NULL != (tmp_buf = read_line_dyn_buf ((dyn_buf_t *) data))) + while (NULL != (buf = read_line_dyn_buf ((dyn_buf_t *) data))) { - tmp_p = tmp_buf; + str_ptr = buf; /* Strip leading spaces/tabs */ - while ((tmp_p[0] == ' ') || (tmp_p[0] == '\t')) - tmp_p++; + while ((str_ptr[0] == ' ') || (str_ptr[0] == '\t')) + str_ptr++; /* Get FIELD name and FIELD value */ - token = strsep (&tmp_p, " "); + token = strsep (&str_ptr, " "); /* FIELD name empty/bogus? */ if ((!check_str (token)) /* We got an empty FIELD value */ - || (!check_str (tmp_p))) + || (!check_str (str_ptr))) { errno = EMSGSIZE; DBG_MSG ("Parsing stopped due to short read!\n"); + goto error; } if (0 == strcmp (token, FIELD_RCSCRIPT)) { - DBG_MSG ("Field = '%s', value = '%s'\n", token, tmp_p); + DBG_MSG ("Field = '%s', value = '%s'\n", token, str_ptr); /* Add the service to the list, and initialize all data */ - retval = service_add (tmp_p); + retval = service_add (str_ptr); if (-1 == retval) { - DBG_MSG ("Failed to add %s to service list!\n", tmp_p); + DBG_MSG ("Failed to add %s to service list!\n", str_ptr); goto error; } - info = service_get_info (tmp_p); + info = service_get_info (str_ptr); if (NULL == info) { - DBG_MSG ("Failed to get info for '%s'!\n", tmp_p); + DBG_MSG ("Failed to get info for '%s'!\n", str_ptr); goto error; } /* Save the rc-script name for next passes of loop */ @@ -643,7 +636,7 @@ parse_cache (const dyn_buf_t * data) * As the values are passed to a bash function, and we * then use 'echo $*' to parse them, they should only * have one space between each value ... */ - token = strsep (&tmp_p, " "); + token = strsep (&str_ptr, " "); /* Get the correct type name */ field = service_type_names[type]; @@ -663,7 +656,7 @@ parse_cache (const dyn_buf_t * data) } /* Get the next value (if any) */ - token = strsep (&tmp_p, " "); + token = strsep (&str_ptr, " "); } goto _continue; @@ -674,16 +667,16 @@ parse_cache (const dyn_buf_t * data) _continue: type = ALL_SERVICE_TYPE_T; - free (tmp_buf); + free (buf); /* Do not free 'rc_name', as it should be consistant * across loops */ } /* read_line_dyn_buf() returned NULL with errno set */ - if (0 != errno) + if ((NULL == buf) && (0 != errno)) { DBG_MSG ("Failed to read line from dynamic buffer!\n"); - goto error; + return -1; } /* Set the mtimes @@ -705,19 +698,19 @@ _continue: return 0; error: - free (tmp_buf); + free (buf); return -1; } size_t -parse_print_start (dyn_buf_t * data) +parse_print_start (dyn_buf_t *data) { size_t write_count; if (!check_arg_dyn_buf (data)) return -1; - + write_count = sprintf_dyn_buf (data, ". /sbin/functions.sh\n" @@ -730,13 +723,13 @@ parse_print_start (dyn_buf_t * data) } size_t -parse_print_header (char *scriptname, dyn_buf_t * data) +parse_print_header (char *scriptname, dyn_buf_t *data) { size_t write_count; if (!check_arg_dyn_buf (data)) return -1; - + write_count = sprintf_dyn_buf (data, "#*** %s ***\n" @@ -749,19 +742,19 @@ parse_print_header (char *scriptname, dyn_buf_t * data) } size_t -parse_print_body (char *scriptname, dyn_buf_t * data) +parse_print_body (char *scriptname, dyn_buf_t *data) { size_t write_count; - char *tmp_buf = NULL; - char *tmp_ptr; + char *buf = NULL; + char *str_ptr; char *base; char *ext; if (!check_arg_dyn_buf (data)) return -1; - - tmp_buf = xstrndup (scriptname, strlen (scriptname)); - if (NULL == tmp_buf) + + buf = xstrndup (scriptname, strlen (scriptname)); + if (NULL == buf) return -1; /* @@ -770,21 +763,21 @@ parse_print_body (char *scriptname, dyn_buf_t * data) */ /* bash: base="${myservice%%.*}" */ - base = tmp_buf; - tmp_ptr = strchr (tmp_buf, '.'); - if (NULL != tmp_ptr) + base = buf; + str_ptr = strchr (buf, '.'); + if (NULL != str_ptr) { - tmp_ptr[0] = '\0'; - tmp_ptr++; + str_ptr[0] = '\0'; + str_ptr++; } else { - tmp_ptr = tmp_buf; + str_ptr = buf; } /* bash: ext="${myservice##*.}" */ - ext = strrchr (tmp_ptr, '.'); + ext = strrchr (str_ptr, '.'); if (NULL == ext) - ext = tmp_ptr; + ext = str_ptr; write_count = sprintf_dyn_buf (data, diff --git a/src/core/librcscripts/runlevels.c b/src/core/librcscripts/runlevels.c index b2d8442..914f7ab 100644 --- a/src/core/librcscripts/runlevels.c +++ b/src/core/librcscripts/runlevels.c @@ -29,7 +29,7 @@ #include "rcscripts.h" -static char ** get_runlevel_dirs (void); +static char **get_runlevel_dirs (void); LIST_HEAD (runlevel_list); diff --git a/src/core/librcscripts/scripts.c b/src/core/librcscripts/scripts.c index 88024f9..040bd8f 100644 --- a/src/core/librcscripts/scripts.c +++ b/src/core/librcscripts/scripts.c @@ -45,6 +45,7 @@ get_rcscripts (void) { errno = ENOENT; DBG_MSG ("'%s' is empty!\n", RCSCRIPTS_INITDDIR); + return -1; } @@ -62,39 +63,36 @@ get_rcscripts (void) else { regex_data_t tmp_data; + dyn_buf_t *dynbuf = NULL; char *buf = NULL; - char *tmp_buf = NULL; - size_t lenght; - int buf_count; - int current = 0; - if (-1 == file_map (rcscript, &buf, &lenght)) + dynbuf = new_dyn_buf_mmap_file (rcscript); + if (NULL == dynbuf) { DBG_MSG ("Could not open '%s' for reading!\n", gbasename (rcscript)); goto error; } - buf_count = buf_get_line (buf, lenght, current); - - tmp_buf = xstrndup (&(buf[current]), buf_count); - if (NULL == tmp_buf) + buf = read_line_dyn_buf (dynbuf); + free_dyn_buf (dynbuf); + if ((NULL == buf) && (0 != errno)) + goto error; + if (NULL == buf) { - file_unmap (buf, lenght); - goto error; + DBG_MSG ("'%s' is not a valid rc-script!\n", + gbasename (rcscript)); + continue; } - file_unmap (buf, lenght); - /* Check if it starts with '#!/sbin/runscript' */ - DO_REGEX (tmp_data, tmp_buf, "[ \t]*#![ \t]*/sbin/runscript[ \t]*.*", + DO_REGEX (tmp_data, buf, "[ \t]*#![ \t]*/sbin/runscript[ \t]*.*", check_error); - free (tmp_buf); + free (buf); if (REGEX_FULL_MATCH != tmp_data.match) { DBG_MSG ("'%s' is not a valid rc-script!\n", gbasename (rcscript)); - continue; } @@ -152,7 +150,7 @@ get_rcscripts (void) continue; check_error: - free (tmp_buf); + free (buf); goto error; loop_error: diff --git a/src/core/librcscripts/simple-regex.c b/src/core/librcscripts/simple-regex.c index a1dc97d..fc365de 100644 --- a/src/core/librcscripts/simple-regex.c +++ b/src/core/librcscripts/simple-regex.c @@ -117,7 +117,7 @@ size_t get_word (const char *regex, char **r_word) { char *r_list; - char *tmp_p; + char *str_ptr; size_t count = 0; size_t tmp_count; @@ -128,7 +128,7 @@ get_word (const char *regex, char **r_word) if (NULL == r_word) return 0; - tmp_p = *r_word; + str_ptr = *r_word; while (strlen (regex) > 0) { @@ -138,7 +138,7 @@ get_word (const char *regex, char **r_word) case '+': case '?': /* If its a wildcard, backup one step */ - *(--tmp_p) = '\0'; + *(--str_ptr) = '\0'; count--; return count; case '[': @@ -152,17 +152,17 @@ get_word (const char *regex, char **r_word) /* Bail if we have a list */ if (tmp_count > 0) { - tmp_p[0] = '\0'; + str_ptr[0] = '\0'; return count; } default: - *tmp_p++ = *regex++; + *str_ptr++ = *regex++; count++; break; } } - tmp_p[0] = '\0'; + str_ptr[0] = '\0'; return count; @@ -276,7 +276,7 @@ get_list_size (const char *regex) size_t get_list (const char *regex, char **r_list) { - char *tmp_buf = NULL; + char *buf = NULL; size_t count = 0; size_t size; @@ -301,7 +301,7 @@ get_list (const char *regex, char **r_list) if (NULL == *r_list) return -1; - tmp_buf = *r_list; + buf = *r_list; /* Take care of '[' */ regex++; @@ -315,10 +315,10 @@ get_list (const char *regex, char **r_list) && (strlen (regex) >= 2) && (regex[-1] < regex[1])) { /* Fill in missing chars in sequence */ - while (tmp_buf[-1] < regex[1]) + while (buf[-1] < regex[1]) { - tmp_buf[0] = (char) (tmp_buf[-1] + 1); - tmp_buf++; + buf[0] = (char) (buf[-1] + 1); + buf++; /* We do not increase count */ } /* Take care of '-' and next char */ @@ -327,12 +327,12 @@ get_list (const char *regex, char **r_list) } else { - *tmp_buf++ = *regex++; + *buf++ = *regex++; count++; } } - tmp_buf[0] = '\0'; + buf[0] = '\0'; /* Take care of ']' */ count++; @@ -784,7 +784,7 @@ match (regex_data_t * regex_data) regex_data_t tmp_data; char *data_p = regex_data->data; char *regex_p; - char *tmp_buf = NULL; + char *buf = NULL; int from_start = 0; int to_end = 0; int retval; @@ -792,11 +792,11 @@ match (regex_data_t * regex_data) CHECK_REGEX_DATA_P (regex_data, failed); /* We might be modifying regex_p, so make a copy */ - tmp_buf = xstrndup (regex_data->regex, strlen (regex_data->regex)); - if (NULL == tmp_buf) + buf = xstrndup (regex_data->regex, strlen (regex_data->regex)); + if (NULL == buf) goto error; - regex_p = tmp_buf; + regex_p = buf; /* Should we only match from the start? */ if ('^' == regex_p[0]) @@ -857,13 +857,13 @@ failed: regex_data->r_count = 0; } - free (tmp_buf); + free (buf); return 0; error: regex_data->match = REGEX_NO_MATCH; - free (tmp_buf); + free (buf); return -1; } |