forked from vysheng/tg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-telegram-daemon
executable file
·206 lines (170 loc) · 5.17 KB
/
start-telegram-daemon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/perl -w
# start-engine
# derived from start-memcached
# 2003/2004 - Jay Bonci <[email protected]>
# This script handles the parsing of the /etc/memcached.conf file
# and was originally created for the Debian distribution.
# Anyone may use this little script under the same terms as
# memcached itself.
# a bit changed it for telegram-daemon to generate config before start (Vitaly Valtman)
use POSIX qw(strftime);
use strict;
my $id = defined($ARGV[0]) ? $ARGV[0] : '';
my $idx = $id eq '' ? '' : " '$id'";
my $idl = ($id =~ /^\.(.*)$/ ? "-$1" : $id);
my $root = "";
my $params; my $etchandle; my $etcfile = "$root/etc/telegram-daemon/telegram-daemon$id.conf";
# This script assumes that engine is located at /usr/bin/engine, and
# that the pidfile is writable at /var/run/engine.pid
my $engine_pfx = "$root/usr/share/telegram-daemon/bin/";
my $pidfile = "$root/var/run/telegram-daemon$id.pid";
# If we don't get a valid logfile parameter in the /etc/engine.conf file,
# we'll just throw away all of our in-daemon output. We need to re-tie it so
# that non-bash shells will not hang on logout. Thanks to Michael Renner for
# the tip
my $fd_reopened = "/dev/null";
$fd_reopened = "$root/var/log/telegram-daemon/telegram-daemon$idl.log" if -d "$root/var/log/telegram-daemon/";
sub handle_logfile
{
my ($logfile) = @_;
$logfile = "$root/var/log/telegram-daemon/$logfile" unless $logfile =~ /\//;
$fd_reopened = $logfile;
}
sub reopen_logfile
{
my ($logfile) = @_;
open *STDERR, ">>$logfile";
open *STDOUT, ">>$logfile";
open *STDIN, ">>/dev/null";
chown 239, 239, $logfile;
$fd_reopened = $logfile;
}
sub adjust_arg
{
my ($arg) = @_;
if ($arg =~ /^newest:(.*)$/) {
my @L = split /:/, $1;
my $x = $arg;
my $xt = 0;
for my $y (@L) {
my @S = stat($y);
if (scalar @S && $S[9] > $xt) {
$x = $y;
$xt = $S[9];
}
}
return $x;
}
return $arg;
}
# This is set up in place here to support other non -[a-z] directives
my $conf_directives = {
"logfile" => \&handle_logfile,
};
my %Vars = (
"execute" => "msg-search-engine",
"work_dir" => "$root/var/lib/telegram-daemon"
);
my $have_c = 0;
my $have_l = 0;
my $have_u = 0;
my $have_aes = 0;
if(open $etchandle, $etcfile)
{
foreach my $line (<$etchandle>)
{
$line ||= "";
$line =~ s/\#.*//g;
$line =~ s/\s+$//g;
$line =~ s/^\s+//g;
next unless $line;
next if $line =~ /^\-[h]/;
if($line =~ /^[^\-]/)
{
my ($directive, $int, $arg) = $line =~ /^(.*?)(\s+|\s*=\s*)(.*)/;
next unless $directive;
if (exists $conf_directives->{$directive}) {
$conf_directives->{$directive}->($arg);
} else {
$Vars{$directive} = $arg;
}
next;
}
$have_l = 1 if $line =~ /^-L\s/;
$have_c = 1 if $line =~ /^-c\s/;
$have_u = 1 if $line =~ /^-U\s/;
push @$params, $line;
}
}else{
$params = [];
}
push @$params, "-U telegramd" unless($have_u);
push @$params, "-L $fd_reopened" unless($have_l);
push @$params, "-c config$idl" unless($have_c);
$params = join " ", @$params;
if(-e $pidfile)
{
open PIDHANDLE, "$pidfile";
my $localpid = <PIDHANDLE>;
close PIDHANDLE;
if ($localpid && $localpid =~ /(\d+)/) { $localpid = $1; } else { $localpid = -1; }
if (-d "/proc/$localpid")
{
print STDERR "telegram-daemon$idl is already running.\n";
exit;
} else {
print STDERR "removing stale $pidfile.\n";
`rm -f $pidfile`;
}
}
if (exists $Vars{'quit'}) {
print STDERR "telegram-daemon$idl disabled\n";
exit(0);
}
my $engine = $engine_pfx.$Vars{'execute'};
my $wdir = $Vars{'work_dir'};
chdir $wdir if -d $wdir;
unless (-x $engine) {
print STDERR "executable $engine not found\n";
exit(1);
}
unless (-d $wdir) {
print STDERR "work directory $wdir not found\n";
exit(1);
}
for my $x ('s', 0 .. 9) {
if (defined($Vars{"arg$x"})) {
$params .= " ".adjust_arg($Vars{"arg$x"});
}
}
my $pid = fork();
if (!$pid) {
reopen_logfile ($fd_reopened);
chdir $wdir;
open(my $fh, '>', "config$idl");
my $text = <<"END_MSG"
config_directory="$wdir";
test=false;
msg_num=true;
binlog_enabled=true;
binlog="binlog$idl.bin";
pfs_enabled=true;
log_level = 0;
lua_script="script$idl.lua";
END_MSG
;
print $fh $text;
close $fh;
my $t;
$t = strftime ("%Y-%m-%d %H:%M:%S %Z", localtime);
print STDERR "[$t] invoking telegram-daemon$idl: $engine $params\n";
exec "$engine $params";
exit (0);
} else {
if (open PIDHANDLE,">$pidfile") {
print PIDHANDLE $pid;
close PIDHANDLE;
} else {
print STDERR "Can't write pidfile to $pidfile.\n";
}
}