Skip to content

Commit 44e64cb

Browse files
committed
Add a switch to send log messages to syslog instead of stderr.
- Correct some exit codes. - #include <syslog.h> can't be removed since we use LOG_* defines. - syslog/openlog/closelog do not need any special libraries.
1 parent 84a36dd commit 44e64cb

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

proxy.c

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@
3737
#include <netdb.h>
3838
#include <resolv.h>
3939
#include <signal.h>
40+
#include <stdarg.h>
4041
#include <stdio.h>
4142
#include <stdlib.h>
4243
#include <string.h>
4344
#include <sys/socket.h>
45+
#include <syslog.h>
4446
#include <unistd.h>
4547
#include <wait.h>
4648
#ifdef USE_SYSTEMD
@@ -74,11 +76,13 @@ void forward_data(int source_sock, int destination_sock);
7476
void forward_data_ext(int source_sock, int destination_sock, char *cmd);
7577
int create_connection();
7678
int parse_options(int argc, char *argv[]);
79+
void plog(int priority, const char *format, ...);
7780

7881
int server_sock, client_sock, remote_sock, remote_port = 0;
7982
int connections_processed = 0;
8083
char *remote_host, *cmd_in, *cmd_out;
8184
bool foreground = FALSE;
85+
bool use_syslog = FALSE;
8286

8387
/* Program start */
8488
int main(int argc, char *argv[]) {
@@ -88,12 +92,15 @@ int main(int argc, char *argv[]) {
8892
local_port = parse_options(argc, argv);
8993

9094
if (local_port < 0) {
91-
printf("Syntax: %s -l local_port -h remote_host -p remote_port [-i \"input parser\"] [-o \"output parser\"] [-f (stay in foreground)]\n", argv[0]);
95+
printf("Syntax: %s -l local_port -h remote_host -p remote_port [-i \"input parser\"] [-o \"output parser\"] [-f (stay in foreground)] [-s (use syslog)]\n", argv[0]);
9296
return local_port;
9397
}
9498

99+
if (use_syslog)
100+
openlog("proxy", LOG_PID, LOG_DAEMON);
101+
95102
if ((server_sock = create_socket(local_port)) < 0) { // start server
96-
perror("Cannot run server");
103+
plog(LOG_CRIT, "Cannot run server: %m");
97104
return server_sock;
98105
}
99106

@@ -108,21 +115,24 @@ int main(int argc, char *argv[]) {
108115
server_loop();
109116
break;
110117
case -1: // error
111-
perror("Cannot daemonize");
118+
plog(LOG_CRIT, "Cannot daemonize: %m");
112119
return pid;
113120
default: // parent
114121
close(server_sock);
115122
}
116123
}
117124

125+
if (use_syslog)
126+
closelog();
127+
118128
return EXIT_SUCCESS;
119129
}
120130

121131
/* Parse command line options */
122132
int parse_options(int argc, char *argv[]) {
123133
int c, local_port = 0;
124134

125-
while ((c = getopt(argc, argv, "l:h:p:i:o:f")) != -1) {
135+
while ((c = getopt(argc, argv, "l:h:p:i:o:fs")) != -1) {
126136
switch(c) {
127137
case 'l':
128138
local_port = atoi(optarg);
@@ -141,6 +151,10 @@ int parse_options(int argc, char *argv[]) {
141151
break;
142152
case 'f':
143153
foreground = TRUE;
154+
break;
155+
case 's':
156+
use_syslog = TRUE;
157+
break;
144158
}
145159
}
146160

@@ -180,6 +194,24 @@ int create_socket(int port) {
180194
return server_sock;
181195
}
182196

197+
/* Send log message to stderr or syslog */
198+
void plog(int priority, const char *format, ...)
199+
{
200+
va_list ap;
201+
202+
va_start(ap, format);
203+
204+
if (use_syslog)
205+
vsyslog(priority, format, ap);
206+
else {
207+
vfprintf(stderr, format, ap);
208+
fprintf(stderr, "\n");
209+
}
210+
211+
va_end(ap);
212+
}
213+
214+
/* Update systemd status with connection count */
183215
void update_connection_count()
184216
{
185217
#ifdef USE_SYSTEMD
@@ -226,7 +258,7 @@ void server_loop() {
226258
void handle_client(int client_sock, struct sockaddr_in client_addr)
227259
{
228260
if ((remote_sock = create_connection()) < 0) {
229-
perror("Cannot connect to host");
261+
plog(LOG_ERR, "Cannot connect to host: %m");
230262
goto cleanup;
231263
}
232264

@@ -261,14 +293,14 @@ void forward_data(int source_sock, int destination_sock) {
261293
int buf_pipe[2];
262294

263295
if (pipe(buf_pipe) == -1) {
264-
perror("pipe");
265-
exit(EXIT_FAILURE);
296+
plog(LOG_ERR, "pipe: %m");
297+
exit(CREATE_PIPE_ERROR);
266298
}
267299

268300
while ((n = splice(source_sock, NULL, buf_pipe[WRITE], NULL, SSIZE_MAX, SPLICE_F_NONBLOCK|SPLICE_F_MOVE)) > 0) {
269301
if (splice(buf_pipe[READ], NULL, destination_sock, NULL, SSIZE_MAX, SPLICE_F_MOVE) < 0) {
270-
perror("write");
271-
exit(EXIT_FAILURE);
302+
plog(LOG_ERR, "write: %m");
303+
exit(BROKEN_PIPE_ERROR);
272304
}
273305
}
274306
#else
@@ -279,8 +311,10 @@ void forward_data(int source_sock, int destination_sock) {
279311
}
280312
#endif
281313

282-
if (n < 0)
283-
perror("read");
314+
if (n < 0) {
315+
plog(LOG_ERR, "read: %m");
316+
exit(BROKEN_PIPE_ERROR);
317+
}
284318

285319
#ifdef USE_SPLICE
286320
close(buf_pipe[0]);
@@ -300,7 +334,7 @@ void forward_data_ext(int source_sock, int destination_sock, char *cmd) {
300334
int n, i, pipe_in[2], pipe_out[2];
301335

302336
if (pipe(pipe_in) < 0 || pipe(pipe_out) < 0) { // create command input and output pipes
303-
perror("Cannot create pipe");
337+
plog(LOG_CRIT, "Cannot create pipe: %m");
304338
exit(CREATE_PIPE_ERROR);
305339
}
306340

@@ -317,7 +351,7 @@ void forward_data_ext(int source_sock, int destination_sock, char *cmd) {
317351

318352
while ((n = recv(source_sock, buffer, BUF_SIZE, 0)) > 0) { // read data from input socket
319353
if (write(pipe_in[WRITE], buffer, n) < 0) { // write data to input pipe of external command
320-
perror("Cannot write to pipe");
354+
plog(LOG_ERR, "Cannot write to pipe: %m");
321355
exit(BROKEN_PIPE_ERROR);
322356
}
323357
if ((i = read(pipe_out[READ], buffer, BUF_SIZE)) > 0) { // read command output

0 commit comments

Comments
 (0)