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
|
#!/usr/bin/env perl
# UDP Server
# Copyright 2025 Bob Proulx <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Ensure a standard behavior regardless of caller locale setting.
$ENV{'LC_ALL'} = "C";
use POSIX;
use IO::File;
use IO::Socket::INET6;
use Getopt::Long;
use strict;
use warnings FATAL => 'all';
use constant PORT => 9417;
use constant MAX_MSG_LEN => 4096;
my $progname = "git-repos-notify-catcher";
my $debug = 0;
my $verbose = 0;
my $version = '@VERSION@';
my $help = 0;
my $just_print = 0;
my $print_version = 0;
my $foreground = 0;
my %sessions; # hash of server sessions indexed by socket
sub sighandler {
my $sig = shift;
STDERR->print("$progname: signal $sig received, cleaning up.\n");
$SIG{$sig} = 'DEFAULT';
kill($sig,$$);
}
sub print_short_usage {
print $progname, ": Use --help to give you more information.\n";
}
sub print_help {
print <<'EOF';
Usage: git-repos-notify-catcher [options]
A test UDP server in perl.
--help print this help message
--version print program version
-d,--debug debug messages on
-v,--verbose verbose messages on
-f,--foreground run in foreground instead of background
Examples:
$ git-repos-notify-catcher
Report bugs to Bob Proulx <[email protected]>.
EOF
}
sub print_version {
print "$progname $version\n";
print <<'EOF';
Copyright (C) 2025 Bob Proulx.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Bob Proulx <[email protected]>.
EOF
}
sub process_options {
$Getopt::Long::bundling = 1;
$Getopt::Long::order = $REQUIRE_ORDER;
my %longopts = (
"d" => \$debug,
"debug" => \$debug,
"help" => \$help,
"v" => \$verbose,
"verbose" => \$verbose,
"version" => \$print_version,
"f" => \$foreground,
"foreground" => \$foreground,
);
if (!&GetOptions(%longopts)) {
warn $progname, ": invalid command line option\n";
&print_short_usage();
return undef;
}
if ($help) {
&print_help();
exit(0);
}
if ($print_version) {
&print_version();
exit(0);
}
return 1;
}
sub daemon {
chdir("/");
umask(02);
my $maxpid = 1024;
for (my $fd = 0; $fd <= $maxpid; ++$fd) {
POSIX::close($fd);
}
open(STDIN,"</dev/null");
open(STDOUT,">/dev/null");
open(STDERR,">&STDOUT");
#open(STDOUT,">>/var/tmp/git-repos-notify-catcher.out");
#open(STDERR,">&STDOUT");
POSIX::setsid() or die "setsid: $!\n";
my $pid = fork();
exit(0) if $pid;
}
sub main {
$SIG{'HUP'} = \&sighandler;
$SIG{'INT'} = \&sighandler;
$SIG{'QUIT'} = \&sighandler;
$SIG{'TERM'} = \&sighandler;
&process_options();
if (!$foreground) {
&daemon();
}
my $sock = IO::Socket::INET6->new(LocalPort => PORT,
Proto => 'udp')
or die $@;
my $msg;
for (;;) { # loop forever
if (!$sock->recv($msg,MAX_MSG_LEN)) {
next;
}
chomp($msg);
warn "INPUT: $msg\n" if $debug;
system("git-repos-updater",$msg) == 0
or warn "git-repos-updater: $?\n";
}
return 0;
}
if (__FILE__ eq $0) {
exit(&main(@ARGV));
}
1;
|