]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
Replace all use of write() with tvh_write().
authorAdam Sutton <dev@adamsutton.me.uk>
Sun, 30 Dec 2012 19:55:46 +0000 (19:55 +0000)
committerAdam Sutton <dev@adamsutton.me.uk>
Mon, 31 Dec 2012 21:21:45 +0000 (21:21 +0000)
tvh_write() will deal with incomplete/interrupted writes and will ensure
the write operation is completed unless a fatal error occurs.

src/capmt.c
src/cwc.c
src/dvb/dvb_adapter.c
src/epgdb.c
src/htsp_server.c
src/settings.c
src/v4l.c

index 6da676cdc00e6ee3e10aa209fe5cad7b1626a946..304bdd2397ab00e18f46d8e81c0b8bf7c95446ae 100644 (file)
@@ -227,7 +227,7 @@ static int
 capmt_send_msg(capmt_t *capmt, int sid, const uint8_t *buf, size_t len)
 {
   if (capmt->capmt_oscam) {
-    int i, sent = 0;
+    int i;
 
     // dumping current SID table
     for (i = 0; i < MAX_SOCKETS; i++)
@@ -280,18 +280,17 @@ capmt_send_msg(capmt_t *capmt, int sid, const uint8_t *buf, size_t len)
         tvhlog(LOG_DEBUG, "capmt", "created socket with socket_fd=%d", capmt->capmt_sock[i]);
     }
     if (capmt->capmt_sock[i] > 0) {
-      sent = write(capmt->capmt_sock[i], buf, len);
-      tvhlog(LOG_DEBUG, "capmt", "socket_fd=%d len=%d sent=%d", capmt->capmt_sock[i], (int)len, sent);
-      if (sent != len) {
-        tvhlog(LOG_ERR, "capmt", "%s: len != sent", __FUNCTION__);
+      if (tvh_write(capmt->capmt_sock[i], buf, len)) {
+        tvhlog(LOG_DEBUG, "capmt", "socket_fd=%d send failed", capmt->capmt_sock[i]);
         close(capmt->capmt_sock[i]);
         capmt->capmt_sock[i] = 0;
+        return -1;
       }
     }
-    return sent;
   }
   else  // standard old capmt mode
-    return write(capmt->capmt_sock[0], buf, len);
+    tvh_write(capmt->capmt_sock[0], buf, len);
+  return 0;
 }
 
 static void 
index 79c0745262bd47fb82c217210e47d158fe717b4b..d8733ec7baff273a2697368f8ae94bfc93ca8643 100644 (file)
--- a/src/cwc.c
+++ b/src/cwc.c
@@ -477,7 +477,7 @@ cwc_send_msg(cwc_t *cwc, const uint8_t *msg, size_t len, int sid, int enq)
 {
   cwc_message_t *cm = malloc(sizeof(cwc_message_t));
   uint8_t *buf = cm->cm_data;
-  int seq, n;
+  int seq;
 
   if(len + 12 > CWS_NETMSGSIZE) {
     free(cm);
@@ -513,8 +513,7 @@ cwc_send_msg(cwc_t *cwc, const uint8_t *msg, size_t len, int sid, int enq)
     pthread_cond_signal(&cwc->cwc_writer_cond);
     pthread_mutex_unlock(&cwc->cwc_writer_mutex);
   } else {
-    n = write(cwc->cwc_fd, buf, len);
-    if(n != len)
+    if (tvh_write(cwc->cwc_fd, buf, len))
       tvhlog(LOG_INFO, "cwc", "write error %s", strerror(errno));
 
     free(cm);
@@ -1032,7 +1031,8 @@ cwc_writer_thread(void *aux)
       TAILQ_REMOVE(&cwc->cwc_writeq, cm, cm_link);
       pthread_mutex_unlock(&cwc->cwc_writer_mutex);
       //      int64_t ts = getmonoclock();
-      r = write(cwc->cwc_fd, cm->cm_data, cm->cm_len);
+      if (tvh_write(cwc->cwc_fd, cm->cm_data, cm->cm_len))
+        tvhlog(LOG_INFO, "cwc", "write error %s", strerror(errno));
       //      printf("Write took %lld usec\n", getmonoclock() - ts);
       free(cm);
       pthread_mutex_lock(&cwc->cwc_writer_mutex);
index 21b9cab643e103f7a21bf72617d557fdc4758ad8..75681b5efd8d2bd64f46583a5f5b974b3e07d41c 100644 (file)
@@ -623,8 +623,8 @@ dvb_adapter_stop ( th_dvb_adapter_t *tda )
   /* Stop DVR thread */
   if (tda->tda_dvr_pipe.rd != -1) {
     tvhlog(LOG_DEBUG, "dvb", "%s stopping thread", tda->tda_rootpath);
-    int err = write(tda->tda_dvr_pipe.wr, "", 1);
-    assert(err != -1);
+    int err = tvh_write(tda->tda_dvr_pipe.wr, "", 1);
+    assert(!err);
     pthread_join(tda->tda_dvr_thread, NULL);
     close(tda->tda_dvr_pipe.rd);
     close(tda->tda_dvr_pipe.wr);
index f56e5982c54e27e06736ea0553b4b9fd78e6f5ce..33e531430817328a98e8859a9b8c76292e5846fe 100644 (file)
@@ -241,9 +241,8 @@ static int _epg_write ( int fd, htsmsg_t *m )
     int r = htsmsg_binary_serialize(m, &msgdata, &msglen, 0x10000);
     htsmsg_destroy(m);
     if (!r) {
-      ssize_t w = write(fd, msgdata, msglen);
+      ret = tvh_write(fd, msgdata, msglen);
       free(msgdata);
-      if(w == msglen) ret = 0;
     }
   } else {
     ret = 0;
index b84768e035067dc62207ce32967199b10e78230e..ebbb07a22d8fe1005acbd402312f069fe9393476 100644 (file)
@@ -1669,7 +1669,6 @@ static void *
 htsp_write_scheduler(void *aux)
 {
   htsp_connection_t *htsp = aux;
-  int r;
   htsp_msg_q_t *hmq;
   htsp_msg_t *hm;
   void *dptr;
@@ -1706,33 +1705,21 @@ htsp_write_scheduler(void *aux)
 
     pthread_mutex_unlock(&htsp->htsp_out_mutex);
 
-    r = htsmsg_binary_serialize(hm->hm_msg, &dptr, &dlen, INT32_MAX);
+    if (htsmsg_binary_serialize(hm->hm_msg, &dptr, &dlen, INT32_MAX) != 0) {
+      tvhlog(LOG_WARNING, "htsp", "%s: failed to serialize data",
+             htsp->htsp_logname);
+    }
 
     htsp_msg_destroy(hm);
 
-    void *freeme = dptr;
-
-    while(dlen > 0) {
-      r = write(htsp->htsp_fd, dptr, dlen);
-      if(r < 0) {
-        if(errno == EAGAIN || errno == EWOULDBLOCK)
-          continue;
-        tvhlog(LOG_INFO, "htsp", "%s: Write error -- %s",
-               htsp->htsp_logname, strerror(errno));
-        break;
-      }
-      if(r == 0) {
-        tvhlog(LOG_ERR, "htsp", "%s: write() returned 0",
-               htsp->htsp_logname);
-      }
-      dptr += r;
-      dlen -= r;
+    if (tvh_write(htsp->htsp_fd, dptr, dlen)) {
+      tvhlog(LOG_INFO, "htsp", "%s: Write error -- %s",
+             htsp->htsp_logname, strerror(errno));
+      break;
     }
 
-    free(freeme);
+    free(dptr);
     pthread_mutex_lock(&htsp->htsp_out_mutex);
-    if(dlen)
-      break;
   }
   // Shutdown socket to make receive thread terminate entire HTSP connection
 
index 2f1ab07714613e70ddbbe1fa841a6f3f263016e0..c98fc2fdee31cfe2585675b261f0cf8febd7440f 100644 (file)
@@ -158,8 +158,7 @@ hts_settings_save(htsmsg_t *record, const char *pathfmt, ...)
   htsbuf_queue_init(&hq, 0);
   htsmsg_json_serialize(record, &hq, 1);
   TAILQ_FOREACH(hd, &hq.hq_q, hd_link)
-    if(write(fd, hd->hd_data + hd->hd_data_off, hd->hd_data_len) != 
-       hd->hd_data_len) {
+    if(tvh_write(fd, hd->hd_data + hd->hd_data_off, hd->hd_data_len)) {
       tvhlog(LOG_ALERT, "settings", "Failed to write file \"%s\" - %s",
              tmppath, strerror(errno));
       ok = 0;
index a7b86075837ed71dca003e20b811b6807bc209bb..a640a38797f029a708b82c79feeb4a625c40e7b3 100644 (file)
--- a/src/v4l.c
+++ b/src/v4l.c
@@ -253,7 +253,7 @@ v4l_service_stop(service_t *t)
 
   assert(va->va_current_service != NULL);
 
-  if(write(va->va_pipe[1], &c, 1) != 1)
+  if(tvh_write(va->va_pipe[1], &c, 1))
     tvhlog(LOG_ERR, "v4l", "Unable to close video thread -- %s",
           strerror(errno));