]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Wrapper.pm
fix child process error propigation
[ikiwiki.git] / IkiWiki / Wrapper.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use File::Spec;
8 use Data::Dumper;
9 use IkiWiki;
10
11 sub gen_wrapper () {
12         $config{srcdir}=File::Spec->rel2abs($config{srcdir});
13         $config{destdir}=File::Spec->rel2abs($config{destdir});
14         my $this=File::Spec->rel2abs($0);
15         if (! -x $this) {
16                 error(sprintf(gettext("%s doesn't seem to be executable"), $this));
17         }
18
19         if ($config{setup}) {
20                 error(gettext("cannot create a wrapper that uses a setup file"));
21         }
22         my $wrapper=possibly_foolish_untaint($config{wrapper});
23         if (! defined $wrapper || ! length $wrapper) {
24                 error(gettext("wrapper filename not specified"));
25         }
26         delete $config{wrapper};
27         
28         my @envsave;
29         push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
30                        CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
31                        HTTP_COOKIE REMOTE_USER HTTPS REDIRECT_STATUS
32                        REDIRECT_URL} if $config{cgi};
33         my $envsave="";
34         foreach my $var (@envsave) {
35                 $envsave.=<<"EOF";
36         if ((s=getenv("$var")))
37                 addenv("$var", s);
38 EOF
39         }
40         
41         my @wrapper_hooks;
42         run_hooks(genwrapper => sub { push @wrapper_hooks, shift->() });
43
44         my $check_commit_hook="";
45         my $pre_exec="";
46         if ($config{post_commit}) {
47                 # Optimise checking !commit_hook_enabled() , 
48                 # so that ikiwiki does not have to be started if the
49                 # hook is disabled.
50                 #
51                 # Note that perl's flock may be implemented using fcntl
52                 # or lockf on some systems. If so, and if there is no
53                 # interop between the locking systems, the true C flock will
54                 # always succeed, and this optimisation won't work.
55                 # The perl code will later correctly check the lock,
56                 # so the right thing will still happen, though without
57                 # the benefit of this optimisation.
58                 $check_commit_hook=<<"EOF";
59         {
60                 int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
61                 if (fd != -1) {
62                         if (flock(fd, LOCK_SH | LOCK_NB) != 0)
63                                 exit(0);
64                         close(fd);
65                 }
66         }
67 EOF
68         }
69         elsif ($config{cgi}) {
70                 # Avoid more than one ikiwiki cgi running at a time by
71                 # taking a cgi lock. Since ikiwiki uses several MB of
72                 # memory, a pile up of processes could cause thrashing
73                 # otherwise. The fd of the lock is stored in
74                 # IKIWIKI_CGILOCK_FD so unlockwiki can close it.
75                 $pre_exec=<<"EOF";
76         lockfd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
77         if (lockfd != -1 && flock(lockfd, LOCK_EX) == 0) {
78                 char *fd_s=malloc(8);
79                 sprintf(fd_s, "%i", lockfd);
80                 setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
81         }
82 EOF
83         }
84
85         my $set_background_command='';
86         if (defined $config{wrapper_background_command} &&
87             length $config{wrapper_background_command}) {
88                 my $background_command=delete $config{wrapper_background_command};
89                 $set_background_command=~s/"/\\"/g;
90                 $set_background_command='#define BACKGROUND_COMMAND "'.$background_command.'"';
91         }
92
93         $Data::Dumper::Indent=0; # no newlines
94         my $configstring=Data::Dumper->Dump([\%config], ['*config']);
95         $configstring=~s/\\/\\\\/g;
96         $configstring=~s/"/\\"/g;
97         $configstring=~s/\n/\\n/g;
98         
99         writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
100 /* A wrapper for ikiwiki, can be safely made suid. */
101 #include <stdio.h>
102 #include <sys/types.h>
103 #include <sys/stat.h>
104 #include <fcntl.h>
105 #include <unistd.h>
106 #include <stdlib.h>
107 #include <string.h>
108 #include <sys/file.h>
109
110 extern char **environ;
111 char *newenviron[$#envsave+6];
112 int i=0;
113
114 void addenv(char *var, char *val) {
115         char *s=malloc(strlen(var)+1+strlen(val)+1);
116         if (!s)
117                 perror("malloc");
118         sprintf(s, "%s=%s", var, val);
119         newenviron[i++]=s;
120 }
121
122 int main (int argc, char **argv) {
123         int lockfd=-1;
124         char *s;
125
126 $check_commit_hook
127 @wrapper_hooks
128 $envsave
129         newenviron[i++]="HOME=$ENV{HOME}";
130         newenviron[i++]="WRAPPED_OPTIONS=$configstring";
131
132 #ifdef __TINYC__
133         /* old tcc versions do not support modifying environ directly */
134         if (clearenv() != 0) {
135                 perror("clearenv");
136                 exit(1);
137         }
138         for (; i>0; i--)
139                 putenv(newenviron[i-1]);
140 #else
141         newenviron[i]=NULL;
142         environ=newenviron;
143 #endif
144
145         if (setregid(getegid(), -1) != 0 &&
146             setregid(getegid(), -1) != 0) {
147                 perror("failed to drop real gid");
148                 exit(1);
149         }
150         if (setreuid(geteuid(), -1) != 0 &&
151             setreuid(geteuid(), -1) != 0) {
152                 perror("failed to drop real uid");
153                 exit(1);
154         }
155
156 $pre_exec
157
158 $set_background_command
159 #ifdef BACKGROUND_COMMAND
160         if (lockfd != -1) {
161                 close(lockfd);
162         }
163
164         pid_t pid=fork();
165         if (pid == -1) {
166                 perror("fork");
167                 exit(1);
168         }
169         else if (pid == 0) {
170                 execl("$this", "$this", NULL);
171                 perror("exec $this");
172                 exit(1);                
173         }
174         else {
175                 waitpid(pid, NULL, 0);
176
177                 if (daemon(1, 0) == 0) {
178                         system(BACKGROUND_COMMAND);
179                         exit(0);
180                 }
181                 else {
182                         perror("daemon");
183                         exit(1);
184                 }
185         }
186 #else
187         execl("$this", "$this", NULL);
188         perror("exec $this");
189         exit(1);
190 #endif
191 }
192 EOF
193
194         my @cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
195         push @cc, possibly_foolish_untaint($ENV{CFLAGS}) if exists $ENV{CFLAGS};
196         if (system(@cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
197                 #translators: The parameter is a C filename.
198                 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
199         }
200         unlink("$wrapper.c");
201         if (defined $config{wrappergroup}) {
202                 my $gid=(getgrnam($config{wrappergroup}))[2];
203                 if (! defined $gid) {
204                         error(sprintf("bad wrappergroup"));
205                 }
206                 if (! chown(-1, $gid, "$wrapper.new")) {
207                         error("chown $wrapper.new: $!");
208                 }
209         }
210         if (defined $config{wrappermode} &&
211             ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
212                 error("chmod $wrapper.new: $!");
213         }
214         if (! rename("$wrapper.new", $wrapper)) {
215                 error("rename $wrapper.new $wrapper: $!");
216         }
217         #translators: The parameter is a filename.
218         printf(gettext("successfully generated %s"), $wrapper);
219         print "\n";
220 }
221
222 1