winlin

fix #518, fix fd leak bug when fork. 2.0.200

@@ -336,6 +336,7 @@ Remark: @@ -336,6 +336,7 @@ Remark:
336 336
337 ## History 337 ## History
338 338
  339 +* v2.0, 2015-11-16, for [#518][bug #518] fix fd leak bug when fork. 2.0.200
339 * v2.0, 2015-11-05, for [#511][bug #511] fix bug for restart thread. 2.0.199 340 * v2.0, 2015-11-05, for [#511][bug #511] fix bug for restart thread. 2.0.199
340 * v2.0, 2015-11-02, for [#515][bug #515] use srs_freepa and SrsAutoFreeA for array. 2.0.198 341 * v2.0, 2015-11-02, for [#515][bug #515] use srs_freepa and SrsAutoFreeA for array. 2.0.198
341 * v2.0, 2015-10-28, for [ExoPlayer #828][exo #828], remove duration for live. 342 * v2.0, 2015-10-28, for [ExoPlayer #828][exo #828], remove duration for live.
@@ -1203,6 +1204,7 @@ Winlin @@ -1203,6 +1204,7 @@ Winlin
1203 [bug #512]: https://github.com/ossrs/srs/issues/512 1204 [bug #512]: https://github.com/ossrs/srs/issues/512
1204 [bug #515]: https://github.com/ossrs/srs/issues/515 1205 [bug #515]: https://github.com/ossrs/srs/issues/515
1205 [bug #511]: https://github.com/ossrs/srs/issues/511 1206 [bug #511]: https://github.com/ossrs/srs/issues/511
  1207 +[bug #518]: https://github.com/ossrs/srs/issues/518
1206 [bug #xxxxxxxxxx]: https://github.com/ossrs/srs/issues/xxxxxxxxxx 1208 [bug #xxxxxxxxxx]: https://github.com/ossrs/srs/issues/xxxxxxxxxx
1207 1209
1208 [exo #828]: https://github.com/google/ExoPlayer/pull/828 1210 [exo #828]: https://github.com/google/ExoPlayer/pull/828
@@ -1223,10 +1223,11 @@ void SrsServer::resample_kbps() @@ -1223,10 +1223,11 @@ void SrsServer::resample_kbps()
1223 int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd) 1223 int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd)
1224 { 1224 {
1225 int ret = ERROR_SUCCESS; 1225 int ret = ERROR_SUCCESS;
  1226 +
1226 int fd = st_netfd_fileno(client_stfd); 1227 int fd = st_netfd_fileno(client_stfd);
  1228 +
1227 int max_connections = _srs_config->get_max_connections(); 1229 int max_connections = _srs_config->get_max_connections();
1228 if ((int)conns.size() >= max_connections) { 1230 if ((int)conns.size() >= max_connections) {
1229 -  
1230 srs_error("exceed the max connections, drop client: " 1231 srs_error("exceed the max connections, drop client: "
1231 "clients=%d, max=%d, fd=%d", (int)conns.size(), max_connections, fd); 1232 "clients=%d, max=%d, fd=%d", (int)conns.size(), max_connections, fd);
1232 1233
@@ -1234,20 +1235,26 @@ int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd) @@ -1234,20 +1235,26 @@ int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd)
1234 1235
1235 return ret; 1236 return ret;
1236 } 1237 }
1237 - int val;  
1238 - if ((val = fcntl(fd, F_GETFD, 0)) < 0) {  
1239 - ret = ERROR_SYSTEM_PID_GET_FILE_INFO;  
1240 - srs_error("fnctl F_GETFD error! fd=%d. ret=%#x", fd, ret);  
1241 - srs_close_stfd(client_stfd);  
1242 - return ret; 1238 +
  1239 + // avoid fd leak when fork.
  1240 + // @see https://github.com/ossrs/srs/issues/518
  1241 + if (true) {
  1242 + int val;
  1243 + if ((val = fcntl(fd, F_GETFD, 0)) < 0) {
  1244 + ret = ERROR_SYSTEM_PID_GET_FILE_INFO;
  1245 + srs_error("fnctl F_GETFD error! fd=%d. ret=%#x", fd, ret);
  1246 + srs_close_stfd(client_stfd);
  1247 + return ret;
  1248 + }
  1249 + val |= FD_CLOEXEC;
  1250 + if (fcntl(fd, F_SETFD, val) < 0) {
  1251 + ret = ERROR_SYSTEM_PID_SET_FILE_INFO;
  1252 + srs_error("fcntl F_SETFD error! fd=%d ret=%#x", fd, ret);
  1253 + srs_close_stfd(client_stfd);
  1254 + return ret;
  1255 + }
1243 } 1256 }
1244 - val |= FD_CLOEXEC;  
1245 - if (fcntl(fd, F_SETFD, val) < 0) {  
1246 - ret = ERROR_SYSTEM_PID_SET_FILE_INFO;  
1247 - srs_error("fcntl F_SETFD error! fd=%d ret=%#x", fd, ret);  
1248 - srs_close_stfd(client_stfd);  
1249 - return ret;  
1250 - } 1257 +
1251 SrsConnection* conn = NULL; 1258 SrsConnection* conn = NULL;
1252 if (type == SrsListenerRtmpStream) { 1259 if (type == SrsListenerRtmpStream) {
1253 conn = new SrsRtmpConn(this, client_stfd); 1260 conn = new SrsRtmpConn(this, client_stfd);
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR 2 32 #define VERSION_MAJOR 2
33 #define VERSION_MINOR 0 33 #define VERSION_MINOR 0
34 -#define VERSION_REVISION 199 34 +#define VERSION_REVISION 200
35 35
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "SRS" 37 #define RTMP_SIG_SRS_KEY "SRS"