]> git.proxmox.com Git - pve-manager.git/blob - bin/pvedaemon
introduce base_handler_class
[pve-manager.git] / bin / pvedaemon
1 #!/usr/bin/perl -T
2
3 $ENV{'PATH'} = '/sbin:/bin:/usr/sbin:/usr/bin';
4
5 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
6
7 use strict;
8 use warnings;
9 use Getopt::Long;
10 use POSIX ":sys_wait_h";
11 use Socket;
12 use PVE::SafeSyslog;
13 use PVE::APIDaemon;
14 use PVE::API2;
15
16 my $pidfile = "/var/run/pvedaemon.pid";
17 my $lockfile = "/var/lock/pvedaemon.lck";
18
19 my $opt_debug;
20
21 initlog ('pvedaemon');
22
23 if (!GetOptions ('debug' => \$opt_debug)) {
24 die "usage: $0 [--debug]\n";
25 }
26
27 $SIG{'__WARN__'} = sub {
28 my $err = $@;
29 my $t = $_[0];
30 chomp $t;
31 syslog('warning', "WARNING: %s", $t);
32 $@ = $err;
33 };
34
35 $0 = "pvedaemon";
36
37 # create dir for dtach sockets
38 mkdir "/var/run/dtach";
39
40 my $cpid;
41 my $daemon;
42 eval {
43 $daemon = PVE::APIDaemon->new(
44 base_handler_class => 'PVE::API2',
45 host => "127.0.0.1",
46 port => 85,
47 trusted_env => 1, # partly trusted, because only local programs can connect
48 lockfile => $lockfile,
49 debug => $opt_debug,
50 keep_alive => 100,
51 max_conn => 500,
52 max_requests => 1000);
53 };
54
55 my $err = $@;
56
57 if ($err) {
58 syslog ('err' , "unable to start server: $err");
59 print STDERR $err;
60 exit (-1);
61 }
62
63 if ($opt_debug || !($cpid = fork ())) {
64
65 $SIG{PIPE} = 'IGNORE';
66 $SIG{INT} = 'IGNORE' if !$opt_debug;
67
68 $SIG{TERM} = $SIG{QUIT} = sub {
69 syslog ('info' , "server closing");
70
71 $SIG{INT} = 'DEFAULT';
72
73 unlink "$pidfile";
74
75 exit (0);
76 };
77
78 syslog ('info' , "starting server");
79
80 if (!$opt_debug) {
81 # redirect STDIN/STDOUT/SDTERR to /dev/null
82 open STDIN, '</dev/null' || die "can't read /dev/null [$!]";
83 open STDOUT, '>/dev/null' || die "can't write /dev/null [$!]";
84 open STDERR, '>&STDOUT' || die "can't open STDERR to STDOUT [$!]";
85 }
86
87 POSIX::setsid();
88
89 system ("echo > /var/lib/pve-manager/vmops"); # init vmops file
90
91 eval {
92 $daemon->start_server();
93 };
94 my $err = $@;
95
96 if ($err) {
97 syslog ('err' , "unexpected server error: $err");
98 print STDERR $err if $opt_debug;
99 exit (-1);
100 }
101
102 } else {
103
104 open (PIDFILE, ">$pidfile") ||
105 die "cant write '$pidfile' - $! :ERROR";
106 print PIDFILE "$cpid\n";
107 close (PIDFILE) ||
108 die "cant write '$pidfile' - $! :ERROR";
109 }
110
111 exit (0);
112
113 __END__
114
115 =head1 NAME
116
117 pvedaemon - the PVE configuration server
118
119 =head1 SYNOPSIS
120
121 pvedaemon [--debug]
122
123 =head1 DESCRIPTION
124
125 All configuration is done using this Server. The Server only
126 listens to a local address 127.0.0.1 port 85 for security
127 reasons.
128
129 =head1 COPYRIGHT AND DISCLAIMER
130
131 Copyright (C) 2007-2013 Proxmox Server Solutions GmbH
132
133 This program is free software: you can redistribute it and/or modify it
134 under the terms of the GNU Affero General Public License as published
135 by the Free Software Foundation, either version 3 of the License, or
136 (at your option) any later version.
137
138 This program is distributed in the hope that it will be useful, but
139 WITHOUT ANY WARRANTY; without even the implied warranty of
140 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
141 Affero General Public License for more details.
142
143 You should have received a copy of the GNU Affero General Public
144 License along with this program. If not, see
145 <http://www.gnu.org/licenses/>.
146