]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
Handle too-short write(3)s in mod_json_cdr
authorTravis Cross <tc@traviscross.com>
Sun, 2 Mar 2014 22:14:51 +0000 (22:14 +0000)
committerTravis Cross <tc@traviscross.com>
Mon, 3 Mar 2014 01:27:30 +0000 (01:27 +0000)
write(3) can write fewer bytes than was requested for any number of
reasons.  The correct behavior is to retry unless there is an error.

If there is an error, try to unlink the file; no sense in leaving
corrupted data laying around.

src/mod/event_handlers/mod_json_cdr/mod_json_cdr.c

index ebb14566d9cdc82f393bd85d30b9c4301143b6b3..6cb42447b207ad667e78171b7b5ef6202dcbc86b 100644 (file)
@@ -244,12 +244,15 @@ static switch_status_t my_on_reporting(switch_core_session_t *session)
 #else
                        if ((fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) > -1) {
 #endif
-                               int wrote;
-                               wrote = write(fd, json_text, (unsigned) strlen(json_text));
-                               close(fd);
-                               fd = -1;
-                               if(wrote < 0) {
+                               switch_size_t json_len = strlen(json_text);
+                               switch_ssize_t wrote = 0, x;
+                               do { x = write(fd, json_text, json_len);
+                               } while (!(x<0) && json_len > (wrote += x));
+                               close(fd); fd = -1;
+                               if (x < 0) {
                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error writing [%s]\n",path);
+                                       if (0 > unlink(path))
+                                               switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error unlinking [%s]\n",path);
                                }
                        } else {
                                char ebuf[512] = { 0 };