diff options
author | Luo Jinghua <sunmoon1997@gmail.com> | 2009-09-13 10:45:55 +0800 |
---|---|---|
committer | Luo Jinghua <sunmoon1997@gmail.com> | 2009-09-13 10:45:55 +0800 |
commit | 79b1649dbf7cbf53c214b9cacea7db9171d6aaa8 (patch) | |
tree | f6814bf30f2a8a627823f324a19133296aa36099 | |
parent | 5b446132bfb821106ce32a90539fc58e7475dbdb (diff) |
sopcastsrc: say goodbye to subprocess on stop0.0.1
-rw-r--r-- | src/gstscsrc.c | 49 |
1 files changed, 39 insertions, 10 deletions
diff --git a/src/gstscsrc.c b/src/gstscsrc.c index 61f7f6a..1d4b4f8 100644 --- a/src/gstscsrc.c +++ b/src/gstscsrc.c @@ -403,6 +403,7 @@ gst_sopcast_src_helper_is_running (GstSopcastSrc *src) if (ret == 0) return TRUE; + perror ("helper is not running"); GST_ERROR_OBJECT (src, "helper is not running."); return FALSE; } @@ -656,9 +657,10 @@ gst_sopcast_src_subproc_main (GstSopcastSrc * src, pid_t ppid, int sock) fd_set efs; struct timeval tv; int i, max, ret; + char buf[1024]; if (!gst_sopcast_src_spawn_helper (src)) - exit (-1); + _exit (-1); max = sysconf(_SC_OPEN_MAX); for (i = 3; i < max; i++) @@ -667,6 +669,7 @@ gst_sopcast_src_subproc_main (GstSopcastSrc * src, pid_t ppid, int sock) signal (SIGHUP, SIG_IGN); signal (SIGINT, SIG_IGN); + while (1) { int error; @@ -675,8 +678,9 @@ gst_sopcast_src_subproc_main (GstSopcastSrc * src, pid_t ppid, int sock) break; } - if (!gst_sopcast_src_helper_is_running (src)) + if (!gst_sopcast_src_helper_is_running (src)) { break; + } errno = 0; @@ -695,16 +699,37 @@ gst_sopcast_src_subproc_main (GstSopcastSrc * src, pid_t ppid, int sock) continue; if (ret < 0 && error == EINTR) continue; - break; + + if (FD_ISSET (sock, &efs)) { + GST_DEBUG_OBJECT (src, "player closed the sock."); + break; + } + + if (FD_ISSET (sock, &rfs)) { + do { + ret = recv (sock, buf, sizeof (buf), 0); + } while (ret < 0 && errno == EINTR); + if (ret < 0 || buf[0] == 'b') { + GST_DEBUG_OBJECT (src, "player said goodbye to me."); + break; + } + } } - GST_DEBUG_OBJECT (src, "killing sp-sc(%d)", src->helper_pid); + + GST_DEBUG_OBJECT (src, "killing sp-sc(%d).", src->helper_pid); kill (src->helper_pid, SIGINT); kill (src->helper_pid, SIGTERM); kill (src->helper_pid, SIGKILL); g_spawn_close_pid (src->helper_pid); - exit (0); + GST_DEBUG_OBJECT (src, "sp-sc is killed."); + + GST_DEBUG_OBJECT (src, "say goodbye back to player.", ppid); + send (sock, "bye", 3, 0); + close (sock); + + _exit (0); } static gboolean @@ -721,7 +746,7 @@ gst_sopcast_src_spawn_sopcast (GstSopcastSrc * src) ppid = getpid (); pid = fork (); if (pid == 0) { - /* fds[0] will be closed later */ + close (fds[0]); gst_sopcast_src_subproc_main (src, ppid, fds[1]); } else if (pid > 0) { close (fds[1]); @@ -766,6 +791,7 @@ gst_sopcast_src_stop (GstBaseSrc * bsrc) { GstSopcastSrc *src; int status; + char buf[12]; src = GST_SOPCAST_SRC (bsrc); GST_DEBUG_OBJECT (src, "stop()"); @@ -773,13 +799,14 @@ gst_sopcast_src_stop (GstBaseSrc * bsrc) close (src->socks[0]); src->socks[0] = -1; - send (src->heatebeat_fd, "quit", 4, 0); + GST_DEBUG_OBJECT (src, "say goodbye to subprocess %d", src->helper_pid); + + send (src->heatebeat_fd, "bye", 3, 0); + recv (src->heatebeat_fd, buf, 3, 0); close (src->heatebeat_fd); src->heatebeat_fd = -1; - g_usleep (500000); - - GST_DEBUG_OBJECT (src, "killing helper %d", src->helper_pid); + 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); @@ -788,6 +815,8 @@ gst_sopcast_src_stop (GstBaseSrc * bsrc) src->helper_pid = 0; + GST_DEBUG_OBJECT (src, "subprocess is killed"); + return TRUE; } |