10 sub gen_wrapper () { #{{{
11 $config{srcdir}=abs_path($config{srcdir});
12 $config{destdir}=abs_path($config{destdir});
13 my $this=abs_path($0);
15 error("$this doesn't seem to be executable");
19 error("cannot create a wrapper that uses a setup file");
21 my $wrapper=possibly_foolish_untaint($config{wrapper});
22 if (! defined $wrapper || ! length $wrapper) {
23 error("wrapper filename not specified");
25 delete $config{wrapper};
28 push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
29 CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
30 HTTP_COOKIE} if $config{cgi};
32 foreach my $var (@envsave) {
34 if ((s=getenv("$var")))
35 asprintf(&newenviron[i++], "%s=%s", "$var", s);
38 if ($config{svn} && $config{notify}) {
39 # Support running directly as hooks/post-commit by passing
40 # $2 in REV in the environment.
43 asprintf(&newenviron[i++], "REV=%s", argv[2]);
44 else if ((s=getenv("REV")))
45 asprintf(&newenviron[i++], "%s=%s", "REV", s);
49 $Data::Dumper::Indent=0; # no newlines
50 my $configstring=Data::Dumper->Dump([\%config], ['*config']);
51 $configstring=~s/\\/\\\\/g;
52 $configstring=~s/"/\\"/g;
54 open(OUT, ">$wrapper.c") || error("failed to write $wrapper.c: $!");;
56 /* A wrapper for ikiwiki, can be safely made suid. */
63 extern char **environ;
65 int main (int argc, char **argv) {
66 /* Sanitize environment. */
68 char *newenviron[$#envsave+5];
71 newenviron[i++]="HOME=$ENV{HOME}";
72 newenviron[i++]="WRAPPED_OPTIONS=$configstring";
76 execl("$this", "$this", NULL);
77 perror("failed to run $this");
82 if (system("gcc", "$wrapper.c", "-o", $wrapper) != 0) {
83 error("failed to compile $wrapper.c");
86 if (defined $config{wrappermode} &&
87 ! chmod(oct($config{wrappermode}), $wrapper)) {
88 error("chmod $wrapper: $!");
90 print "successfully generated $wrapper\n";