diff options
author | Luo Jinghua <sunmoon1997@gmail.com> | 2009-09-13 15:03:58 +0800 |
---|---|---|
committer | Luo Jinghua <sunmoon1997@gmail.com> | 2009-09-13 15:03:58 +0800 |
commit | b4a9ddbe0ecfa3427f2bc4a5c0598700065d443f (patch) | |
tree | a76e6b3e5da3e3f91b59ae8b4609053f32eeb16e | |
parent | 2f58c4dd67115a7159122a169b580c54a0efc8e7 (diff) |
sopcastsrc: initialize some field and don't close fd 0 by accident
-rw-r--r-- | src/gstscsrc.c | 91 |
1 files changed, 48 insertions, 43 deletions
diff --git a/src/gstscsrc.c b/src/gstscsrc.c index 27e6b94..5582796 100644 --- a/src/gstscsrc.c +++ b/src/gstscsrc.c @@ -176,6 +176,8 @@ gst_sopcast_src_init (GstSopcastSrc * src, GstSopcastSrcClass * g_class) src->location = NULL; src->request_position = 0; src->percent = 0; + src->sock = -1; + src->heatebeat_fd = -1; } static void @@ -659,9 +661,6 @@ gst_sopcast_src_subproc_main (GstSopcastSrc * src, pid_t ppid, int sock) int i, max, ret; char buf[1024]; - if (!gst_sopcast_src_spawn_helper (src)) - goto goodbye; - max = sysconf(_SC_OPEN_MAX); for (i = 3; i < max; i++) if (i != sock) @@ -670,6 +669,9 @@ gst_sopcast_src_subproc_main (GstSopcastSrc * src, pid_t ppid, int sock) signal (SIGHUP, SIG_IGN); signal (SIGINT, SIG_IGN); + if (!gst_sopcast_src_spawn_helper (src)) + goto goodbye; + while (1) { int error; @@ -730,7 +732,7 @@ goodbye: send (sock, "bye", 3, 0); close (sock); - _exit (0); + exit (0); } static gboolean @@ -738,32 +740,29 @@ gst_sopcast_src_spawn_sopcast (GstSopcastSrc * src) { pid_t ppid, pid; int ret; - int fds[2]; + int socks[2]; - ret = socketpair (AF_UNIX, SOCK_STREAM, 0, fds); - if (ret < 0) + ret = socketpair (AF_UNIX, SOCK_STREAM, 0, socks); + if (ret != 0) return FALSE; - GST_DEBUG_OBJECT (src, "socketpair() = (%d, %d)", fds[0], fds[1]); + GST_DEBUG_OBJECT (src, "socketpair() = (%d, %d)", socks[0], socks[1]); ppid = getpid (); pid = fork (); if (pid == 0) { - close (fds[0]); - gst_sopcast_src_subproc_main (src, ppid, fds[1]); - } else if (pid > 0) { - close (fds[1]); - - GST_DEBUG_OBJECT (src, "child %d heatebeat fd = %d", pid, fds[0]); - src->heatebeat_fd = fds[0]; - src->helper_pid = pid; - return TRUE; - } else { - close (fds[0]); - close (fds[1]); + gst_sopcast_src_subproc_main (src, ppid, socks[1]); + } else if (pid < 0) { + close (socks[0]); + close (socks[1]); return FALSE; } + GST_DEBUG_OBJECT (src, "child %d heatebeat fd = %d", pid, socks[0]); + src->heatebeat_fd = socks[0]; + src->helper_pid = pid; + + close (socks[1]); return TRUE; } @@ -782,7 +781,7 @@ gst_sopcast_src_start (GstBaseSrc * bsrc) src->heatebeat_fd = -1; if (!gst_sopcast_src_spawn_sopcast (src)) { - GST_ERROR_OBJECT (src, "Coudn't spawn the helper."); + GST_ERROR_OBJECT (src, "Coudn't fork subprocess."); return FALSE; } @@ -803,35 +802,41 @@ gst_sopcast_src_stop (GstBaseSrc * bsrc) src = GST_SOPCAST_SRC (bsrc); GST_DEBUG_OBJECT (src, "stop()"); - close (src->sock); - src->sock = -1; - GST_DEBUG_OBJECT (src, "say goodbye to subprocess %d", src->helper_pid); - GST_DEBUG_OBJECT (src, "heatebeat fd = %d", src->heatebeat_fd); - do { - errno = 0; - ret = send (src->heatebeat_fd, "bye", 3, 0); - } while (ret < 0 && errno == EINTR); - GST_DEBUG_OBJECT (src, "ret %d %s", ret, strerror (errno)); + if (src->heatebeat_fd >= 0) { + GST_DEBUG_OBJECT (src, "heatebeat fd = %d", src->heatebeat_fd); + do { + errno = 0; + ret = send (src->heatebeat_fd, "bye", 3, 0); + } while (ret < 0 && errno == EINTR); + GST_DEBUG_OBJECT (src, "ret %d", ret); - do { - errno = 0; - ret = recv (src->heatebeat_fd, buf, 3, 0); - } while (ret < 0 && errno == EINTR); - GST_DEBUG_OBJECT (src, "ret %d %s", ret, strerror (errno)); + do { + errno = 0; + ret = recv (src->heatebeat_fd, buf, 3, 0); + } while (ret < 0 && errno == EINTR); + GST_DEBUG_OBJECT (src, "ret %d", ret); - close (src->heatebeat_fd); - src->heatebeat_fd = -1; + close (src->heatebeat_fd); + src->heatebeat_fd = -1; + } - GST_DEBUG_OBJECT (src, "killing subprocess %d", src->helper_pid); - kill (src->helper_pid, SIGINT); - kill (src->helper_pid, SIGTERM); - kill (src->helper_pid, SIGKILL); + GST_DEBUG_OBJECT (src, "fd = %d", src->sock); + if (src->sock >= 0) { + close (src->sock); + src->sock = -1; + } - waitpid (src->helper_pid, &status, 0); + if (src->helper_pid != 0) { + GST_DEBUG_OBJECT (src, "killing subprocess %d", src->helper_pid); + kill (src->helper_pid, SIGINT); + kill (src->helper_pid, SIGTERM); + kill (src->helper_pid, SIGKILL); - src->helper_pid = 0; + waitpid (src->helper_pid, &status, 0); + src->helper_pid = 0; + } GST_DEBUG_OBJECT (src, "subprocess is killed"); |