From ae561e41e5578b373311556c9f9e619f277f825b Mon Sep 17 00:00:00 2001 From: Lukas Larsson Date: Wed, 8 Jan 2014 16:42:12 +0100 Subject: ose: Refactor global variables to ppdata This is needed because when starting multiple processes from the same shell command they will see the same global data if using status variables. --- erts/etc/common/run_erl_common.c | 198 ++++++++++++++++++++++++--------------- erts/etc/common/run_erl_common.h | 3 - erts/etc/ose/run_erl.c | 2 +- erts/etc/unix/run_erl.c | 2 +- 4 files changed, 127 insertions(+), 78 deletions(-) (limited to 'erts/etc') diff --git a/erts/etc/common/run_erl_common.c b/erts/etc/common/run_erl_common.c index bf55056090..dc55c2bea4 100644 --- a/erts/etc/common/run_erl_common.c +++ b/erts/etc/common/run_erl_common.c @@ -68,6 +68,54 @@ #define USE_FSYNC 1 #endif +/* Global variable definitions + * We need this complex way of handling global variables because of how + * OSE works here. We want to make it possible to run the shell command + * run_erl multiple times with different global variables without them + * effecting eachother. + */ +typedef struct run_erl_ run_erl; + +#ifdef __OSE__ +static OSPPDKEY run_erl_pp_key; +#define RE_DATA (*(run_erl**)ose_get_ppdata(run_erl_pp_key)) +#else +static run_erl re; +#define RE_DATA (&re) +#endif + +#define STATUSFILE (RE_DATA->statusfile) +#define LOG_DIR (RE_DATA->log_dir) +#define STDSTATUS (RE_DATA->stdstatus) +#define LOG_GENERATIONS (RE_DATA->log_generations) +#define LOG_MAXSIZE (RE_DATA->log_maxsize) +#define LOG_ACTIVITY_MINUTES (RE_DATA->log_activity_minutes) +#define LOG_ALIVE_IN_GMT (RE_DATA->log_alive_in_gmt) +#define LOG_ALIVE_FORMAT (RE_DATA->log_alive_format) +#define RUN_DAEMON (RE_DATA->run_daemon) +#define LOG_ALIVE_MINUTES (RE_DATA->log_alive_minutes) +#define LOG_NUM (RE_DATA->log_num) +#define LFD (RE_DATA->lfd) +#define PROTOCOL_VER (RE_DATA->protocol_ver) + +struct run_erl_ { + /* constant config data */ + char statusfile[FILENAME_BUFSIZ]; + char log_dir[FILENAME_BUFSIZ]; + FILE *stdstatus; + int log_generations; + int log_maxsize; + int log_activity_minutes; + int log_alive_in_gmt; + char log_alive_format[ALIVE_BUFFSIZ+1]; + int run_daemon; + int log_alive_minutes; + /* Current log number and log fd */ + int log_num; + int lfd; + unsigned protocol_ver; +}; + /* prototypes */ static int next_log(int log_num); @@ -75,25 +123,6 @@ static int prev_log(int log_num); static int find_next_log_num(void); static int open_log(int log_num, int flags); -/* static data */ -static char statusfile[FILENAME_BUFSIZ]; -static char log_dir[FILENAME_BUFSIZ]; -static FILE *stdstatus = NULL; -static int log_generations = DEFAULT_LOG_GENERATIONS; -static int log_maxsize = DEFAULT_LOG_MAXSIZE; -static int log_activity_minutes = DEFAULT_LOG_ACTIVITY_MINUTES; -static int log_alive_in_gmt = 0; -static char log_alive_format[ALIVE_BUFFSIZ+1]; -static int run_daemon = 0; -static unsigned protocol_ver = RUN_ERL_LO_VER; /* assume lowest to begin with */ - -/* - * Current log number and log fd - */ -static int log_num; -static int lfd=0; - - /* * getenv_int: */ @@ -111,7 +140,7 @@ static char *getenv_int(const char *name) { * (Wrapping after log_generations) */ static int next_log(int log_num) { - return log_num>=log_generations?1:log_num+1; + return log_num>=LOG_GENERATIONS?1:log_num+1; } /* @@ -120,7 +149,7 @@ static int next_log(int log_num) { * (Wrapping after log_generations) */ static int prev_log(int log_num) { - return log_num<=1?log_generations:log_num-1; + return log_num<=1?LOG_GENERATIONS:log_num-1; } /* @@ -139,11 +168,11 @@ static int find_next_log_num(void) { /* Initialize exiting log table */ - for(i=log_generations; i>=0; i--) + for(i=LOG_GENERATIONS; i>=0; i--) log_exists[i] = 0; - dirp = opendir(log_dir); + dirp = opendir(LOG_DIR); if(!dirp) { - ERRNO_ERR1(LOG_ERR,"Can't access log directory '%s'", log_dir); + ERRNO_ERR1(LOG_ERR,"Can't access log directory '%s'", LOG_DIR); exit(1); } @@ -152,7 +181,7 @@ static int find_next_log_num(void) { while((direntp=readdir(dirp)) != NULL) { if(strncmp(direntp->d_name,LOG_STUBNAME,stub_len)==0) { int num = atoi(direntp->d_name+stub_len); - if(num < 1 || num > log_generations) + if(num < 1 || num > LOG_GENERATIONS) continue; log_exists[num] = 1; } @@ -162,7 +191,7 @@ static int find_next_log_num(void) { /* Find out the next available log file number */ next_gen = 0; - for(i=log_generations; i>=0; i--) { + for(i=LOG_GENERATIONS; i>=0; i--) { if(log_exists[i]) if(next_gen) break; @@ -191,27 +220,27 @@ static int open_log(int log_num, int flags) /* Remove the next log (to keep a "hole" in the log sequence) */ sn_printf(buf, sizeof(buf), "%s/%s%d", - log_dir, LOG_STUBNAME, next_log(log_num)); + LOG_DIR, LOG_STUBNAME, next_log(log_num)); unlink(buf); /* Create or continue on the current log file */ - sn_printf(buf, sizeof(buf), "%s/%s%d", log_dir, LOG_STUBNAME, log_num); + sn_printf(buf, sizeof(buf), "%s/%s%d", LOG_DIR, LOG_STUBNAME, log_num); - lfd = sf_open(buf, flags, LOG_PERM); + LFD = sf_open(buf, flags, LOG_PERM); - if(lfd <0){ + if(LFD <0){ ERRNO_ERR1(LOG_ERR,"Can't open log file '%s'.", buf); exit(1); } /* Write a LOGGING STARTED and time stamp into the log file */ time(&now); - if (log_alive_in_gmt) { + if (LOG_ALIVE_IN_GMT) { tmptr = gmtime(&now); } else { tmptr = localtime(&now); } - if (!strftime(log_buffer, ALIVE_BUFFSIZ, log_alive_format, + if (!strftime(log_buffer, ALIVE_BUFFSIZ, LOG_ALIVE_FORMAT, tmptr)) { strn_cpy(log_buffer, sizeof(log_buffer), "(could not format time in 256 positions " @@ -221,14 +250,14 @@ static int open_log(int log_num, int flags) sn_printf(buf, sizeof(buf), "\n=====\n===== LOGGING STARTED %s\n=====\n", log_buffer); - if (erts_run_erl_write_all(lfd, buf, strlen(buf)) < 0) + if (erts_run_erl_write_all(LFD, buf, strlen(buf)) < 0) erts_run_erl_log_status("Error in writing to log.\n"); #if USE_FSYNC - fsync(lfd); + fsync(LFD); #endif - return lfd; + return LFD; } /* Instead of making sure basename exists, we do our own */ @@ -307,12 +336,12 @@ void erts_run_erl_log_status(const char *format,...) va_list args; time_t now; - if (stdstatus == NULL) - stdstatus = fopen(statusfile, "w"); - if (stdstatus == NULL) + if (STDSTATUS == NULL) + STDSTATUS = fopen(STATUSFILE, "w"); + if (STDSTATUS == NULL) return; now = time(NULL); - fprintf(stdstatus, "run_erl [%d] %s", + fprintf(STDSTATUS, "run_erl [%d] %s", #ifdef __OSE__ (int)current_process(), #else @@ -320,12 +349,17 @@ void erts_run_erl_log_status(const char *format,...) #endif ctime(&now)); va_start(args, format); - vfprintf(stdstatus, format, args); + vfprintf(STDSTATUS, format, args); va_end(args); - fflush(stdstatus); + fflush(STDSTATUS); return; } +/* Fetch the current log alive minutes */ +int erts_run_erl_log_alive_minutes() { + return LOG_ALIVE_MINUTES; +} + /* error_logf() * Prints the arguments to stderr or syslog * Works like printf (see vfprintf) @@ -336,7 +370,7 @@ void erts_run_erl_log_error(int priority, int line, const char *format, ...) va_start(args, format); #ifdef HAVE_SYSLOG_H - if (run_daemon) { + if (RUN_DAEMON) { vsyslog(priority,format,args); } else @@ -373,24 +407,24 @@ int erts_run_erl_log_write(char* buf, size_t len) ssize_t res; /* Decide if new logfile needed, and open if so */ - size = lseek(lfd,0,SEEK_END); - if(size+len > log_maxsize) { + size = lseek(LFD,0,SEEK_END); + if(size+len > LOG_MAXSIZE) { int res; do { - res = close(lfd); + res = close(LFD); } while (res < 0 && errno == EINTR); - log_num = next_log(log_num); - lfd = open_log(log_num, O_RDWR|O_CREAT|O_TRUNC|O_SYNC); + LOG_NUM = next_log(LOG_NUM); + LFD = open_log(LOG_NUM, O_RDWR|O_CREAT|O_TRUNC|O_SYNC); } /* Write to log file */ - if ((res = erts_run_erl_write_all(lfd, buf, len)) < 0) { + if ((res = erts_run_erl_write_all(LFD, buf, len)) < 0) { erts_run_erl_log_status("Error in writing to log.\n"); } #if USE_FSYNC - fsync(lfd); + fsync(LFD); #endif return res; } @@ -399,17 +433,17 @@ int erts_run_erl_log_activity(int timeout,time_t now,time_t last_activity) { char log_alive_buffer[ALIVE_BUFFSIZ+1]; char buf[BUFSIZ]; - if (timeout || now - last_activity > log_activity_minutes*60) { + if (timeout || now - last_activity > LOG_ACTIVITY_MINUTES*60) { /* Either a time out: 15 minutes without action, */ /* or something is coming in right now, but it's a long time */ /* since last time, so let's write a time stamp this message */ struct tm *tmptr; - if (log_alive_in_gmt) { + if (LOG_ALIVE_IN_GMT) { tmptr = gmtime(&now); } else { tmptr = localtime(&now); } - if (!strftime(log_alive_buffer, ALIVE_BUFFSIZ, log_alive_format, + if (!strftime(log_alive_buffer, ALIVE_BUFFSIZ, LOG_ALIVE_FORMAT, tmptr)) { strn_cpy(log_alive_buffer, sizeof(log_alive_buffer), "(could not format time in 256 positions " @@ -426,30 +460,48 @@ int erts_run_erl_log_activity(int timeout,time_t now,time_t last_activity) { int erts_run_erl_log_open() { - log_num = find_next_log_num(); - lfd = open_log(log_num, O_RDWR|O_APPEND|O_CREAT|O_SYNC); + LOG_NUM = find_next_log_num(); + LFD = open_log(LOG_NUM, O_RDWR|O_APPEND|O_CREAT|O_SYNC); return 0; } int erts_run_erl_log_init(int daemon, char* logdir) { char *p; +#ifdef __OSE__ + run_erl **re_pp; + if (!run_erl_pp_key) + ose_create_ppdata("run_erl_ppdata",&run_erl_pp_key); + re_pp = (run_erl **)ose_get_ppdata(run_erl_pp_key); + *re_pp = malloc(sizeof(run_erl)); +#endif + + STDSTATUS = NULL; + LOG_GENERATIONS = DEFAULT_LOG_GENERATIONS; + LOG_MAXSIZE = DEFAULT_LOG_MAXSIZE; + LOG_ACTIVITY_MINUTES = DEFAULT_LOG_ACTIVITY_MINUTES; + LOG_ALIVE_IN_GMT = 0; + RUN_DAEMON = 0; + LOG_ALIVE_MINUTES = DEFAULT_LOG_ALIVE_MINUTES; + LFD = 0; + PROTOCOL_VER = RUN_ERL_LO_VER; /* assume lowest to begin with */ + /* Get values for LOG file handling from the environment */ if ((p = getenv_int("RUN_ERL_LOG_ALIVE_MINUTES"))) { - erts_run_erl_log_alive_minutes = atoi(p); - if (!erts_run_erl_log_alive_minutes) { + LOG_ALIVE_MINUTES = atoi(p); + if (!LOG_ALIVE_MINUTES) { ERROR1(LOG_ERR,"Minimum value for RUN_ERL_LOG_ALIVE_MINUTES is 1 " "(current value is %s)",p); } - log_activity_minutes = erts_run_erl_log_alive_minutes / 3; - if (!log_activity_minutes) { - ++log_activity_minutes; + LOG_ACTIVITY_MINUTES = LOG_ALIVE_MINUTES / 3; + if (!LOG_ACTIVITY_MINUTES) { + ++LOG_ACTIVITY_MINUTES; } } if ((p = getenv_int( "RUN_ERL_LOG_ACTIVITY_MINUTES"))) { - log_activity_minutes = atoi(p); - if (!log_activity_minutes) { + LOG_ACTIVITY_MINUTES = atoi(p); + if (!LOG_ACTIVITY_MINUTES) { ERROR1(LOG_ERR,"Minimum value for RUN_ERL_LOG_ACTIVITY_MINUTES is 1 " "(current value is %s)",p); } @@ -459,36 +511,36 @@ int erts_run_erl_log_init(int daemon, char* logdir) { ERROR1(LOG_ERR, "RUN_ERL_LOG_ALIVE_FORMAT can contain a maximum of " "%d characters", ALIVE_BUFFSIZ); } - strn_cpy(log_alive_format, sizeof(log_alive_format), p); + strn_cpy(LOG_ALIVE_FORMAT, sizeof(LOG_ALIVE_FORMAT), p); } else { - strn_cpy(log_alive_format, sizeof(log_alive_format), + strn_cpy(LOG_ALIVE_FORMAT, sizeof(LOG_ALIVE_FORMAT), DEFAULT_LOG_ALIVE_FORMAT); } if ((p = getenv_int("RUN_ERL_LOG_ALIVE_IN_UTC")) && strcmp(p,"0")) { - ++log_alive_in_gmt; + ++LOG_ALIVE_IN_GMT; } if ((p = getenv_int("RUN_ERL_LOG_GENERATIONS"))) { - log_generations = atoi(p); - if (log_generations < LOG_MIN_GENERATIONS) + LOG_GENERATIONS = atoi(p); + if (LOG_GENERATIONS < LOG_MIN_GENERATIONS) ERROR1(LOG_ERR,"Minimum RUN_ERL_LOG_GENERATIONS is %d", LOG_MIN_GENERATIONS); - if (log_generations > LOG_MAX_GENERATIONS) + if (LOG_GENERATIONS > LOG_MAX_GENERATIONS) ERROR1(LOG_ERR,"Maximum RUN_ERL_LOG_GENERATIONS is %d", LOG_MAX_GENERATIONS); } if ((p = getenv_int("RUN_ERL_LOG_MAXSIZE"))) { - log_maxsize = atoi(p); - if (log_maxsize < LOG_MIN_MAXSIZE) + LOG_MAXSIZE = atoi(p); + if (LOG_MAXSIZE < LOG_MIN_MAXSIZE) ERROR1(LOG_ERR,"Minimum RUN_ERL_LOG_MAXSIZE is %d", LOG_MIN_MAXSIZE); } - run_daemon = daemon; + RUN_DAEMON = daemon; - strn_cpy(log_dir, sizeof(log_dir), logdir); - strn_cpy(statusfile, sizeof(statusfile), log_dir); - strn_cat(statusfile, sizeof(statusfile), STATUSFILENAME); + strn_cpy(LOG_DIR, sizeof(LOG_DIR), logdir); + strn_cpy(STATUSFILE, sizeof(STATUSFILE), LOG_DIR); + strn_cat(STATUSFILE, sizeof(STATUSFILE), STATUSFILENAME); return 0; } diff --git a/erts/etc/common/run_erl_common.h b/erts/etc/common/run_erl_common.h index 3b763ad927..c47a0db054 100644 --- a/erts/etc/common/run_erl_common.h +++ b/erts/etc/common/run_erl_common.h @@ -71,9 +71,6 @@ char *simple_basename(char *path); #define ERRNO_ERR1(Prio,Format,A1) erts_run_erl_log_error(Prio,__LINE__,ADD_ERRNO(Format),A1) #define ERRNO_ERR2(Prio,Format,A1,A2) erts_run_erl_log_error(Prio,__LINE__,ADD_ERRNO(Format),A1,A2) -/* defined in run_common.c */ -extern int erts_run_erl_log_alive_minutes; - #define RUN_ERL_USAGE \ "%s (pipe_name|pipe_dir/) log_dir \"command [parameters ...]\"" \ "\n\nDESCRIPTION:\n" \ diff --git a/erts/etc/ose/run_erl.c b/erts/etc/ose/run_erl.c index c1a258515c..6bb59b7f7e 100644 --- a/erts/etc/ose/run_erl.c +++ b/erts/etc/ose/run_erl.c @@ -351,7 +351,7 @@ int pass_on(ProgramState *s) { time_t now,last_activity; time(&last_activity); - sig = receive_w_tmo(erts_run_erl_log_alive_minutes*60000,sigsel); + sig = receive_w_tmo(erts_run_erl_log_alive_minutes()*60000,sigsel); time(&now); diff --git a/erts/etc/unix/run_erl.c b/erts/etc/unix/run_erl.c index b3f0591b07..a6fc4c2bf5 100644 --- a/erts/etc/unix/run_erl.c +++ b/erts/etc/unix/run_erl.c @@ -328,7 +328,7 @@ static void pass_on(pid_t childpid) } time(&last_activity); /* don't assume old BSD bug */ - timeout.tv_sec = erts_run_erl_log_alive_minutes*60; + timeout.tv_sec = erts_run_erl_log_alive_minutes()*60; timeout.tv_usec = 0; ready = select(maxfd + 1, &readfds, writefds_ptr, NULL, &timeout); if (ready < 0) { -- cgit v1.2.3