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