Handle hostnames with upper-case letters
[webmin.git] / squid / init_cache.cgi
1 #!/usr/local/bin/perl
2 # init_cache.cgi
3 # Initialize the cache by running squid with the -z option
4
5 require './squid-lib.pl';
6 $access{'rebuild'} || &error($text{'icache_ecannot'});
7 &ReadParse();
8 $whatfailed = $text{'icache_ftic'};
9
10 # set user to run squid as..
11 &lock_file($config{'squid_conf'});
12 $conf = &get_config();
13 if (!$in{'nouser'}) {
14         $in{'user'} || &error($text{'icache_ymcautrsa'});
15         @uinfo = getpwnam($in{'user'});
16         scalar(@uinfo) || &error($text{'icache_euser'});
17         @ginfo = getgrgid($uinfo[3]);
18         if ($squid_version < 2) {
19                 $dir = { 'name' => 'cache_effective_user',
20                          'values' => [ $in{'user'}, $ginfo[0] ] };
21                 &save_directive($conf, "cache_effective_user", [ $dir ]);
22                 }
23         else {
24                 $dir = { 'name' => 'cache_effective_user',
25                          'values' => [ $in{'user'} ] };
26                 &save_directive($conf, "cache_effective_user", [ $dir ]);
27                 $dir = { 'name' => 'cache_effective_group',
28                          'values' => [ $ginfo[0] ] };
29                 &save_directive($conf, "cache_effective_group", [ $dir ]);
30                 }
31         &flush_file_lines();
32         }
33 &unlock_file($config{'squid_conf'});
34
35 # Stop squid (if running)
36 &ui_print_unbuffered_header(undef, $text{'icache_title'}, "");
37 if ($pidstruct = &find_config("pid_filename", $conf)) {
38         $pidfile = $pidstruct->{'values'}->[0];
39         }
40 else { $pidfile = $config{'pid_file'}; }
41 if (open(PID, $pidfile)) {
42         <PID> =~ /(\d+)/; $pid = $1;
43         close(PID);
44         }
45 if ($pid && kill(0, $pid)) {
46         print "<p>$text{'clear_stop'}<br>\n";
47         &system_logged("$config{'squid_path'} -f $config{'squid_conf'} ".
48                        "-k shutdown >/dev/null 2>&1");
49         for($i=0; $i<40; $i++) {
50                 if (!kill(0, $pid)) { last; }
51                 sleep(1);
52                 }
53         print "$text{'clear_done'}<br>\n";
54         $stopped++;
55         }
56
57 # Initialize the cache
58 ($user, $group) = &get_squid_user($conf);
59 if ($user) {
60         foreach $c (split(/\s+/, $in{'caches'})) {
61                 mkdir($c, 0755);
62                 }
63         }
64 &chown_files($user, $group, $conf);
65 $cmd = "$config{'squid_path'} -f $config{'squid_conf'} -z";
66 print "<p>", &text('icache_itscwtc',$cmd), "<br>\n";
67 print "<pre>\n";
68 &additional_log('exec', undef, $cmd);
69 open(INIT, "$cmd 2>&1 |");
70 while(<INIT>) {
71         print;
72         }
73 close(INIT);
74 print "</pre>\n";
75
76 # Try to re-start squid (if it was running before)
77 if ($stopped) {
78         $temp = &transname();
79         &system_logged("$config{'squid_path'} -sY -f $config{'squid_conf'} >$temp 2>&1 </dev/null &");
80         sleep(3);
81         $errs = `cat $temp`;
82         unlink($temp);
83         if ($errs) {
84                 &system_logged("$config{'squid_path'} -k shutdown -f $config{'squid_conf'} >/dev/null 2>&1");
85                 print "$text{'clear_failrestart'}<br>\n";
86                 print "<pre>$errs</pre>\n";
87                 }
88         }
89
90 &webmin_log("init");
91 &ui_print_footer("", $text{'icache_return'});
92