提交 95e4163a authored 作者: Travis Cross's avatar Travis Cross

Handle too-short write(3)s in mod_json_cdr

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.
上级 b04bbc6c
...@@ -213,11 +213,15 @@ static void backup_cdr(cdr_data_t *data) ...@@ -213,11 +213,15 @@ static void backup_cdr(cdr_data_t *data)
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
#endif #endif
if ((fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode)) > -1) { if ((fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode)) > -1) {
int wrote = write(fd, json_text, (unsigned) strlen(json_text)); switch_size_t json_len = strlen(json_text);
close(fd); switch_ssize_t wrote = 0, x;
fd = -1; do { x = write(fd, json_text, json_len);
if (wrote < 0) { } while (!(x<0) && json_len > (wrote += x));
close(fd); fd = -1;
if (x < 0) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(data->uuid), SWITCH_LOG_ERROR, "Error writing [%s]\n",path); switch_log_printf(SWITCH_CHANNEL_UUID_LOG(data->uuid), SWITCH_LOG_ERROR, "Error writing [%s]\n",path);
if (0 > unlink(path))
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(data->uuid), SWITCH_LOG_ERROR, "Error unlinking [%s]\n",path);
} }
switch_safe_free(path); switch_safe_free(path);
break; break;
...@@ -273,11 +277,15 @@ static void process_cdr(cdr_data_t *data) ...@@ -273,11 +277,15 @@ static void process_cdr(cdr_data_t *data)
#else #else
if ((fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) > -1) { if ((fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) > -1) {
#endif #endif
int wrote = write(fd, data->json_text, (unsigned) strlen(data->json_text)); switch_size_t json_len = strlen(data->json_text);
close(fd); switch_ssize_t wrote = 0, x;
fd = -1; do { x = write(fd, data->json_text, json_len);
if (wrote < 0) { } while (!(x<0) && json_len > (wrote += x));
close(fd); fd = -1;
if (x < 0) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(data->uuid), SWITCH_LOG_ERROR, "Error writing [%s]\n",path); switch_log_printf(SWITCH_CHANNEL_UUID_LOG(data->uuid), SWITCH_LOG_ERROR, "Error writing [%s]\n",path);
if (0 > unlink(path))
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(data->uuid), SWITCH_LOG_ERROR, "Error unlinking [%s]\n",path);
} }
} else { } else {
char ebuf[512] = { 0 }; char ebuf[512] = { 0 };
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论