| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::Milter::Limit::Util; | 
| 2 |  |  |  |  |  |  | $App::Milter::Limit::Util::VERSION = '0.52'; | 
| 3 |  |  |  |  |  |  | # ABSTRACT: utility functions for App::Milter::Limit | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 4 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 7 | 1 |  |  | 1 |  | 5 | use POSIX qw(setsid); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 8 | 1 |  |  | 1 |  | 55 | use File::Path 2.0 (); | 
|  | 1 |  |  |  |  | 23 |  | 
|  | 1 |  |  |  |  | 16 |  | 
| 9 | 1 |  |  | 1 |  | 4 | use App::Milter::Limit::Config; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 9 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub daemonize { | 
| 13 | 0 | 0 |  | 0 | 1 |  | my $pid = fork and exit 0; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 0 |  |  |  |  |  | my $sid = setsid(); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | # detach from controlling TTY | 
| 18 | 0 |  |  |  |  |  | $SIG{HUP} = 'IGNORE'; | 
| 19 | 0 | 0 |  |  |  |  | $pid = fork and exit 0; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | # reset umask | 
| 22 | 0 |  |  |  |  |  | umask 027; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 0 | 0 |  |  |  |  | chdir '/' or die "can't chdir: $!"; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 0 |  |  |  |  |  | open STDIN,  '+>/dev/null'; | 
| 27 | 0 |  |  |  |  |  | open STDOUT, '+>&STDIN'; | 
| 28 | 0 |  |  |  |  |  | open STDERR, '+>&STDIN'; | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 0 |  |  |  |  |  | return $sid; | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | sub get_uid { | 
| 35 | 0 |  |  | 0 | 1 |  | my $user = shift; | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 0 | 0 |  |  |  |  | unless ($user =~ /^\d+$/) { | 
| 38 | 0 |  |  |  |  |  | my $uid = getpwnam($user); | 
| 39 | 0 | 0 |  |  |  |  | unless (defined $uid) { | 
| 40 | 0 |  |  |  |  |  | die qq{no such user "$user"\n}; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 0 |  |  |  |  |  | return $uid; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  | else { | 
| 46 | 0 |  |  |  |  |  | return $user; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub get_gid { | 
| 52 | 0 |  |  | 0 | 1 |  | my $group = shift; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 0 | 0 |  |  |  |  | unless ($group =~ /^\d+$/) { | 
| 55 | 0 |  |  |  |  |  | my $gid = getgrnam($group); | 
| 56 | 0 | 0 |  |  |  |  | unless (defined $gid) { | 
| 57 | 0 |  |  |  |  |  | die qq{no such group "$group"\n}; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 0 |  |  |  |  |  | return $gid; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  | else { | 
| 63 | 0 |  |  |  |  |  | return $group; | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub make_path { | 
| 69 | 0 |  |  | 0 | 1 |  | my $path = shift; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 0 | 0 |  |  |  |  | unless (-d $path) { | 
| 72 | 0 |  |  |  |  |  | File::Path::make_path($path, { mode => 0755 }); | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 0 |  |  |  |  |  | my $conf = App::Milter::Limit::Config->global; | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 0 | 0 |  |  |  |  | chown $$conf{user}, $$conf{group}, $path | 
| 78 |  |  |  |  |  |  | or die "chown($path): $!"; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | 1; | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | __END__ |