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>
38 static void NONRETURNING serv_syscallfail(const char *msg) {
39 fputs("uservd(service): ",stderr);
44 static void serv_resetsignal(int signo) {
47 sig.sa_handler= SIG_DFL;
48 sigemptyset(&sig.sa_mask);
50 if (sigaction(signo,&sig,0)) serv_syscallfail("reset signal handler");
53 static const char *see_logname(void) { return serviceuser; }
54 static const char *see_home(void) { return serviceuser_dir; }
55 static const char *see_shell(void) { return serviceuser_shell; }
57 static const char *see_path(void) {
58 return serviceuser_uid ? DEFAULTPATH_USER : DEFAULTPATH_ROOT;
61 static const char *see_service(void) { return service; }
62 static const char *see_c_cwd(void) { return cwd; }
63 static const char *see_c_logname(void) { return logname; }
64 static const char *see_c_uid(void) {
65 static char buf[CHAR_BIT*sizeof(uid_t)/3+4];
66 snyprintf(buf,sizeof(buf),"%lu",(unsigned long)request_mbuf.callinguid);
70 static const char *see_c_list(int n, const char *(*fn)(int i)) {
74 for (i=0, l=1; i<n; i++) l+= strlen(fn(i))+1;
75 r= xmalloc(l); r[l-1]= '*';
76 for (i=0, *r=0; i<n; i++) snytprintfcat(r,l,"%s ",fn(i));
77 assert(!r[l-1] && r[l-2]==' ');
82 static const char *seei_group(int i) {
83 return calling_groups[i];
85 static const char *see_c_group(void) {
86 return see_c_list(request_mbuf.ngids,seei_group);
89 static const char *seei_gid(int i) {
90 static char buf[CHAR_BIT*sizeof(gid_t)/3+4];
92 snyprintf(buf,sizeof(buf),"%d",calling_gids[i]);
95 static const char *see_c_gid(void) {
96 return see_c_list(request_mbuf.ngids,seei_gid);
99 static const struct serv_envinfo {
101 const char *(*fn)(void);
103 { "USER", see_logname },
104 { "LOGNAME", see_logname },
105 { "HOME", see_home },
106 { "SHELL", see_shell },
107 { "PATH", see_path },
108 { "USERV_SERVICE", see_service },
109 { "USERV_CWD", see_c_cwd },
110 { "USERV_USER", see_c_logname },
111 { "USERV_UID", see_c_uid },
112 { "USERV_GROUP", see_c_group },
113 { "USERV_GID", see_c_gid },
117 void execservice(const int synchsocket[], int clientfd) {
118 static const char *const setenvpfargs[]= {
121 ". " SETENVIRONMENTPATH "; exec \"$@\"",
125 int fd, realfd, holdfd, newfd, r, envvarbufsize=0, targ, nargs, i, l;
127 const char **args, *const *cpp;
130 const struct serv_envinfo *sei;
132 if (dup2(fdarray[2].realfd,2)<0) {
133 static const char duperrmsg[]= "uservd(service): cannot dup2 for stderr\n";
134 write(fdarray[2].realfd,duperrmsg,sizeof(duperrmsg)-1);
137 serv_resetsignal(SIGPIPE);
138 serv_resetsignal(SIGCHLD);
140 if (close(synchsocket[0])) serv_syscallfail("close parent synch socket");
142 if (setpgid(0,0)) serv_syscallfail("set process group");
144 r= write(synchsocket[1],&synchmsg,1);
145 if (r!=1) serv_syscallfail("write synch byte to parent");
146 r= synchread(synchsocket[1],'g');
147 if (r) serv_syscallfail("reach synch byte from parent");
149 if (close(clientfd)) serv_syscallfail("close client socket fd");
151 /* Now we have to make all the fd's work. It's rather a complicated
152 * algorithm, unfortunately. We remember in holdfd[fd] whether fd
153 * is being used to hold a file descriptor we actually want for some
154 * other real fd in the service program; holdfd[fd] contains the fd
155 * we eventually want fd to be dup'd into, so that realfd[holdfd[fd]]==fd.
156 * After setting up the holdfds we go through the fds in order of
157 * eventual fd making sure that fd is the one we want it to be. If the
158 * holdfd tells us we're currently storing some other fd in there we
159 * move it out of the way with dup and record its new location.
161 for (fd=0; fd<fdarrayused; fd++) {
162 if (fdarray[fd].holdfd == -1) continue;
163 if (close(fdarray[fd].holdfd)) serv_syscallfail("close pipe hold fd");
164 fdarray[fd].holdfd= -1;
166 for (fd=0; fd<fdarrayused; fd++) {
167 if (fdarray[fd].realfd < fdarrayused) fdarray[fdarray[fd].realfd].holdfd= fd;
169 for (fd=0; fd<fdarrayused; fd++) {
170 realfd= fdarray[fd].realfd;
171 if (realfd == -1) continue;
172 holdfd= fdarray[fd].holdfd;
174 assert(realfd == fd);
175 fdarray[fd].holdfd= -1;
177 } else if (holdfd != -1) {
178 assert(fdarray[holdfd].realfd == fd);
179 newfd= dup(fd); if (newfd<0) serv_syscallfail("dup out of the way");
180 fdarray[holdfd].realfd= newfd;
181 if (newfd<fdarrayused) fdarray[newfd].holdfd= holdfd;
182 fdarray[fd].holdfd= -1;
184 if (dup2(fdarray[fd].realfd,fd)<0) serv_syscallfail("dup2 set up fd");
185 if (close(fdarray[fd].realfd)) serv_syscallfail("close old fd");
186 if (fcntl(fd,F_SETFD,0)<0) serv_syscallfail("set no-close-on-exec on fd");
187 fdarray[fd].realfd= fd;
190 for (sei= serv_envinfos; sei->name; sei++)
191 if (setenv(sei->name,sei->fn(),1)) serv_syscallfail("setenv standard");
192 for (i=0; i<request_mbuf.nvars; i++) {
193 l= strlen(defvararray[i][0])+9;
194 if (l>envvarbufsize) { envvarbufsize= l; envvarbuf= xrealloc(envvarbuf,l); }
195 snyprintf(envvarbuf,l,"USERV_U_%s",defvararray[i][0]);
196 if (setenv(envvarbuf,defvararray[i][1],1)) serv_syscallfail("setenv defvar");
200 if (setenvironment) for (cpp= setenvpfargs; *cpp; cpp++) nargs++;
202 if (execargs) for (pp= execargs; *pp; pp++) nargs++;
203 if (!suppressargs) nargs+= request_mbuf.nargs;
204 args= xmalloc(sizeof(char*)*(nargs+1));
206 if (setenvironment) for (cpp= setenvpfargs; *cpp; cpp++) args[targ++]= *cpp;
207 args[targ++]= execpath;
208 if (execargs) for (pp= execargs; *pp; pp++) args[targ++]= *pp;
209 if (!suppressargs) for (i=0; i<request_mbuf.nargs; i++) args[targ++]= argarray[i];
212 execv(args[0],(char* const*)args);
214 serv_syscallfail("exec service program");