Rev 11125 | Rev 15847 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 11125 | Rev 12472 | ||
|---|---|---|---|
| Line 21... | Line 21... | ||
| 21 | 21 | ||
| 22 | int exec_wait; |
22 | int exec_wait; |
| 23 | 23 | ||
| 24 | int execredirected(char *cmdf, char *inf, char *outf, char *errf, char *arg[]) |
24 | int execredirected(char *cmdf, char *inf, char *outf, char *errf, char *arg[]) |
| 25 | { |
25 | { |
| 26 |
|
26 | pid_t pid; |
| 27 |
|
27 | int status, t; |
| 28 | 28 | ||
| 29 |
|
29 | fflush(NULL); /* flush all output streams before forking |
| 30 | |
30 | * otherwise they will be doubled */ |
| 31 |
|
31 | pid=fork(); if(pid==-1) return -1; |
| 32 |
|
32 | if(!pid) { /* child */ |
| 33 |
|
33 | char buf[4096]; int k; |
| 34 |
|
34 | if(inf!=NULL) (void)freopen(inf,"r",stdin); |
| 35 |
|
35 | if(outf!=NULL) (void)freopen(outf,"w",stdout); |
| 36 |
|
36 | if(errf!=NULL) (void)freopen(errf,"w",stderr); |
| 37 |
|
37 | /* This is to patch LinuxPPC uid wrapping |
| 38 | |
38 | * for scripts */ |
| 39 |
|
39 | t=0; if(strchr(cmdf,'/')) { |
| 40 |
|
40 | FILE *tf; |
| 41 |
|
41 | char buf[16]; |
| 42 |
|
42 | tf=fopen(cmdf,"r"); (void)fread(buf,1,10,tf); fclose(tf); |
| 43 |
|
43 | if(memcmp(buf+1,"ELF",3)!=0) t=1; |
| 44 |
|
44 | } |
| 45 |
|
45 | errno=0; |
| 46 |
|
46 | if(strchr(cmdf,'/')) execve(cmdf,arg,environ); |
| 47 |
|
47 | else execvp(cmdf,arg); |
| 48 |
|
48 | snprintf(buf,sizeof(buf),"Failed to execute"); |
| 49 |
|
49 | for(k=0;arg[k];k++) { |
| 50 | t=strlen(buf); |
- | |
| 51 | snprintf(buf+t,sizeof(buf)-t," %s",arg[k]); |
- | |
| 52 | } |
- | |
| 53 | t=strlen(buf); |
50 | t=strlen(buf); |
| - | 51 | snprintf(buf+t,sizeof(buf)-t," %s",arg[k]); |
|
| - | 52 | } |
|
| - | 53 | t=strlen(buf); |
|
| 54 |
|
54 | snprintf(buf+t,sizeof(buf)-t,"\n %s\n",strerror(errno)); |
| 55 |
|
55 | wlogdaccessfile(buf,"a","%s/exec.fail",tmpd); |
| 56 |
|
56 | exit(127); |
| 57 |
|
57 | } |
| 58 |
|
58 | else { /* parent */ |
| 59 |
|
59 | status=0; |
| 60 |
|
60 | if(exec_wait) { |
| 61 |
|
61 | waitpid(pid,&status,0); |
| 62 |
|
62 | return WEXITSTATUS(status); |
| 63 |
|
63 | } |
| 64 |
|
64 | else { |
| 65 |
|
65 | exec_wait=1; addfork(pid,0); return 0; |
| 66 |
|
66 | } |
| 67 |
|
67 | } |
| 68 | } |
68 | } |
| 69 | 69 | ||
| 70 | /* my system(), but with variable parms |
70 | /* my system(), but with variable parms |
| 71 | * More secure than system(), and direct fork. |
71 | * More secure than system(), and direct fork. |
| 72 | */ |
72 | */ |
| 73 | int call_ssh(int wait,char *s,...) |
73 | int call_ssh(int wait,char *s,...) |
| 74 | { |
74 | { |
| 75 |
|
75 | va_list vp; |
| 76 |
|
76 | char buf[MAX_LINELEN+1]; |
| 77 |
|
77 | char *arg[1024]; |
| 78 |
|
78 | char *inf=NULL, *outf=NULL, *errf=NULL; |
| 79 |
|
79 | char *cmdf, *p, *p2; |
| 80 |
|
80 | int i, d; |
| 81 | 81 | ||
| 82 |
|
82 | va_start(vp,s); |
| 83 |
|
83 | vsnprintf(buf,sizeof(buf),s,vp); |
| 84 |
|
84 | va_end(vp); |
| 85 |
|
85 | p=find_word_start(buf); if(*p==0) return 0; |
| 86 |
|
86 | cmdf=p; |
| 87 |
|
87 | for(i=0;*p!=0 && i<1000; p=find_word_start(p2)) { |
| 88 |
|
88 | switch(*p) { |
| 89 |
|
89 | case '\'': { |
| 90 |
|
90 | p++; p2=strchr(p,'\''); if(p2==NULL) p2=p+strlen(p); |
| 91 |
|
91 | d=0; break; |
| 92 |
|
92 | } |
| 93 |
|
93 | case '"': { |
| 94 |
|
94 | p++; p2=strchr(p,'"'); if(p2==NULL) p2=p+strlen(p); |
| 95 |
|
95 | d=0; break; |
| 96 |
|
96 | } |
| 97 |
|
97 | default: d=1; p2=find_word_end(p); break; |
| 98 |
|
98 | } |
| 99 |
|
99 | if(*p2) *p2++=0; |
| 100 |
|
100 | if(!d) {arg[i++]=p; continue;} |
| 101 |
|
101 | switch(*p) { |
| 102 |
|
102 | case '<': inf=++p; break; |
| 103 |
|
103 | case '>': { |
| - | 104 | p++; |
|
| 104 |
|
105 | if(*p=='&') { |
| 105 |
|
106 | merge: p++; errf=outf=p; break; |
| 106 |
|
107 | } |
| 107 |
|
108 | else outf=p; |
| 108 |
|
109 | break; |
| 109 |
|
110 | } |
| 110 |
|
111 | case '&': { |
| 111 |
|
112 | p++; if(*p=='>') goto merge; |
| 112 |
|
113 | else break; |
| 113 |
|
114 | } |
| 114 |
|
115 | case '2': { |
| 115 |
|
116 | if(*(p+1)=='>') {errf=p+2; break;} |
| 116 | } |
- | |
| 117 | default: arg[i++]=p; break; |
- | |
| 118 | } |
117 | } |
| - | 118 | default: arg[i++]=p; break; |
|
| 119 | } |
119 | } |
| - | 120 | } |
|
| 120 |
|
121 | arg[i]=NULL; |
| 121 |
|
122 | exec_wait=wait; |
| 122 |
|
123 | return execredirected(cmdf,inf,outf,errf,arg); |
| 123 | } |
124 | } |
| 124 | 125 | ||
| 125 | /* Read/write to a file with variable parms to print filename */ |
126 | /* Read/write to a file with variable parms to print filename */ |
| 126 | void wlogdaccessfile(char *content, char *type, char *s,...) |
127 | void wlogdaccessfile(char *content, char *type, char *s,...) |
| 127 | { |
128 | { |
| 128 |
|
129 | va_list vp; |
| 129 |
|
130 | char buf[MAX_LINELEN+1]; |
| 130 |
|
131 | FILE *f; |
| 131 |
|
132 | int l; |
| 132 | 133 | ||
| 133 |
|
134 | va_start(vp,s); |
| 134 |
|
135 | vsnprintf(buf,sizeof(buf),s,vp); |
| 135 |
|
136 | va_end(vp); |
| 136 |
|
137 | f=fopen(buf,type); |
| 137 |
|
138 | if(f==NULL) { |
| 138 |
|
139 | if(*type=='r') content[0]=0; |
| 139 |
|
140 | return; |
| - | 141 | } |
|
| - | 142 | switch(*type) { |
|
| - | 143 | case 'a': |
|
| - | 144 | case 'w': { |
|
| - | 145 | l=strlen(content); fwrite(content,1,l,f); break; |
|
| 140 | } |
146 | } |
| 141 | switch(*type) { |
- | |
| 142 | case 'a': |
- | |
| 143 | case 'w': { |
- | |
| 144 | l=strlen(content); fwrite(content,1,l,f); break; |
- | |
| 145 | } |
- | |
| 146 |
|
147 | case 'r': { |
| 147 |
|
148 | l=fread(content,1,MAX_LINELEN-1,f); |
| 148 |
|
149 | if(l>0 && l<MAX_LINELEN) content[l]=0; |
| 149 |
|
150 | else content[0]=0; |
| 150 |
|
151 | _tolinux(content); |
| 151 |
|
152 | break; |
| 152 | } |
- | |
| 153 | default: { |
- | |
| 154 | content[0]=0; break; |
- | |
| 155 | } |
- | |
| 156 | } |
153 | } |
| - | 154 | default: { |
|
| - | 155 | content[0]=0; break; |
|
| - | 156 | } |
|
| - | 157 | } |
|
| 157 |
|
158 | fclose(f); |
| 158 | } |
159 | } |
| 159 | 160 | ||
| 160 | /* system(), but with variable parms |
161 | /* system(), but with variable parms |
| 161 | * Uses sh to execute command. |
162 | * Uses sh to execute command. |
| 162 | */ |
163 | */ |
| 163 | int call_sh(int wait,char *s,...) |
164 | int call_sh(int wait,char *s,...) |
| 164 | { |
165 | { |
| 165 |
|
166 | va_list vp; |
| 166 |
|
167 | char buf[MAX_LINELEN+1]; |
| 167 |
|
168 | char *abuf[8]; |
| 168 | 169 | ||
| 169 |
|
170 | va_start(vp,s); |
| 170 |
|
171 | vsnprintf(buf,sizeof(buf),s,vp); |
| 171 |
|
172 | va_end(vp); |
| 172 |
|
173 | abuf[0]="sh"; abuf[1]="-c"; abuf[2]=buf; abuf[3]=NULL; |
| 173 |
|
174 | exec_wait=wait; |
| 174 |
|
175 | return execredirected(abuf[0],NULL,NULL,NULL,abuf); |
| 175 | } |
176 | } |
| 176 | 177 | ||
| 177 | void wimslogd_error(char *msg) |
178 | void wimslogd_error(char *msg) |
| 178 | { |
179 | { |
| 179 |
|
180 | fprintf(stderr,"%s %s\n",nowstr, msg); |
| 180 | } |
181 | } |
| 181 | 182 | ||
| 182 | void debug(char *p,...) |
183 | void debug(char *p,...) |
| 183 | { |
184 | { |
| 184 |
|
185 | char lbuf[MAX_LINELEN+1]; |
| 185 |
|
186 | char *pp; |
| 186 |
|
187 | va_list vp; |
| 187 | 188 | ||
| 188 |
|
189 | snprintf(lbuf,sizeof(lbuf),"%s: ",nowstr); |
| 189 |
|
190 | pp=lbuf+strlen(lbuf); |
| 190 |
|
191 | va_start(vp,p); |
| 191 |
|
192 | vsnprintf(pp,sizeof(lbuf)-(pp-lbuf),p,vp); |
| 192 |
|
193 | va_end(vp); |
| 193 |
|
194 | pp=strchr(lbuf,'\n'); if(pp) *pp=0; |
| 194 |
|
195 | strip_trailing_spaces(lbuf); strcat(lbuf,"\n"); |
| 195 |
|
196 | wlogdaccessfile(lbuf,"a",debugfile); |
| 196 | } |
197 | } |
| 197 | - | ||