File: src___main.cc.patch

package info (click to toggle)
dhttpd 1.02a-18
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 256 kB
  • ctags: 55
  • sloc: cpp: 622; sh: 185; makefile: 43
file content (186 lines) | stat: -rw-r--r-- 3,949 bytes parent folder | download
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
174
175
176
177
178
179
180
181
182
183
184
185
186
--- a/src/main.cc
+++ b/src/main.cc
@@ -23,6 +23,8 @@
 #include <signal.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <syslog.h>
+#include <stdlib.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -31,9 +33,15 @@
 #include "socket.hh"
 #include "httpsock.hh"
 #include "version.hh"
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
 
 int numProc;
 
+const int ROOT_MAX_LEN = 500;
+char ROOT_DIR[ROOT_MAX_LEN] = WEBDIRPREFIX;
+
 void handleChildTerm( int )
 {
 	if( waitpid( 0, NULL, WNOHANG )>0 )
@@ -43,19 +51,14 @@
 	signal( SIGCHLD, handleChildTerm );
 }
 
-void spork()
+static void spork()
 {
 	struct passwd *pwent;
 	int user_id, group_id;
+
+	daemon(0,0);
 
-	chdir( "/" );
-
-	if( fork()!=0 )
-	{
-		exit( 0 );
-	}
-
-	setsid();
+	openlog("dhttpd",LOG_PID,LOG_DAEMON);
 
 	user_id = UID;
 	group_id = GID;
@@ -66,22 +69,27 @@
 		/* Check password file */
 		if ( (pwent = getpwuid( user_id ) ) == NULL)
 		{
+			syslog(LOG_ERR,"User with UID %d does not have an entry in /etc/passwd"
+				,user_id);
     			exit( 1 );
 		}
 
 		/* Reset groups attribute. */
 		if( initgroups( pwent->pw_name, group_id ) == -1 )
 		{
+			syslog(LOG_ERR,"Could not set up process groups (%m)");
 			exit( 1 );
 		}
 
 		/* Switch to our new user id.  We have to set the group first */
 		if( setgid( group_id )==-1 )
 		{
+			syslog(LOG_ERR,"Could not change GID to %d (%m)",group_id);
 			exit( 1 );
 		}
 		if( setuid( user_id )==-1 )
 		{
+			syslog(LOG_ERR,"Could not change UID to %d (%m)",user_id);
 			exit( 1 );
 		}
 	}
@@ -91,10 +99,12 @@
 {
 	int o;
 	int portnum = DEFAULTPORT;
+	char *bind_addr = DEFAULTBINDADDR;
+	bool nofork=false;
 
 	for( ;; )
 	{
-		o = getopt( argc, argv, "p:h" );
+		o = getopt( argc, argv, "p:b:hdr:" );
 		if( o==-1 )
 		{
 			break;
@@ -109,27 +119,59 @@
 			case 'h':
 				printf( "usage: %s [options]\n", argv[ 0 ] );
 				printf( "  -p (port)  Use a different port than the default of %i\n", DEFAULTPORT );
+				printf( "  -b         Bind to this address instead of %s\n", DEFAULTBINDADDR);
 				printf( "  -h         Help\n" );
+				printf( "  -d         Do not fork into Background on startup\n" );
 				return 0;
 
 			case 'p':
 				sscanf( optarg, "%i", &portnum );
 				break;
+			case 'b':
+				bind_addr = optarg;
+				break;
+			case 'r':
+			        if ( strlen(optarg) > ROOT_MAX_LEN )
+			        {
+				    //  Prevent overflows
+				    fprintf(stderr,
+					    "[ERROR] Too long WWW root path: %s\n"
+					    , optarg );
+
+				    exit(1);
+			        }
+
+			        if ( optarg[strlen(optarg) -1] == '/' )
+			        {
+				    fprintf(stderr,
+					    "[ERROR] No trailing slash allowed: %s\n"
+					    , optarg );
+				    exit(1);
+				}
+
+				sscanf( optarg, "%s", ROOT_DIR );
+				break;
+			case 'd':
+				nofork=true;
+				break;
 		}
 	}
 
-	ListenSocket listen( portnum );
+	ListenSocket listen_socket( portnum, bind_addr );
 	pid_t pid;
 	int s;
 
-	if( listen.geterror() )
+	if( listen_socket.geterror() )
 	{
-		fprintf( stderr, "Could not open port %i.  %s failed to start\n", portnum, DHTTPDVERSION );
+		fprintf( stderr, "Could not listen on port %i.  %s failed to start\n", portnum, DHTTPDVERSION );
 		exit( 1 );
 	}
 
-	/* now let's become a daemon */
-	spork();
+	if(!nofork)
+	{
+		/* now let's become a daemon */
+		spork();
+	}
 
 	signal( SIGCHLD, handleChildTerm );
 	signal( SIGHUP, SIG_IGN );
@@ -139,7 +181,7 @@
 	{
 		if( numProc<MAXCHILDPROC )
 		{
-			s = listen.newsock();
+			s = listen_socket.newsock();
 		}
 		else
 		{
@@ -154,7 +196,7 @@
 			if( pid==0 )
 			{
 				/* Child process (fulfill request) */
-				close( listen.getfd() );
+				close( listen_socket.getfd() );
 				
 				/* Handle Socket */ {
 					HttpSocket sock( s );
@@ -178,6 +220,9 @@
 				close( s );
 			}
 		}
+		/* In case signals are lost */
+		while( waitpid(0, NULL, WNOHANG)>0 )
+			numProc--;
 	}
 
 	/* make compiler happy */