1 |
/* |
2 |
* Boa, an http server |
3 |
* Copyright (C) 1999 Larry Doolittle <ldoolitt@boa.org> |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* it under the terms of the GNU General Public License as published by |
7 |
* the Free Software Foundation; either version 1, or (at your option) |
8 |
* any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 |
* |
19 |
*/ |
20 |
|
21 |
/* $Id: sublog.c,v 1.1.1.1 2002/09/21 13:53:54 nmav Exp $*/ |
22 |
|
23 |
#include <errno.h> |
24 |
#include <stdio.h> |
25 |
#include <stdlib.h> |
26 |
#include <string.h> |
27 |
#include <netdb.h> |
28 |
#include <unistd.h> |
29 |
#include <fcntl.h> |
30 |
#include <sys/types.h> |
31 |
#include <sys/stat.h> |
32 |
#include <sys/socket.h> |
33 |
#include <netinet/in.h> |
34 |
#include <arpa/inet.h> |
35 |
#include "compat.h" |
36 |
|
37 |
int open_pipe_fd(char *command); |
38 |
int open_net_fd(char *spec); |
39 |
int open_gen_fd(char *spec); |
40 |
|
41 |
/* Like popen, but gives fd instead of FILE * */ |
42 |
int open_pipe_fd(char *command) |
43 |
{ |
44 |
int pipe_fds[2]; |
45 |
int pid; |
46 |
/* "man pipe" says "filedes[0] is for reading, |
47 |
* filedes[1] is for writing. */ |
48 |
if (pipe(pipe_fds) == -1) |
49 |
return -1; |
50 |
pid = fork(); |
51 |
if (pid == 0) { /* child */ |
52 |
close(pipe_fds[1]); |
53 |
if (pipe_fds[0] != 0) { |
54 |
dup2(pipe_fds[0], 0); |
55 |
close(pipe_fds[0]); |
56 |
} |
57 |
execl("/bin/sh", "sh", "-c", command, (char *) 0); |
58 |
exit(127); |
59 |
} |
60 |
close(pipe_fds[0]); |
61 |
if (pid < 0) { |
62 |
close(pipe_fds[1]); |
63 |
return -1; |
64 |
} |
65 |
return pipe_fds[1]; |
66 |
} |
67 |
|
68 |
int open_net_fd(char *spec) |
69 |
{ |
70 |
char *p; |
71 |
int fd, port; |
72 |
struct sockaddr_in sa; |
73 |
struct hostent *he; |
74 |
p = strchr(spec, ':'); |
75 |
if (!p) |
76 |
return -1; |
77 |
*p++ = '\0'; |
78 |
port = strtol(p, NULL, 10); |
79 |
/* printf("Host %s, port %d\n",spec,port); */ |
80 |
sa.sin_family = AF_INET; |
81 |
sa.sin_port = htons(port); |
82 |
he = gethostbyname(spec); |
83 |
if (!he) { |
84 |
#ifdef HAVE_HERROR |
85 |
herror("open_net_fd"); |
86 |
#endif |
87 |
return -1; |
88 |
} |
89 |
memcpy(&sa.sin_addr, he->h_addr, he->h_length); |
90 |
/* printf("using ip %s\n",inet_ntoa(sa.sin_addr)); */ |
91 |
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
92 |
if (fd < 0) |
93 |
return fd; |
94 |
if (connect(fd, (struct sockaddr *) &sa, sizeof (sa)) < 0) |
95 |
return -1; |
96 |
return fd; |
97 |
} |
98 |
|
99 |
int open_gen_fd(char *spec) |
100 |
{ |
101 |
int fd; |
102 |
if (*spec == '|') { |
103 |
fd = open_pipe_fd(spec + 1); |
104 |
} else if (*spec == ':') { |
105 |
fd = open_net_fd(spec + 1); |
106 |
} else { |
107 |
fd = open(spec, |
108 |
O_WRONLY | O_CREAT | O_APPEND, |
109 |
S_IRUSR | S_IWUSR | S_IROTH | S_IRGRP); |
110 |
} |
111 |
return fd; |
112 |
} |
113 |
|
114 |
#ifdef STANDALONE_TEST |
115 |
int main(int argc, char *argv[]) |
116 |
{ |
117 |
char buff[1024]; |
118 |
int fd, nr, nw; |
119 |
if (argc < 2) { |
120 |
fprintf(stderr, |
121 |
"usage: %s output-filename\n" |
122 |
" %s |output-command\n" |
123 |
" %s :host:port\n", argv[0], argv[0], argv[0]); |
124 |
return 1; |
125 |
} |
126 |
fd = open_gen_fd(argv[1]); |
127 |
if (fd < 0) { |
128 |
perror("open_gen_fd"); |
129 |
exit(1); |
130 |
} |
131 |
while ((nr = read(0, buff, sizeof (buff))) != 0) { |
132 |
if (nr < 0) { |
133 |
if (errno == EINTR) |
134 |
continue; |
135 |
perror("read"); |
136 |
exit(1); |
137 |
} |
138 |
nw = write(fd, buff, nr); |
139 |
if (nw < 0) { |
140 |
perror("write"); |
141 |
exit(1); |
142 |
} |
143 |
} |
144 |
return 0; |
145 |
} |
146 |
#endif |