]> git.proxmox.com Git - pve-manager-legacy.git/blob - bin/spiceproxy
spiceproxy: improve loggin code
[pve-manager-legacy.git] / bin / spiceproxy
1 #!/usr/bin/perl -w -T
2
3 # Note: In theory, all this can be done by 'pveproxy' daemon. But som API call
4 # still have blocking code, so we use a separate daemon to avoid that the console
5 # get blocked.
6
7 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
8
9 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
10
11 use strict;
12 use English;
13 use Getopt::Long;
14 use PVE::SafeSyslog;
15 use PVE::APIDaemon;
16
17 my $pidfile = "/var/run/pveproxy/spiceproxy.pid";
18 my $lockfile = "/var/lock/spiceproxy.lck";
19
20 my $opt_debug;
21
22 initlog ('spiceproxy');
23
24 if (!GetOptions ('debug' => \$opt_debug)) {
25 die "usage: $0 [--debug]\n";
26 }
27
28 $SIG{'__WARN__'} = sub {
29 my $err = $@;
30 my $t = $_[0];
31 chomp $t;
32 syslog('warning', "WARNING: %s", $t);
33 $@ = $err;
34 };
35
36 $0 = "spiceproxy";
37
38 my $gid = getgrnam('www-data') || die "getgrnam failed - $!\n";
39 POSIX::setgid($gid) || die "setgid $gid failed - $!\n";
40 $EGID = "$gid $gid"; # this calls setgroups
41 my $uid = getpwnam('www-data') || die "getpwnam failed - $!\n";
42 POSIX::setuid($uid) || die "setuid $uid failed - $!\n";
43
44 # just to be sure
45 die "detected strange uid/gid\n" if !($UID == $uid && $EUID == $uid && $GID eq "$gid $gid" && $EGID eq "$gid $gid");
46
47 my $cpid;
48 my $daemon;
49 eval {
50 $daemon = PVE::APIDaemon->new(
51 port => 3128,
52 keep_alive => 0,
53 max_workers => 1, # do we need more?
54 max_conn => 500,
55 lockfile => $lockfile,
56 debug => $opt_debug,
57 spiceproxy => 1,
58 logfile => '/var/log/pveproxy/access.log',
59 );
60 };
61
62 my $err = $@;
63
64 if ($err) {
65 syslog ('err' , "unable to start server: $err");
66 print STDERR $err;
67 exit (-1);
68 }
69
70 if ($opt_debug || !($cpid = fork ())) {
71
72 $SIG{PIPE} = 'IGNORE';
73 $SIG{INT} = 'IGNORE' if !$opt_debug;
74
75 $SIG{TERM} = $SIG{QUIT} = sub {
76 syslog ('info' , "server closing");
77
78 $SIG{INT} = 'DEFAULT';
79
80 unlink "$pidfile";
81
82 exit (0);
83 };
84
85 syslog ('info' , "starting server");
86
87 if (!$opt_debug) {
88 # redirect STDIN/STDOUT/SDTERR to /dev/null
89 open STDIN, '</dev/null' || die "can't read /dev/null [$!]";
90 open STDOUT, '>/dev/null' || die "can't write /dev/null [$!]";
91 open STDERR, '>&STDOUT' || die "can't open STDERR to STDOUT [$!]";
92 }
93
94 POSIX::setsid();
95
96 eval {
97 $daemon->start_server();
98 };
99 my $err = $@;
100
101 if ($err) {
102 syslog ('err' , "unexpected server error: $err");
103 print STDERR $err if $opt_debug;
104 exit (-1);
105 }
106
107 } else {
108
109 open (PIDFILE, ">$pidfile") ||
110 die "cant write '$pidfile' - $! :ERROR";
111 print PIDFILE "$cpid\n";
112 close (PIDFILE) ||
113 die "cant write '$pidfile' - $! :ERROR";
114 }
115
116 exit (0);
117
118 __END__
119
120 =head1 NAME
121
122 spiceproxy - SPICE proxy server for Proxmox VE
123
124 =head1 SYNOPSIS
125
126 spiceproxy [--debug]
127
128 =head1 DESCRIPTION
129
130 SPICE proxy server for Proxmox VE. Listens on port 3128.
131
132 =head1 COPYRIGHT AND DISCLAIMER
133
134 Copyright (C) 2007-2013 Proxmox Server Solutions GmbH
135
136 This program is free software: you can redistribute it and/or modify it
137 under the terms of the GNU Affero General Public License as published
138 by the Free Software Foundation, either version 3 of the License, or
139 (at your option) any later version.
140
141 This program is distributed in the hope that it will be useful, but
142 WITHOUT ANY WARRANTY; without even the implied warranty of
143 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
144 Affero General Public License for more details.
145
146 You should have received a copy of the GNU Affero General Public
147 License along with this program. If not, see
148 <http://www.gnu.org/licenses/>.
149