comparison svr-main.c @ 447:278805938dcf

Patch from Nicolai Ehemann to try binding before going to the background, so that if it exits early (because something's already listening etc) then it will return an exitcode of 1.
author Matt Johnston <matt@ucc.asn.au>
date Thu, 19 Jul 2007 15:54:18 +0000
parents edbee0596531
children 7e43f5e473b9
comparison
equal deleted inserted replaced
446:2cd2edfa11ee 447:278805938dcf
128 /* Note: commonsetup() must happen before we daemon()ise. Otherwise 128 /* Note: commonsetup() must happen before we daemon()ise. Otherwise
129 daemon() will chdir("/"), and we won't be able to find local-dir 129 daemon() will chdir("/"), and we won't be able to find local-dir
130 hostkeys. */ 130 hostkeys. */
131 commonsetup(); 131 commonsetup();
132 132
133 /* fork */
134 if (svr_opts.forkbg) {
135 int closefds = 0;
136 #ifndef DEBUG_TRACE
137 if (!svr_opts.usingsyslog) {
138 closefds = 1;
139 }
140 #endif
141 if (daemon(0, closefds) < 0) {
142 dropbear_exit("Failed to daemonize: %s", strerror(errno));
143 }
144 }
145
146 /* should be done after syslog is working */
147 if (svr_opts.forkbg) {
148 dropbear_log(LOG_INFO, "Running in background");
149 } else {
150 dropbear_log(LOG_INFO, "Not backgrounding");
151 }
152
153 /* create a PID file so that we can be killed easily */
154 pidfile = fopen(svr_opts.pidfile, "w");
155 if (pidfile) {
156 fprintf(pidfile, "%d\n", getpid());
157 fclose(pidfile);
158 }
159
160 /* sockets to identify pre-authenticated clients */ 133 /* sockets to identify pre-authenticated clients */
161 for (i = 0; i < MAX_UNAUTH_CLIENTS; i++) { 134 for (i = 0; i < MAX_UNAUTH_CLIENTS; i++) {
162 childpipes[i] = -1; 135 childpipes[i] = -1;
163 } 136 }
164 bzero(preauth_addrs, sizeof(preauth_addrs)); 137 bzero(preauth_addrs, sizeof(preauth_addrs));
166 /* Set up the listening sockets */ 139 /* Set up the listening sockets */
167 listensockcount = listensockets(listensocks, MAX_LISTEN_ADDR, &maxsock); 140 listensockcount = listensockets(listensocks, MAX_LISTEN_ADDR, &maxsock);
168 if (listensockcount == 0) 141 if (listensockcount == 0)
169 { 142 {
170 dropbear_exit("No listening ports available."); 143 dropbear_exit("No listening ports available.");
144 }
145
146 /* fork */
147 if (svr_opts.forkbg) {
148 int closefds = 0;
149 #ifndef DEBUG_TRACE
150 if (!svr_opts.usingsyslog) {
151 closefds = 1;
152 }
153 #endif
154 if (daemon(0, closefds) < 0) {
155 dropbear_exit("Failed to daemonize: %s", strerror(errno));
156 }
157 }
158
159 /* should be done after syslog is working */
160 if (svr_opts.forkbg) {
161 dropbear_log(LOG_INFO, "Running in background");
162 } else {
163 dropbear_log(LOG_INFO, "Not backgrounding");
164 }
165
166 /* create a PID file so that we can be killed easily */
167 pidfile = fopen(svr_opts.pidfile, "w");
168 if (pidfile) {
169 fprintf(pidfile, "%d\n", getpid());
170 fclose(pidfile);
171 } 171 }
172 172
173 /* incoming connection select loop */ 173 /* incoming connection select loop */
174 for(;;) { 174 for(;;) {
175 175