2 "$Id: misc.c 11626 2009-04-04 12:41:46Z lalo $";
40 #include <sys/types.h>
58 if (!directory || !*directory)
60 strcpy (buf, directory);
61 while ((cp = strchr (cp + 1, (
int)
'/'))) {
63 if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) {
67 if (mkdir (buf, 0777)) {
69 perror (
"Couldn't make path to file");
76 if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) {
80 if (mkdir (buf, 0777)) {
82 perror (
"Couldn't make path to file");
99 if (!filename || !*filename)
101 strcpy (buf, filename);
102 while ((cp = strchr (cp + 1, (
int)
'/'))) {
104 if (stat (buf, &statbuf) || !S_ISDIR (statbuf.st_mode)) {
108 "Couldn't make path to file: %s", strerror(errno));
110 if (mkdir (buf, 0777)) {
111 perror (
"Couldn't make path to file");
126 char *c=(
char *)malloc(
sizeof(
char)*strlen(str)+1);
158 static char mybuf[20480];
160 snprintf(mybuf,
sizeof(mybuf),
"[%s] (%s) %s\n",
getLogLevelText(level),origin,message);
178 void LOG(
LogLevel level,
const char *origin,
const char *format, ...)
182 static char buf[20480];
189 va_start(ap, format);
192 vsnprintf(buf,
sizeof(buf), format, ap);
213 len=read (cp->
tube[pipe],buf,511);
217 LOG(
LOG_ERROR,
"common::purgePipe",
"Child %s: could not read from pipe %d!",cp->
name?cp->
name:
"UNKNOWN",pipe);
222 buf[len<512?len:511]=
'\0';
228 next=strstr(current,
"\n");
246 if (waitpid(cp->
pid,NULL,WNOHANG)){
250 LOG(
LOG_INFO,
"common::monitorChilds",
"Child %s died. Removing and closing pipes",cp->
name?cp->
name:
"UNKNOWN");
274 if ( (pipe<1) || (pipe>2))
277 snprintf(buf,
sizeof(buf),
"Child%d::%s::%d",child->
pid,child->
name?child->
name:
"NONAME",pipe);
280 if (fcntl(child->
tube[pipe], F_SETFL, O_NDELAY)==-1) {
307 LOG(
LOG_INFO,
"common::raiseChild",
"Raising %s with flags %d",name,flag);
311 "Serious CHILD error, unknown pipe requested: 0x%X for %s",
321 while ( *args && *args!=
' ' ) ++args;
322 while ( *args && *args==
' ' ) ++args;
329 if ( pipe(pipe_err) ){
330 LOG(
LOG_ERROR,
"common::raiseChild",
"Couldn't create stderr pipe for %s",name);
335 if ( pipe(pipe_in) ){
336 LOG(
LOG_ERROR,
"common::raiseChild",
"Couldn't create stdin pipe for %s",name);
337 if (flag&CHILD_STDERR){
345 if ( pipe(pipe_out) ){
346 LOG(
LOG_ERROR,
"common::raiseChild",
"Couldn't create stdout pipe for %s",name);
347 if (flag&CHILD_STDERR){
351 if (flag&CHILD_STDIN){
361 LOG(
LOG_ERROR,
"common::raiseChild",
"Couldn't create child for %s. Closing pipes",name);
362 if (flag&CHILD_STDIN){
366 if (flag&CHILD_STDOUT){
370 if (flag&CHILD_STDERR){
385 while (args && *args)
389 while ( *args && *args!=
' ' ) ++args;
395 while ( *args && *args==
' ' ) ++args;
400 if (flag&CHILD_STDERR){
401 r=dup2(pipe_err[1],2);
405 fprintf(stderr,
"common::raiseChild Failed to set pipe_err as stderr\n");
408 if (flag&CHILD_STDOUT){
409 r=dup2(pipe_out[1],1);
413 fprintf(stderr,
"common::raiseChild Failed to set pipe_out as stdout\n");
416 if (flag&CHILD_STDIN){
417 r=dup2(pipe_in[0],0);
421 fprintf(stderr,
"common::raiseChild Failed to set pipe_in as stdin\n");
424 for (i=3;i<100;++i) close(i);
427 execvp(argv[0],argv);
431 if (flag&CHILD_STDIN){
437 if (flag&CHILD_STDOUT){
443 if (flag&CHILD_STDERR){
449 cp->
name=strdup(name);
int make_path_to_file(char *filename)
void logChildPipe(ChildProcess *child, LogLevel level, int flag)
void logPipe(ChildProcess *child, LogLevel level, int pipe)
ChildProcess * FirstChild
#define LOG_SETMESSAGE(_Entry, _msg)
void purgePipe(ChildProcess *cp, int pipe)
int setLogListener(LogListener li)
void LOG(LogLevel level, const char *origin, const char *format,...)
#define CHILD_PIPEIN(__child)
#define CHILD_PIPEERR(__child)
#define LOG_SETORIGIN(_Entry, _orig)
char * getLogText(const LogEntry *le)
struct ChildProcess * next
void clearLogListener(void)
char * getLogTextRaw(LogLevel level, const char *origin, const char *message)
#define LOG_APPEND(_Entry)
const char *const rcsid_common_misc_c
#define CHILD_PIPEOUT(__child)
ChildProcess * raiseChild(char *name, int flag)
void(* LogListener)(LogEntry *)
static const char *const LogLevelTexts[]
char * strdup_local(const char *str)
int make_path_to_dir(char *directory)
static const char * getLogLevelText(LogLevel level)