1 /*	$Id$ */
2 /*
3  * Copyright (c) 2015 Kristaps Dzonsons <kristaps@bsd.lv>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #include "../config.h"
18 
19 #include <sys/stat.h>
20 
21 #include <fcntl.h>
22 #include <paths.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 #include "../kcgi.h"
31 #include "../extern.h"
32 
33 int
main(int argc,char * argv[])34 main(int argc, char *argv[])
35 {
36 	int	 	 fdout, fdin;
37 	enum kcgi_err	 kerr;
38 	struct stat	 st;
39 	char		 buf[1024];
40 
41 	if (2 != argc)
42 		return(EXIT_FAILURE);
43 
44 	if (-1 == (fdout = open(_PATH_DEVNULL, O_RDWR, 0))) {
45 		perror(_PATH_DEVNULL);
46 		return(EXIT_FAILURE);
47 	} else if (-1 == (fdin = open(argv[1], O_RDONLY, 0))) {
48 		perror(argv[1]);
49 		close(fdout);
50 		return(EXIT_FAILURE);
51 	} else if (-1 == fstat(fdin, &st)) {
52 		perror(argv[1]);
53 		close(fdout);
54 		close(fdin);
55 		return(EXIT_FAILURE);
56 	} else if (KCGI_OK != kxsocketprep(fdin)) {
57 		perror(argv[1]);
58 		close(fdout);
59 		close(fdin);
60 		return(EXIT_FAILURE);
61 	} else if (-1 == dup2(fdin, STDIN_FILENO)) {
62 		perror(argv[1]);
63 		close(fdout);
64 		close(fdin);
65 		return(EXIT_FAILURE);
66 	}
67 
68 	snprintf(buf, sizeof(buf), "%llu",
69 		(unsigned long long)st.st_size);
70 	setenv("CONTENT_TYPE", "text/plain", 1);
71 	setenv("REQUEST_METHOD", "post", 1);
72 	setenv("CONTENT_LENGTH", buf, 1);
73 	kerr = kworker_child(fdout, NULL, 0, kmimetypes, KMIME__MAX, 0);
74 	close(fdin);
75 	close(fdout);
76 	return(KCGI_OK == kerr ? EXIT_SUCCESS : EXIT_FAILURE);
77 }
78