/[hydra]/hydra/src/sublog.c
ViewVC logotype

Contents of /hydra/src/sublog.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Sat Sep 21 13:53:54 2002 UTC (19 years, 2 months ago) by nmav
Branch: boas
CVS Tags: BOAS_WITH_RANGES_AND_CGI, start, hydra_0_0_2, hydra_0_0_3, hydra_0_0_6, hydra_0_0_7, hydra_0_0_4, hydra_0_0_5
Changes since 1.1: +0 -0 lines
File MIME type: text/plain
Imported sources

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.6 2002/03/24 22:40:31 jnelson 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
36 int open_pipe_fd(char *command);
37 int open_net_fd(char *spec);
38 int open_gen_fd(char *spec);
39
40 /* Like popen, but gives fd instead of FILE * */
41 int open_pipe_fd(char *command)
42 {
43 int pipe_fds[2];
44 int pid;
45 /* "man pipe" says "filedes[0] is for reading,
46 * filedes[1] is for writing. */
47 if (pipe(pipe_fds) == -1)
48 return -1;
49 pid = fork();
50 if (pid == 0) { /* child */
51 close(pipe_fds[1]);
52 if (pipe_fds[0] != 0) {
53 dup2(pipe_fds[0], 0);
54 close(pipe_fds[0]);
55 }
56 execl("/bin/sh", "sh", "-c", command, (char *) 0);
57 exit(127);
58 }
59 close(pipe_fds[0]);
60 if (pid < 0) {
61 close(pipe_fds[1]);
62 return -1;
63 }
64 return pipe_fds[1];
65 }
66
67 int open_net_fd(char *spec)
68 {
69 char *p;
70 int fd, port;
71 struct sockaddr_in sa;
72 struct hostent *he;
73 p = strchr(spec, ':');
74 if (!p)
75 return -1;
76 *p++ = '\0';
77 port = strtol(p, NULL, 10);
78 /* printf("Host %s, port %d\n",spec,port); */
79 sa.sin_family = AF_INET;
80 sa.sin_port = htons(port);
81 he = gethostbyname(spec);
82 if (!he) {
83 herror("open_net_fd");
84 return -1;
85 }
86 memcpy(&sa.sin_addr, he->h_addr, he->h_length);
87 /* printf("using ip %s\n",inet_ntoa(sa.sin_addr)); */
88 fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
89 if (fd < 0)
90 return fd;
91 if (connect(fd, (struct sockaddr *) &sa, sizeof (sa)) < 0)
92 return -1;
93 return fd;
94 }
95
96 int open_gen_fd(char *spec)
97 {
98 int fd;
99 if (*spec == '|') {
100 fd = open_pipe_fd(spec + 1);
101 } else if (*spec == ':') {
102 fd = open_net_fd(spec + 1);
103 } else {
104 fd = open(spec,
105 O_WRONLY | O_CREAT | O_APPEND,
106 S_IRUSR | S_IWUSR | S_IROTH | S_IRGRP);
107 }
108 return fd;
109 }
110
111 #ifdef STANDALONE_TEST
112 int main(int argc, char *argv[])
113 {
114 char buff[1024];
115 int fd, nr, nw;
116 if (argc < 2) {
117 fprintf(stderr,
118 "usage: %s output-filename\n"
119 " %s |output-command\n"
120 " %s :host:port\n", argv[0], argv[0], argv[0]);
121 return 1;
122 }
123 fd = open_gen_fd(argv[1]);
124 if (fd < 0) {
125 perror("open_gen_fd");
126 exit(1);
127 }
128 while ((nr = read(0, buff, sizeof (buff))) != 0) {
129 if (nr < 0) {
130 if (errno == EINTR)
131 continue;
132 perror("read");
133 exit(1);
134 }
135 nw = write(fd, buff, nr);
136 if (nw < 0) {
137 perror("write");
138 exit(1);
139 }
140 }
141 return 0;
142 }
143 #endif

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26