summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAn-Cheng Huang <ancheng@vyatta.com>2007-10-29 15:55:58 -0700
committerAn-Cheng Huang <ancheng@vyatta.com>2007-10-29 15:55:58 -0700
commit386653f4d9af2cbb1124aaba76ce2873e94cfd1f (patch)
treef56f0224f8b23992bba0038fbf59eea22980726f /src
parent650699ea3d06e36390bd06a848ce493827c02e1e (diff)
downloadvyatta-cfg-386653f4d9af2cbb1124aaba76ce2873e94cfd1f.tar.gz
vyatta-cfg-386653f4d9af2cbb1124aaba76ce2873e94cfd1f.zip
redirect output by default
Diffstat (limited to 'src')
-rw-r--r--src/cli_new.c93
-rw-r--r--src/cli_val.h9
-rw-r--r--src/commit.c6
-rw-r--r--src/delete.c4
-rw-r--r--src/set.c4
5 files changed, 109 insertions, 7 deletions
diff --git a/src/cli_new.c b/src/cli_new.c
index 2f2801d..abc87db 100644
--- a/src/cli_new.c
+++ b/src/cli_new.c
@@ -884,6 +884,8 @@ static int change_var_value(const char* var_reference,const char* value, int act
return ret;
}
+int system_out(const char *command);
+
/****************************************************
check_syn:
evaluate syntax tree;
@@ -908,10 +910,8 @@ static boolean check_syn_func(vtw_node *cur,const char* func,int line)
ret = check_syn(cur->vtw_node_left);
if (ret <= 0){
if (expand_string(cur->vtw_node_right->vtw_node_string) == VTWERR_OK) {
- fprintf(stderr, exe_string);
- fprintf(stderr, "\n");
- printf(exe_string);
- printf("\n");
+ fprintf(out_stream, exe_string);
+ fprintf(out_stream, "\n");
}
}
return ret;
@@ -967,7 +967,7 @@ static boolean check_syn_func(vtw_node *cur,const char* func,int line)
set_at_string(save_at);
return FALSE;
}
- ret = system(exe_string);
+ ret = system_out(exe_string);
if (ret) {
set_at_string(save_at);
return FALSE;
@@ -981,7 +981,7 @@ static boolean check_syn_func(vtw_node *cur,const char* func,int line)
if (status != VTWERR_OK) {
return FALSE;
}
- ret = system(exe_string);
+ ret = system_out(exe_string);
return !ret;
case PATTERN_OP: /* left to var, right to pattern */
@@ -1935,4 +1935,85 @@ static int set_reference_environment(const char* var_reference,
}
}
+/*** output ***/
+
+int out_fd = -1;
+FILE *out_stream = NULL;
+
+int err_fd = -1;
+FILE *err_stream = NULL;
+int new_out_fd = -1;
+int new_err_fd = -1;
+
+int
+initialize_output()
+{
+ if ((out_fd = dup(STDOUT_FILENO)) == -1) {
+ return -1;
+ }
+ if ((out_stream = fdopen(out_fd, "a")) == NULL) {
+ return -1;
+ }
+ if ((err_fd = dup(STDOUT_FILENO)) == -1) {
+ return -1;
+ }
+ if ((err_stream = fdopen(err_fd, "a")) == NULL) {
+ return -1;
+ }
+
+ new_out_fd = open(LOGFILE_STDOUT, O_WRONLY | O_CREAT, 0660);
+ new_err_fd = open(LOGFILE_STDERR, O_WRONLY | O_CREAT, 0660);
+ if (new_out_fd == -1 || new_err_fd == -1) {
+ return -1;
+ }
+ if ((lseek(new_out_fd, 0, SEEK_END) == ((off_t) -1))
+ || (lseek(new_err_fd, 0, SEEK_END) == ((off_t) -1))) {
+ return -1;
+ }
+ if ((dup2(new_out_fd, STDOUT_FILENO) == -1)
+ || (dup2(new_err_fd, STDERR_FILENO) == -1)) {
+ return -1;
+ }
+
+ return 0;
+}
+
+int
+redirect_output()
+{
+ if ((dup2(new_out_fd, STDOUT_FILENO) == -1)
+ || (dup2(new_err_fd, STDERR_FILENO) == -1)) {
+ return -1;
+ }
+ return 0;
+}
+
+int
+restore_output()
+{
+ if ((dup2(out_fd, STDOUT_FILENO) == -1)
+ || (dup2(err_fd, STDERR_FILENO) == -1)) {
+ return -1;
+ }
+ return 0;
+}
+
+/* system_out:
+ * call system() with output re-enabled.
+ * output is again redirected before returning from here.
+ */
+int
+system_out(const char *command)
+{
+ int ret = -1;
+ if (restore_output() == -1) {
+ return -1;
+ }
+ ret = system(command);
+ if (redirect_output() == -1) {
+ return -1;
+ }
+ return ret;
+}
+
/**********************************************************/
diff --git a/src/cli_val.h b/src/cli_val.h
index 8a98a88..6ccfde2 100644
--- a/src/cli_val.h
+++ b/src/cli_val.h
@@ -203,4 +203,13 @@ extern boolean get_config_lock(const char* adirp, const char* lock_name);
#define INTERNAL internal_error(__LINE__, __FILE__)
+/*** output ***/
+#define LOGFILE_STDOUT "/tmp/cfg-stdout.log"
+#define LOGFILE_STDERR "/tmp/cfg-stderr.log"
+
+extern int out_fd;
+extern FILE *out_stream;
+
+extern int initialize_output();
+
#endif
diff --git a/src/commit.c b/src/commit.c
index a136b58..a8edbaf 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -257,7 +257,11 @@ int main(int argc, char **argv)
struct stat statbuf;
int st;
boolean update_pending = FALSE;
-
+
+ if (initialize_output() == -1) {
+ exit(-1);
+ }
+
set_in_commit(TRUE);
dump_log( argc, argv);
init_paths(TRUE);
diff --git a/src/delete.c b/src/delete.c
index a825a22..5981923 100644
--- a/src/delete.c
+++ b/src/delete.c
@@ -70,6 +70,10 @@ int main(int argc, char **argv)
char *cp, *delp, *endp;
boolean do_umount;
+ if (initialize_output() == -1) {
+ exit(-1);
+ }
+
if (argc < 2) {
fprintf(stderr, "Need to specify the config node to delete\n");
exit(1);
diff --git a/src/set.c b/src/set.c
index 566dfe0..39d4a2f 100644
--- a/src/set.c
+++ b/src/set.c
@@ -94,6 +94,10 @@ int main(int argc, char **argv)
boolean need_mod = FALSE, not_new = FALSE;
boolean empty_val = FALSE;
+ if (initialize_output() == -1) {
+ exit(-1);
+ }
+
dump_log( argc, argv);
init_edit();
last_tag = FALSE;