Create pid file in workdir if run directly from unpriv. user
This commit is contained in:
@@ -34,7 +34,7 @@ my $runAsUid = $<;
|
|||||||
my $piddir = "/var/run/" . basename (abs_path ($0));
|
my $piddir = "/var/run/" . basename (abs_path ($0));
|
||||||
my $C = loadConf ();
|
my $C = loadConf ();
|
||||||
|
|
||||||
# if we run under root, create pid dir and set correct owner for it
|
# if we run under root, create pid dir and set correct owner for it, same for debug log if it is defined
|
||||||
if ($< == 0) {
|
if ($< == 0) {
|
||||||
my $user = $C->{user};
|
my $user = $C->{user};
|
||||||
die "Unable to run as root, please define unprivileged user in config.json\n" unless (defined ($user));
|
die "Unable to run as root, please define unprivileged user in config.json\n" unless (defined ($user));
|
||||||
@@ -60,25 +60,22 @@ if ($< == 0) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# script intended to run as "unit" user, so drop privs to "unit" user
|
# drop privs
|
||||||
setgid ($gid) or die "Unable to switch to group of $user($!)\n";
|
setgid ($gid) or die "Unable to switch to group of $user($!)\n";
|
||||||
setuid ($uid) or die "Unable to switch to account $user($!)\n";
|
setuid ($uid) or die "Unable to switch to account $user($!)\n";
|
||||||
|
} else {
|
||||||
|
$piddir = $workdir;
|
||||||
}
|
}
|
||||||
|
|
||||||
# double fork attach to /dev/null all stdio and go background
|
# double fork attach to /dev/null all stdio and go background
|
||||||
daemonize ();
|
daemonize ();
|
||||||
|
|
||||||
# write pidfile
|
# write pidfile
|
||||||
my $pidfile;
|
my $pidfile = sprintf ("%s/%s.pid", $piddir, basename (abs_path ($0)));
|
||||||
|
logger ("Creating pidfile at $pidfile");
|
||||||
if ($runAsUid == 0) {
|
open (PID, ">", $pidfile) or die "Error opening $pidfile: $!\n";
|
||||||
# TODO: handle case where we run with unpriv. user
|
print PID $$ or die "Error writing to $pidfile: $!\n";
|
||||||
$pidfile = sprintf ("%s/%s.pid", $piddir, basename (abs_path ($0)));
|
close PID;
|
||||||
logger ("Creating pidfile at $pidfile");
|
|
||||||
open (PID, ">", $pidfile) or die "Error opening $pidfile: $!\n";
|
|
||||||
print PID $$ or die "Error writing to $pidfile: $!\n";
|
|
||||||
close PID;
|
|
||||||
}
|
|
||||||
|
|
||||||
# set correct proctitle
|
# set correct proctitle
|
||||||
$0 = abs_path ($0);
|
$0 = abs_path ($0);
|
||||||
|
|||||||
Reference in New Issue
Block a user