diff options
author | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2008-07-29 15:54:17 -0700 |
---|---|---|
committer | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2008-07-29 15:54:17 -0700 |
commit | 363464a3815cd59dea763f05069e94cdc1107726 (patch) | |
tree | a00bebf1c8e0b506a47148643518dcbae9ac6578 | |
parent | 51dabae40799358785a147b3447b7075e7fae22b (diff) | |
parent | e579a863ade994cedc433cfffcd2d3f32f5739be (diff) | |
download | vyatta-cfg-363464a3815cd59dea763f05069e94cdc1107726.tar.gz vyatta-cfg-363464a3815cd59dea763f05069e94cdc1107726.zip |
Merge branch 'islavista' of speedy:git/vyatta-cfg into islavista
-rw-r--r-- | src/cli_new.c | 120 | ||||
-rw-r--r-- | src/cli_path_utils.c | 1 | ||||
-rw-r--r-- | src/cli_val.h | 41 | ||||
-rw-r--r-- | src/commit.c | 126 | ||||
-rw-r--r-- | src/delete.c | 79 | ||||
-rw-r--r-- | src/set.c | 28 |
6 files changed, 193 insertions, 202 deletions
diff --git a/src/cli_new.c b/src/cli_new.c index d9e1687..83cc150 100644 --- a/src/cli_new.c +++ b/src/cli_new.c @@ -16,6 +16,7 @@ #include <regex.h> #include <errno.h> #include <time.h> +#include <utime.h> #include "cli_objects.h" #include "cli_val_engine.h" @@ -110,7 +111,7 @@ char *cli_operation_name = NULL; printf("echo \"bla-bla-bla%s\";", sptr) note very important ';' as the end of the format */ -void bye(char *msg, ...) +void bye(const char *msg, ...) { va_list ap; @@ -135,7 +136,7 @@ void bye(char *msg, ...) which are executed as eval `command` in order to modify BASH env */ -void print_msg(char *msg, ...) +void print_msg(const char *msg, ...) { va_list ap; @@ -149,21 +150,49 @@ void print_msg(char *msg, ...) va_end(ap); } +int mkdir_p(const char *path) +{ + if (mkdir(path, 0777) == 0) + return 0; + + if (errno != ENOENT) + return -1; + + char *tmp = strdup(path); + if (tmp == NULL) { + errno = ENOMEM; + return -1; + } + + char *slash = strrchr(tmp, '/'); + if (slash == NULL) + return -1; + *slash = '\0'; + + /* recurse to make missing piece of path */ + int ret = mkdir_p(tmp); + if (ret == 0) + ret = mkdir(path, 0777); + + free(tmp); + return ret; +} + void touch_dir(const char *dp) { struct stat statbuf; + if (lstat(dp, &statbuf) < 0) { - char *command; - command = my_malloc(strlen(dp) + 10, "set"); - sprintf(command, "mkdir -p %s", dp); - system(command); - free(command); - return; - } - if ((statbuf.st_mode & S_IFMT) != S_IFDIR) { - bye("directory %s expected, found regular file", dp); + if (errno != ENOENT) + bye("can't access directory: %s (%s)", dp, strerror(errno)); + + if (mkdir_p(dp) < 0) + bye("can't make directory: %s (%s)", dp, strerror(errno)); + } else { + if(!S_ISDIR(statbuf.st_mode)) + bye("directory %s expected, found file", dp); + utime(dp, NULL); } - return; } /***************************************************** @@ -367,7 +396,7 @@ get_config_lock() return ret; } -void internal_error(int line, char *file) +void internal_error(int line, const char *file) { printf("\n\nInternal Error at line %d in %s\n", line, file); exit (-1); @@ -802,7 +831,7 @@ int char2val(vtw_def *def, char *value, valstruct *valp) compare two values per cond returns result of comparison ****************************************************/ -boolean val_cmp(valstruct *left, valstruct *right, vtw_cond_e cond) +boolean val_cmp(const valstruct *left, const valstruct *right, vtw_cond_e cond) { unsigned int left_parts[9], right_parts[9]; vtw_type_e val_type; @@ -2045,35 +2074,7 @@ int cli_val_read(char *buf, int max_size) } return len; } -/*==========================================================*/ -/* MEMORY */ -/*==========================================================*/ - -void *my_malloc(size_t size, const char *name) -{ - return malloc(size); -} -void *my_realloc(void *ptr, size_t size, const char *name) -{ - return realloc(ptr, size); -} - -void my_free(void *ptr) -{ - free(ptr); -} - -/************************************************* - my_strdup: - do a strdup, - exit on no memory -**************************************************/ -char *my_strdup(const char *s, const char *name) -{ - return strdup(s); -} - -void done() +void done(void) { free_reuse_list(); free_path(&t_path); @@ -2094,16 +2095,28 @@ void restore_paths(vtw_mark *markp) pop_path(&t_path); } -void touch() +void touch_file(const char *filename) { - char *command; - command = my_malloc(strlen(get_mdirp()) + 20, "delete"); - sprintf(command, "touch %s/%s", get_mdirp(), MOD_NAME); - system(command); - free(command); + int fd = creat(filename, 0666); + if (fd < 0) + { + if (errno == EEXIST) + utime(filename, NULL); + else + bye("can't touch %s (%s)", filename, strerror(errno)); + } + else + close(fd); +} + +void touch(void) +{ + char filename[strlen(get_mdirp()) + 20]; + sprintf(filename, "%s/%s", get_mdirp(), MOD_NAME); + touch_file(filename); } -char *type_to_name(vtw_type_e type) { +const char *type_to_name(vtw_type_e type) { switch(type) { case INT_TYPE: return("u32"); case IPV4_TYPE: return("ipv4"); @@ -2118,12 +2131,7 @@ char *type_to_name(vtw_type_e type) { } } -#if 1 -void -dump_log(int argc, char **argv) -{ -} -#else +#ifdef CLI_DEBUG void dump_log(int argc, char **argv) { int i; diff --git a/src/cli_path_utils.c b/src/cli_path_utils.c index 642b5ae..4111568 100644 --- a/src/cli_path_utils.c +++ b/src/cli_path_utils.c @@ -55,6 +55,7 @@ #include <string.h> #include "cli_path_utils.h" +#include "cli_val.h" /********************* * Data definitions diff --git a/src/cli_val.h b/src/cli_val.h index c9d59a6..5abaaee 100644 --- a/src/cli_val.h +++ b/src/cli_val.h @@ -156,8 +156,6 @@ extern void push_path(vtw_path *path, char *segm); extern void push_path_no_escape(vtw_path *path, char *segm); extern void free_def(vtw_def *defp); extern void free_sorted(vtw_sorted *sortp); -extern void *my_malloc(size_t size, const char *name); -extern void *my_realloc(void *ptr, size_t size, const char *name); extern vtw_path m_path, t_path; @@ -167,29 +165,33 @@ extern vtw_path m_path, t_path; extern void add_val(valstruct *first, valstruct *second); extern int cli_val_read(char *buf, int max_size); extern vtw_node *make_val_node(valstruct *val); -extern char *my_strdup(const char *s, const char *name); extern valstruct str2val(char *cp); extern void dump_tree(vtw_node *node, int lev); extern void dump_def(vtw_def *defp); -extern boolean val_cmp(valstruct *left, valstruct *right, vtw_cond_e cond); -extern void out_of_memory(void); +extern boolean val_cmp(const valstruct *left, const valstruct *right, + vtw_cond_e cond); +extern void out_of_memory(void) __attribute__((noreturn)); extern void subtract_values(char **lhs, const char *rhs); extern boolean validate_value(vtw_def *def, char *value); -extern void internal_error(int line, char *file); +extern void internal_error(int line, const char *file) + __attribute__((noreturn)); extern void done(void); extern void del_value(vtw_def *defp, char *cp); -extern void bye(char *msg, ...); -extern void print_msg(char *msg, ...); +extern void bye(const char *msg, ...) + __attribute__((format(printf, 1, 2), noreturn)); +extern void print_msg(const char *msg, ...) + __attribute__((format(printf, 1, 2))); extern void switch_path(first_seg *seg); extern void vtw_sort(valstruct *valp, vtw_sorted *sortp); extern void free_val(valstruct *val); -extern void my_free(void *ptr); extern void touch(void); -extern void dump_log(int argc, char **argv); -extern char *type_to_name(vtw_type_e type); +extern int mkdir_p(const char *path); + +extern const char *type_to_name(vtw_type_e type); extern boolean execute_list(vtw_node *cur, vtw_def *def); extern void touch_dir(const char *dp); +extern void touch_file(const char *name); extern void copy_path(vtw_path *to, vtw_path *from); extern void free_path(vtw_path *path); @@ -197,7 +199,7 @@ extern void free_path(vtw_path *path); void mark_paths(vtw_mark *markp); void restore_paths(vtw_mark *markp); -extern int get_config_lock(); +extern int get_config_lock(void); #define VTWERR_BADPATH -2 #define VTWERR_OK 0 @@ -217,14 +219,21 @@ extern int out_fd; extern FILE *out_stream; extern FILE *err_stream; -extern int initialize_output(); +extern int initialize_output(void); + +/* debug hooks? */ +#define my_malloc(size, name) malloc(size) +#define my_realloc(ptr, size, name) realloc(ptr, size) +#define my_strdup(str, name) strdup(str) +#define my_free(ptr) free(ptr) /*** debug ***/ #undef CLI_DEBUG #ifdef CLI_DEBUG -#define DPRINT(...) printf(__VA_ARGS__) +#define DPRINT(fmt, arg...) printf(fmt, #arg) +extern void dump_log(int argc, char **argv); #else -#define DPRINT(...) +#define DPRINT(fmt, arg...) while (0) { printf(fmt, ##arg); } +#define dump_log(argc, argv) #endif - #endif diff --git a/src/commit.c b/src/commit.c index 3b4d9d3..ee159b7 100644 --- a/src/commit.c +++ b/src/commit.c @@ -22,14 +22,14 @@ struct DirIndex { }; struct DirSort { - char name[255]; unsigned long priority; + char name[255]; }; -static char def_name[] = DEF_NAME; -static char tag_name[] = TAG_NAME; -static char opaque_name[] = OPQ_NAME; +static const char def_name[] = DEF_NAME; +static const char tag_name[] = TAG_NAME; +static const char opaque_name[] = OPQ_NAME; static int fin_commit(boolean ok); static boolean commit_value(vtw_def *defp, char *cp, @@ -51,17 +51,12 @@ static void make_dir() { struct stat statbuf; if (lstat(m_path.path, &statbuf) < 0) { - char *command; - command = my_malloc(strlen(m_path.path) + 10, "set"); - sprintf(command, "mkdir -p %s", m_path.path); - system(command); - free(command); + mkdir_p(m_path.path); return; } - if ((statbuf.st_mode & S_IFMT) != S_IFDIR) { - bye("directory %s expected, found regular file", m_path.path); + if (!S_ISDIR(statbuf.st_mode)) { + bye("directory %s expected, found file", m_path.path); } - return; } #endif @@ -111,18 +106,18 @@ init_next_filtered_dirname(DIR *dp, int sort_order, int exclude_wh) strcpy(d->name,dirp->d_name); d->priority = (unsigned long)0; vtw_def def; - char *path; - path = malloc(strlen(t_path.path)+strlen(dirp->d_name)+2+8+1); - sprintf(path,"%s/%s/node.def",t_path.path,dirp->d_name); + + char path[strlen(t_path.path)+strlen(dirp->d_name)+2+8+1]; + sprintf(path,"%s/%s/node.def", t_path.path, dirp->d_name); + struct stat s; - if ((lstat(path,&s) >= 0) && - ((s.st_mode & S_IFMT) == S_IFREG)) { + if ((lstat(path,&s) == 0) && S_ISREG(s.st_mode)) { memset(&def, 0, sizeof(def)); if (parse_def(&def,path,FALSE) == 0) { d->priority = def.def_priority; } } - free(path); + di->dirname[di->dirname_ct++] = d; if (di->dirname_ct % 1024 == 0) { di->dirname = realloc(di->dirname, (di->dirname_ct+1024)*sizeof(char*)); @@ -380,9 +375,7 @@ int main(int argc, char **argv) { boolean status; - char *mod; struct stat statbuf; - int st; boolean update_pending = FALSE; cli_operation_name = "Commit"; @@ -394,11 +387,10 @@ int main(int argc, char **argv) set_in_commit(TRUE); dump_log( argc, argv); init_paths(TRUE); - mod = my_malloc(strlen(get_mdirp()) + strlen(MOD_NAME)+2, "COMMIT"); + + char mod[strlen(get_mdirp()) + sizeof(MOD_NAME)+2]; sprintf(mod, "%s/%s", get_mdirp(), MOD_NAME); - st = lstat(mod, &statbuf); - my_free(mod); - if (st < 0 ) { + if (lstat(mod, &statbuf) < 0) { fprintf(out_stream, "No configuration changes to commit\n"); return 0; } @@ -430,17 +422,14 @@ int main(int argc, char **argv) static void perform_create_node() { #if BITWISE - static char format[]="rm -f -r %s;mkdir %s"; - char *command; + static const char format[]="rm -f -r %s;mkdir %s"; + char command[2 * strlen(m_path.path) + sizeof(format)]; + switch_path(APATH); - command = my_malloc(2 * strlen(m_path.path) + sizeof(format), - "commit_create"); sprintf(command, format, m_path.path, m_path.path); system(command); - my_free(command); switch_path(CPATH); #endif - return; } @@ -451,32 +440,27 @@ static void perform_create_node() static void perform_delete_node() { #if BITWISE - static char format[]="rm -f -r %s"; - char *command; - command = my_malloc(strlen(m_path.path) + sizeof(format), - "commit_delete"); + static const char format[]="rm -f -r %s"; + char command[strlen(m_path.path) + sizeof(format)]; sprintf(command, format, m_path.path); system(command); - my_free(command); #endif - return; } + static void perform_move() { #if BITWISE - static char format[] = "rm -r -f %s;mkdir %s;mv %s/" VAL_NAME " %s"; + static const char format[] = "rm -r -f %s;mkdir %s;mv %s/" VAL_NAME " %s"; char *a_path; - char *command; + char command[sizeof(format)+3*strlen(a_path)+strlen(m_path.path)]; + switch_path(APATH); a_path = my_strdup(m_path.path, ""); switch_path(CPATH); - command = my_malloc(sizeof(format)+3*strlen(a_path)+strlen(m_path.path),""); sprintf(command, format, a_path, a_path, m_path.path, a_path); system(command); - my_free(command); my_free(a_path); #endif - return; } /************************************************* @@ -753,26 +737,26 @@ boolean commit_update_child(vtw_def *pdefp, char *child, switch_path(MPATH); len = sizeof(format1) + 2 * strlen(get_tmpp()) + strlen(m_path.path) + strlen(child); - command = my_malloc(len, ""); + command = malloc(len); sprintf(command, format1, get_tmpp(), m_path.path, child, get_tmpp()); system(command); - my_free(command); + switch_path(APATH); len = sizeof(format2) + 2 *strlen(m_path.path) + 2 * strlen( child) + strlen(get_tmpp()); - command = my_malloc(len, ""); + command = realloc(command, len); sprintf(command, format2, m_path.path, child, get_tmpp(), child, m_path.path); system(command); - my_free(command); + switch_path(CPATH); len = sizeof(format3) + strlen(m_path.path) + strlen( child); - command = my_malloc(len, ""); + command = realloc(command, len); sprintf(command, format3, m_path.path, child); system(command); - my_free(command); + free(command); } #endif restore_paths(&mark); @@ -1530,14 +1514,14 @@ static boolean commit_value(vtw_def *defp, char *cp, static int fin_commit(boolean ok) { char *command; - static char format1[]="cp -r -f %s/* %s"; /*mdirp, tmpp*/ - static char format2[]="sudo umount %s"; /*mdirp*/ - static char format3[]="rm -f %s/" MOD_NAME " >&/dev/null ; /bin/true"; + static const char format1[]="cp -r -f %s/* %s"; /*mdirp, tmpp*/ + static const char format2[]="sudo umount %s"; /*mdirp*/ + static const char format3[]="rm -f %s/" MOD_NAME " >&/dev/null ; /bin/true"; /*tmpp*/ - static char format4[]="rm -rf %s/{.*,*} >&/dev/null ; /bin/true"; /*cdirp*/ - static char format5[]="rm -rf %s/{.*,*} >&/dev/null ; /bin/true"; /*adirp*/ - static char format6[]="mv -f %s/* -t %s";/*tmpp, adirp*/ - static char format7[]="sudo mount -t $UNIONFS -o dirs=%s=rw:%s=ro" + static const char format4[]="rm -rf %s/{.*,*} >&/dev/null ; /bin/true"; /*cdirp*/ + static const char format5[]="rm -rf %s/{.*,*} >&/dev/null ; /bin/true"; /*adirp*/ + static const char format6[]="mv -f %s/* -t %s";/*tmpp, adirp*/ + static const char format7[]="sudo mount -t $UNIONFS -o dirs=%s=rw:%s=ro" " $UNIONFS %s"; /*cdirp, adirp, mdirp*/ int m_len = strlen(get_mdirp()); int t_len = strlen(get_tmpp()); @@ -1548,40 +1532,34 @@ static int fin_commit(boolean ok) fprintf(out_stream, "Commit failed\n"); return -1; } - command = my_malloc(strlen(format1) + m_len + t_len, ""); + command = malloc(strlen(format1) + m_len + t_len); sprintf(command, format1, get_mdirp(), get_tmpp()); system(command); - my_free(command); - command = my_malloc(strlen(format2) + m_len, ""); - sprintf(command, format2, get_mdirp()); + command = realloc(command, strlen(format2) + m_len); + sprintf(command, format2, get_mdirp()); system(command); - my_free(command); - command = my_malloc(strlen(format3) + t_len, ""); - sprintf(command, format3, get_tmpp()); + command = realloc(command, strlen(format3) + t_len); + sprintf(command, format3, get_tmpp()); system(command); - my_free(command); - command = my_malloc(strlen(format4) + c_len, ""); - sprintf(command, format4, get_cdirp()); + command = realloc(command, strlen(format4) + c_len); + sprintf(command, format4, get_cdirp()); system(command); - my_free(command); - command = my_malloc(strlen(format5) + a_len, ""); - sprintf(command, format5, get_adirp()); + command = realloc(command, strlen(format5) + a_len); + sprintf(command, format5, get_adirp()); system(command); - my_free(command); - command = my_malloc(strlen(format6) + t_len + a_len, ""); - sprintf(command, format6, get_tmpp(), get_adirp()); + command = realloc(command, strlen(format6) + t_len + a_len); + sprintf(command, format6, get_tmpp(), get_adirp()); system(command); - my_free(command); - command = my_malloc(strlen(format7) + c_len + a_len + m_len, ""); - sprintf(command, format7, get_cdirp(), get_adirp(), get_mdirp()); + command = realloc(command, strlen(format7) + c_len + a_len + m_len); + sprintf(command, format7, get_cdirp(), get_adirp(), get_mdirp()); system(command); - my_free(command); + free(command); /* notify other users in config mode */ system("/opt/vyatta/sbin/vyatta-cfg-notify"); diff --git a/src/delete.c b/src/delete.c index bb2c987..004986d 100644 --- a/src/delete.c +++ b/src/delete.c @@ -12,35 +12,35 @@ extern char *cli_operation_name; static void remove_rf(boolean do_umount) { - char *command; - touch(); - if (do_umount) { - command = my_malloc(strlen(get_mdirp()) + 20, "delete"); - sprintf(command, "sudo umount %s", get_mdirp()); - system(command); - free(command); - } - command = my_malloc(strlen(m_path.path) + 10, "delete"); - sprintf(command, "rm -rf %s", m_path.path); + char *command = NULL; + touch(); + if (do_umount) { + command = malloc(strlen(get_mdirp()) + 20); + sprintf(command, "sudo umount %s", get_mdirp()); system(command); - free(command); - if (do_umount) { - command = my_malloc(strlen(get_mdirp()) + strlen(get_cdirp()) + - strlen(get_mdirp()) + 100, - "delete"); - sprintf(command, "sudo mount -t $UNIONFS -o dirs=%s=rw:%s=ro" - " $UNIONFS %s", get_cdirp(), get_adirp(), get_mdirp()); - system(command); - free(command); - } + } + + command = realloc(command, strlen(m_path.path) + 10); + sprintf(command, "rm -rf %s", m_path.path); + system(command); + + if (do_umount) { + command = realloc(command, strlen(get_mdirp()) + strlen(get_cdirp()) + + strlen(get_mdirp()) + 100); + sprintf(command, "sudo mount -t $UNIONFS -o dirs=%s=rw:%s=ro" + " $UNIONFS %s", get_cdirp(), get_adirp(), get_mdirp()); + system(command); + } + free(command); } + static boolean has_default(char **def, int size) { - char *buf; - buf = malloc(1025); char *buf_ptr; FILE *fp = fopen(t_path.path, "r"); + if (fp) { + char buf[1025]; while (fgets(buf, 1024, fp)) { if (strncmp(buf, "default:", 8) == 0) { buf_ptr = index(buf,':'); @@ -53,33 +53,32 @@ static boolean has_default(char **def, int size) } memcpy(*def, buf_ptr, strlen(buf_ptr)-1); fclose(fp); - free(buf); return 0; } } fclose(fp); } - free(buf); + return 1; } -static void reset_default(char *def_val) + +static void reset_default(const char *def_val) { - char *command,*def_cmd; - boolean has_default = 1; - if (def_val == NULL) { + if (def_val == NULL) return; - } - if (has_default) { - touch(); - command = my_malloc(strlen(m_path.path) + 100, "set"); - sprintf(command, "echo %s > %s/node.val", def_val, m_path.path); - system(command); - def_cmd = malloc(strlen(m_path.path) + 12); - sprintf(def_cmd, "touch %s/def",m_path.path); - system(def_cmd); - free(command); - free(def_cmd); - } + + char filename[strlen(m_path.path) + 10]; + touch(); + sprintf(filename, "%s/node.val", m_path.path); + + FILE *fp = fopen(filename, "w"); + if (fp == NULL) + bye("can not open: %s", filename); + fputs(def_val, fp); + fclose(fp); + + sprintf(filename, "%s/def", m_path.path); + touch_file(filename); } /*************************************************** set_validate: @@ -12,12 +12,11 @@ extern char *cli_operation_name; -static void make_dir(void); static void handle_defaults(void); static void make_dir() { - touch_dir(m_path.path); + mkdir_p(m_path.path); } /*************************************************** set_validate: @@ -107,7 +106,6 @@ int main(int argc, char **argv) FILE *fp; boolean res; char *cp; - char *command; boolean need_mod = FALSE, not_new = FALSE; boolean empty_val = FALSE; @@ -248,11 +246,9 @@ int main(int argc, char **argv) if (!def.multi) { - char *path; - path= malloc(strlen(m_path.path)+5); + char path[strlen(m_path.path)+5]; sprintf(path, "%s/def",m_path.path); unlink(path); - free(path); } push_path(&m_path, VAL_NAME); @@ -276,9 +272,9 @@ int main(int argc, char **argv) bye("Error writing file %s", m_path.path); if (need_mod) { pop_path(&m_path); /* get rid of "value" */ - command = my_malloc(strlen(m_path.path) + 30, "set"); - sprintf(command, "touch %s/" MOD_NAME, m_path.path); - system(command); + char filename[strlen(m_path.path) + sizeof(MOD_NAME)+1]; + sprintf(filename, "%s/" MOD_NAME, m_path.path); + touch_file(filename); } return 0; } @@ -359,13 +355,13 @@ handle_default(vtw_path *mpath, vtw_path *tpath, char *exclude) touch_dir(mpath->path); /* make sure directory exist */ //create def marker - char *def_file; - def_file = malloc(strlen(mpath->path)+22); - sprintf(def_file,"touch %s/def",mpath->path); - system(def_file); - sprintf(def_file,"echo 'empty' > %s/def",mpath->path); - system(def_file); - free(def_file); + char def_file[strlen(mpath->path)+8]; + sprintf(def_file,"%s/def",mpath->path); + fp = fopen(def_file, "w"); + if (fp == NULL) + bye("Can not open def file %s", def_file); + fputs("empty\n", fp); + fclose(fp); push_path(mpath, VAL_NAME); fp = fopen(mpath->path, "w"); |