Check-in [679bcc1373]
Overview
Comment:Fixed file descriptor leaking introduced in [b4fa45b6aa] and made connection re-use more clean
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:679bcc1373c006bfd0e3e17b4de5425e3064cd62
User & Date: rkeene on 2014-10-13 14:59:46
Other Links: manifest | tags
Context
2014-10-13
15:00
Updated version check-in: c261ffbca5 user: rkeene tags: trunk
14:59
Fixed file descriptor leaking introduced in [b4fa45b6aa] and made connection re-use more clean check-in: 679bcc1373 user: rkeene tags: trunk
08:12
Post-release version increment check-in: 7099ed9113 user: rkeene tags: trunk
Changes

Modified filed.c from [5391cbc0bc] to [390765784d].

   933    933   
   934    934   		log->buffer[0] = '\0';
   935    935   		log->http_code = -1;
   936    936   		log->reason = "fdopen_failed";
   937    937   
   938    938   		filed_log_entry(log);
   939    939   
   940         -		return(0);
          940  +		return(FILED_CONNECTION_CLOSE);
   941    941   	}
   942    942   
   943    943   	request = filed_get_http_request(fp, request, options);
   944    944   
   945    945   	if (request == NULL) {
   946    946   		log->buffer[0] = '\0';
   947    947   
   948    948   		filed_error_page(fp, date_current, 500, FILED_REQUEST_METHOD_GET, "format", log);
   949    949   
   950         -		return(0);
          950  +		return(FILED_CONNECTION_CLOSE);
   951    951   	}
   952    952   
   953    953   	path = request->path;
   954    954   	strcpy(log->buffer, path);
   955    955   	log->method = request->method;
   956    956   
   957    957   	/* If the requested path is a directory, redirect to index page */
   958    958   	if (request->type == FILED_REQUEST_TYPE_DIRECTORY) {
   959    959   		filed_redirect_index(fp, date_current, path, log);
   960    960   
   961         -		return(0);
          961  +		return(FILED_CONNECTION_CLOSE);
   962    962   	}
   963    963   
   964    964   	fileinfo = filed_open_file(path, &request->fileinfo);
   965    965   	if (fileinfo == NULL) {
   966    966   		filed_error_page(fp, date_current, 404, request->method, "open_failed", log);
   967    967   
   968         -		return(0);
          968  +		return(FILED_CONNECTION_CLOSE);
   969    969   	}
   970    970   
   971    971   	if (request->headers.range.present) {
   972    972   		if (request->headers.range.offset != 0 || request->headers.range.length >= 0) {
   973    973   			if (request->headers.range.offset >= fileinfo->len) {
   974    974   				filed_error_page(fp, date_current, 416, request->method, "range_invalid", log);
   975    975   
   976    976   				close(fileinfo->fd);
   977    977   
   978         -				return(0);
          978  +				return(FILED_CONNECTION_CLOSE);
   979    979   			}
   980    980   
   981    981   			if (request->headers.range.length == ((off_t) -1)) {
   982    982   				filed_log_msg_debug("Computing length to fit in bounds: fileinfo->len = %llu, request->headers.range.offset = %llu",
   983    983   					(unsigned long long) fileinfo->len,
   984    984   					(unsigned long long) request->headers.range.offset
   985    985   				);
................................................................................
  1095   1095   		}
  1096   1096   	}
  1097   1097   
  1098   1098   	log->endtime = (time_t) -1;
  1099   1099   	log->sent_length = sendfile_sent;
  1100   1100   
  1101   1101   	filed_log_entry(log);
         1102  +
         1103  +	close(fileinfo->fd);
  1102   1104   
  1103   1105   	if (request->headers.connection != FILED_CONNECTION_KEEP_ALIVE) {
  1104         -		close(fileinfo->fd);
  1105         -
  1106   1106   		fclose(fp);
  1107   1107   
  1108         -		return(0);
         1108  +		return(FILED_CONNECTION_CLOSE);
  1109   1109   	}
  1110   1110   
  1111         -	return(1);
         1111  +	return(FILED_CONNECTION_KEEP_ALIVE);
  1112   1112   }
  1113   1113   
  1114   1114   /* Handle incoming connections */
  1115   1115   static void *filed_worker_thread(void *arg_v) {
  1116   1116   	struct filed_worker_thread_args *arg;
  1117   1117   	struct filed_http_request request;
  1118   1118   	struct filed_log_entry *log, local_dummy_log;
  1119   1119   	struct filed_options *options;
  1120   1120   	struct sockaddr_in6 addr;
  1121   1121   	socklen_t addrlen;
  1122   1122   	int failure_count = 0, max_failure_count = FILED_MAX_FAILURE_COUNT;
  1123         -	int accept_new = 1;
         1123  +	int connection_state = FILED_CONNECTION_CLOSE;
  1124   1124   	int master_fd, fd = -1;
  1125   1125   
  1126   1126   	/* Read arguments */
  1127   1127   	arg = arg_v;
  1128   1128   
  1129   1129   	master_fd = arg->fd;
  1130   1130   	options = &arg->options;
................................................................................
  1141   1141   			filed_log_msg("ALLOCATE_LOG_MSG_FAILED");
  1142   1142   
  1143   1143   			break;
  1144   1144   		}
  1145   1145   
  1146   1146   		log->type = FILED_LOG_TYPE_TRANSFER;
  1147   1147   
  1148         -		/* Accept a new client */
  1149         -		if (accept_new) {
         1148  +		/* If we closed the old connection, accept a new one */
         1149  +		if (connection_state == FILED_CONNECTION_CLOSE) {
         1150  +			/* Accept a new client */
  1150   1151   			addrlen = sizeof(addr);
         1152  +
  1151   1153   			fd = accept(master_fd, (struct sockaddr *) &addr, &addrlen);
  1152   1154   		}
  1153   1155   
  1154   1156   		/*
  1155   1157   		 * If we fail, make a note of it so we don't go into a loop of
  1156   1158   		 * accept() failing
  1157   1159   		 */
................................................................................
  1174   1176   			log->port = addr.sin6_port;
  1175   1177   		}
  1176   1178   
  1177   1179   		/* Reset failure count*/
  1178   1180   		failure_count = 0;
  1179   1181   
  1180   1182   		/* Handle socket */
  1181         -		accept_new = !filed_handle_client(fd, &request, log, options);
         1183  +		connection_state = filed_handle_client(fd, &request, log, options);
  1182   1184   	}
  1183   1185   
  1184   1186   	/* Report error */
  1185   1187   	filed_log_msg("THREAD_DIED ABNORMAL");
  1186   1188   
  1187   1189   	return(NULL);
  1188   1190