3 * daemon code which executes actual service (ie child process)
5 * Copyright (C)1996-1997 Ian Jackson
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with userv; if not, write to the Free Software
19 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include <sys/types.h>
39 static void NONRETURNING serv_syscallfail(const char *msg) {
40 fputs("uservd(service): ",stderr);
45 static void NONRETURNING serv_checkstdoutexit(void) {
46 if (ferror(stdout) || fclose(stdout)) serv_syscallfail("write stdout");
50 void bisexec_environment(const char *const *argv) {
51 execlp("env","env",(char*)0);
52 serv_syscallfail("execute `env'");
55 void bisexec_parameter(const char *const *argv) {
56 always_dumpparameter(execargs[0],execargs+1);
57 serv_checkstdoutexit();
60 void bisexec_version(const char *const *argv) {
61 const unsigned char *p;
64 printf("uservd version " VERSION "; copyright (C)1996-1997 Ian Jackson.\n"
70 " - protocol magic number %08lx\n"
73 for (i=0, p=protocolchecksumversion; i<sizeof(protocolchecksumversion); i++, p++)
76 "rendezvous socket: `" RENDEZVOUSPATH "'\n"
77 "system config dir: `" SYSTEMCONFIGDIR "'\n"
78 "pipe filename format: `%s' (max length %d)\n"
79 "maximums: fd %-10d general string %d"
80 " gids %-10d override length %d\n\n"
81 " args or variables %-10d error message %d\n"
82 " nested inclusion %-10d errno string reserve %d\n",
83 PIPEFORMAT, PIPEMAXLEN,
84 MAX_ALLOW_FD, MAX_GENERAL_STRING,
85 MAX_GIDS, MAX_OVERRIDE_LEN,
86 MAX_ARGSDEFVAR, ERRMSG_RESERVE_ERRNO,
87 MAX_INCLUDE_NEST, MAX_ERRMSG_LEN);
88 serv_checkstdoutexit();
91 static void NONRETURNING dumpconfig(const char *string) {
96 while ((c= *string++)) {
98 case ' ': nspaces++; break;
99 case '\n': nspaces= 0; putchar('\n'); break;
101 while (nspaces>0) { putchar(' '); nspaces--; }
105 assert(*--string == '\n');
106 serv_checkstdoutexit();
109 void bisexec_toplevel(const char *const *argv) {
110 dumpconfig(TOPLEVEL_CONFIGURATION);
113 void bisexec_override(const char *const *argv) {
114 dumpconfig(TOPLEVEL_OVERRIDDEN_CONFIGURATION);
117 void bisexec_reset(const char *const *argv) {
118 dumpconfig(RESET_CONFIGURATION);
121 void bisexec_execute(const char *const *argv) {
122 always_dumpexecsettings();
123 serv_checkstdoutexit();
126 static void serv_resetsignal(int signo) {
127 struct sigaction sig;
129 sig.sa_handler= SIG_DFL;
130 sigemptyset(&sig.sa_mask);
132 if (sigaction(signo,&sig,0)) serv_syscallfail("reset signal handler");
135 static const char *see_logname(void) { return serviceuser; }
136 static const char *see_home(void) { return serviceuser_dir; }
137 static const char *see_shell(void) { return serviceuser_shell; }
139 static const char *see_service(void) { return service; }
140 static const char *see_c_cwd(void) { return cwd; }
141 static const char *see_c_logname(void) { return logname; }
142 static const char *see_c_uid(void) {
143 static char buf[CHAR_BIT*sizeof(uid_t)/3+4];
144 snyprintf(buf,sizeof(buf),"%lu",(unsigned long)request_mbuf.callinguid);
148 static const char *see_c_list(int n, const char *(*fn)(int i)) {
152 for (i=0, l=1; i<n; i++) l+= strlen(fn(i))+1;
153 r= xmalloc(l); r[l-1]= '*';
154 for (i=0, *r=0; i<n; i++) snytprintfcat(r,l,"%s ",fn(i));
155 assert(!r[l-1] && r[l-2]==' ');
160 static const char *seei_group(int i) {
161 return calling_groups[i];
163 static const char *see_c_group(void) {
164 return see_c_list(request_mbuf.ngids,seei_group);
167 static const char *seei_gid(int i) {
168 static char buf[CHAR_BIT*sizeof(gid_t)/3+4];
170 snyprintf(buf,sizeof(buf),"%d",calling_gids[i]);
173 static const char *see_c_gid(void) {
174 return see_c_list(request_mbuf.ngids,seei_gid);
177 static const struct serv_envinfo {
179 const char *(*fn)(void);
181 { "USER", see_logname },
182 { "LOGNAME", see_logname },
183 { "HOME", see_home },
184 { "SHELL", see_shell },
185 { "PATH", defaultpath },
186 { "USERV_SERVICE", see_service },
187 { "USERV_CWD", see_c_cwd },
188 { "USERV_USER", see_c_logname },
189 { "USERV_UID", see_c_uid },
190 { "USERV_GROUP", see_c_group },
191 { "USERV_GID", see_c_gid },
195 void execservice(const int synchsocket[], int clientfd) {
196 static const char *const setenvpfargs[]= {
199 ". " SETENVIRONMENTPATH "; exec \"$@\"",
203 int fd, realfd, holdfd, newfd, r, envvarbufsize=0, targ, nargs, i, l;
205 const char **args, *const *cpp;
208 const struct serv_envinfo *sei;
210 if (dup2(fdarray[2].realfd,2)<0) {
211 static const char duperrmsg[]= "uservd(service): cannot dup2 for stderr\n";
212 write(fdarray[2].realfd,duperrmsg,sizeof(duperrmsg)-1);
215 serv_resetsignal(SIGPIPE);
216 serv_resetsignal(SIGCHLD);
218 if (close(synchsocket[0])) serv_syscallfail("close parent synch socket");
220 if (setpgid(0,0)) serv_syscallfail("set process group");
222 r= write(synchsocket[1],&synchmsg,1);
223 if (r!=1) serv_syscallfail("write synch byte to parent");
224 r= synchread(synchsocket[1],'g');
225 if (r) serv_syscallfail("reach synch byte from parent");
227 if (close(clientfd)) serv_syscallfail("close client socket fd");
229 /* Now we have to make all the fd's work. It's rather a complicated
230 * algorithm, unfortunately. We remember in holdfd[fd] whether fd
231 * is being used to hold a file descriptor we actually want for some
232 * other real fd in the service program; holdfd[fd] contains the fd
233 * we eventually want fd to be dup'd into, so that realfd[holdfd[fd]]==fd.
234 * After setting up the holdfds we go through the fds in order of
235 * eventual fd making sure that fd is the one we want it to be. If the
236 * holdfd tells us we're currently storing some other fd in there we
237 * move it out of the way with dup and record its new location.
239 for (fd=0; fd<fdarrayused; fd++) {
240 if (fdarray[fd].holdfd == -1) continue;
241 if (close(fdarray[fd].holdfd)) serv_syscallfail("close pipe hold fd");
242 fdarray[fd].holdfd= -1;
244 for (fd=0; fd<fdarrayused; fd++) {
245 if (fdarray[fd].realfd < fdarrayused) fdarray[fdarray[fd].realfd].holdfd= fd;
247 for (fd=0; fd<fdarrayused; fd++) {
248 realfd= fdarray[fd].realfd;
249 if (realfd == -1) continue;
250 holdfd= fdarray[fd].holdfd;
252 assert(realfd == fd);
253 fdarray[fd].holdfd= -1;
255 } else if (holdfd != -1) {
256 assert(fdarray[holdfd].realfd == fd);
257 newfd= dup(fd); if (newfd<0) serv_syscallfail("dup out of the way");
258 fdarray[holdfd].realfd= newfd;
259 if (newfd<fdarrayused) fdarray[newfd].holdfd= holdfd;
260 fdarray[fd].holdfd= -1;
262 if (dup2(fdarray[fd].realfd,fd)<0) serv_syscallfail("dup2 set up fd");
263 if (close(fdarray[fd].realfd)) serv_syscallfail("close old fd");
264 if (fcntl(fd,F_SETFD,0)<0) serv_syscallfail("set no-close-on-exec on fd");
265 fdarray[fd].realfd= fd;
268 for (sei= serv_envinfos; sei->name; sei++)
269 if (setenv(sei->name,sei->fn(),1)) serv_syscallfail("setenv standard");
270 for (i=0; i<request_mbuf.nvars; i++) {
271 l= strlen(defvararray[i].key)+9;
272 if (l>envvarbufsize) { envvarbufsize= l; envvarbuf= xrealloc(envvarbuf,l); }
273 snyprintf(envvarbuf,l,"USERV_U_%s",defvararray[i].key);
274 if (setenv(envvarbuf,defvararray[i].value,1)) serv_syscallfail("setenv defvar");
278 if (setenvironment) for (cpp= setenvpfargs; *cpp; cpp++) nargs++;
280 if (execargs) for (pp= execargs; *pp; pp++) nargs++;
281 if (!suppressargs) nargs+= request_mbuf.nargs;
282 args= xmalloc(sizeof(char*)*(nargs+1));
284 if (setenvironment) for (cpp= setenvpfargs; *cpp; cpp++) args[targ++]= *cpp;
285 args[targ++]= execpath;
286 if (execargs) for (pp= execargs; *pp; pp++) args[targ++]= *pp;
287 if (!suppressargs) for (i=0; i<request_mbuf.nargs; i++) args[targ++]= argarray[i];
293 execv(args[0],(char* const*)args);
295 serv_syscallfail("exec service program");