- port corosync write() check
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
lib/util.c | 52 ++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 44 insertions(+), 8 deletions(-)
diff --git a/lib/util.c b/lib/util.c
index 336b4a9..0452354 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -248,6 +248,10 @@ int32_t qb_util_mmap_file_open(char *path, const char *file, size_t
bytes,
uint32_t file_flags)
{
int32_t fd;
+ int32_t i;
+ int32_t res = 0;
+ ssize_t written;
+ char *buffer = NULL;
char *is_absolute = strchr(file, '/');;
if (is_absolute) {
@@ -257,25 +261,57 @@ int32_t qb_util_mmap_file_open(char *path, const char *file, size_t
bytes,
}
fd = open_mmap_file(path, file_flags);
if (fd < 0 && !is_absolute) {
+ res = -errno;
qb_util_log(LOG_ERR, "couldn't open file %s error: %s",
- path, strerror(errno));
+ path, strerror(-res));
snprintf(path, PATH_MAX, LOCALSTATEDIR "/run/%s", file);
fd = open_mmap_file(path, file_flags);
if (fd < 0) {
+ res = -errno;
qb_util_log(LOG_ERR, "couldn't open file %s error: %s",
- path, strerror(errno));
- return -errno;
+ path, strerror(-res));
+ return res;
}
}
- if (fd >= 0) {
- ftruncate(fd, bytes);
- } else {
- qb_util_log(LOG_ERR, "couldn't open file %s error: %s",
- path, strerror(errno));
+ if (ftruncate(fd, bytes) == -1) {
+ res = -errno;
+ qb_util_log(LOG_ERR,
+ "couldn't truncate file %s error: %s",
+ path, strerror(-res));
+ goto unlink_exit;
}
+
+
+ if (file_flags & O_CREAT) {
+ long page_size = sysconf(_SC_PAGESIZE);
+ buffer = calloc(1, page_size);
+ if (buffer == NULL) {
+ goto unlink_exit;
+ }
+ for (i = 0; i < (bytes / page_size); i++) {
+retry_write:
+ written = write (fd, buffer, page_size);
+ if (written == -1 && errno == EINTR) {
+ goto retry_write;
+ }
+ if (written != page_size) {
+ free (buffer);
+ goto unlink_exit;
+ }
+ }
+ free (buffer);
+ }
+
return fd;
+
+unlink_exit:
+ unlink (path);
+ if (fd > 0) {
+ close (fd);
+ }
+ return res;
}
int32_t qb_util_circular_mmap(int32_t fd, void **buf, size_t bytes)
--
1.7.3.1
Show replies by date
this just saves the user from doing this.
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
lib/ringbuffer.c | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/lib/ringbuffer.c b/lib/ringbuffer.c
index 2fa44b3..bc57a7d 100644
--- a/lib/ringbuffer.c
+++ b/lib/ringbuffer.c
@@ -198,6 +198,9 @@ cleanup_hdr:
void qb_rb_close(qb_ringbuffer_t * rb)
{
+ if (rb == NULL) {
+ return;
+ }
qb_util_log(LOG_DEBUG,
"Destroying ringbuffer: %s",
rb->shared_hdr->hdr_path);
--
1.7.3.1
also add a test for under running.
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
include/tlist.h | 4 +++-
tests/check_loop.c | 8 +++++++-
2 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/include/tlist.h b/include/tlist.h
index 3600013..a005f81 100644
--- a/include/tlist.h
+++ b/include/tlist.h
@@ -32,6 +32,7 @@ typedef void *timer_handle;
#define TIMER_HANDLE
#endif
+static int64_t timerlist_hertz = HZ;
struct timerlist {
struct qb_list_head timer_head;
@@ -50,6 +51,7 @@ struct timerlist_timer {
static inline void timerlist_init(struct timerlist *timerlist)
{
qb_list_init(&timerlist->timer_head);
+ timerlist_hertz = qb_util_nano_monotonic_hz();
}
static inline void timerlist_add(struct timerlist *timerlist,
@@ -208,7 +210,7 @@ static inline uint64_t timerlist_msec_duration_to_expire(struct
timerlist *timer
msec_duration_to_expire =
((timer_from_list->expire_time -
- current_time) / QB_TIME_NS_IN_MSEC) + (1000 / HZ);
+ current_time) / QB_TIME_NS_IN_MSEC) + (1000 / timerlist_hertz);
return (msec_duration_to_expire);
}
diff --git a/tests/check_loop.c b/tests/check_loop.c
index f34e026..18edbcc 100644
--- a/tests/check_loop.c
+++ b/tests/check_loop.c
@@ -212,11 +212,17 @@ static void stop_watch_tmo(void*data)
{
struct qb_stop_watch *sw = (struct qb_stop_watch *)data;
float per;
+ int64_t diff;
sw->end = qb_util_nano_current_get();
- sw->total += sw->end - sw->start;
+
+ diff = sw->end - sw->start;
+ ck_assert(diff >= (sw->ms_timer * QB_TIME_NS_IN_MSEC));
+
+ sw->total += diff;
sw->total -= sw->ms_timer * QB_TIME_NS_IN_MSEC;
sw->start = sw->end;
+
sw->count++;
if (sw->count < 50) {
qb_loop_timer_add(sw->l, QB_LOOP_LOW, sw->ms_timer, data, stop_watch_tmo,
&sw->th);
--
1.7.3.1