got rid of all uses of strerror
This required some minor refactoring of other things and there were some
other small cleanups I noticed along the way.
diff --git a/aos/linux_code/ipc_lib/aos_sync.c b/aos/linux_code/ipc_lib/aos_sync.c
index 2245436..204322f 100644
--- a/aos/linux_code/ipc_lib/aos_sync.c
+++ b/aos/linux_code/ipc_lib/aos_sync.c
@@ -1,6 +1,5 @@
#include "aos/linux_code/ipc_lib/aos_sync.h"
-#include <stdio.h>
#include <linux/futex.h>
#include <unistd.h>
#include <sys/syscall.h>
@@ -10,6 +9,8 @@
#include <string.h>
#include <inttypes.h>
+#include "aos/common/logging/logging.h"
+
// TODO(brians): Inline these in the new PI version.
#define cmpxchg(ptr, o, n) __sync_val_compare_and_swap(ptr, o, n)
static inline uint32_t xchg(mutex *pointer, uint32_t value) {
@@ -151,31 +152,21 @@
void mutex_unlock(mutex *m) {
/* Unlock, and if not contended then exit. */
- //printf("mutex_unlock(%p) => %d \n",m,*m);
switch (xchg(m, 0)) {
case 0:
- fprintf(stderr, "sync: multiple unlock of %p. aborting\n", m);
- printf("see stderr\n");
- abort();
+ LOG(FATAL, "multiple unlock of %p\n", m);
case 1:
- //printf("mutex_unlock return(%p) => %d \n",m,*m);
break;
case 2: {
const int ret = sys_futex_wake(m, 1);
if (ret < 0) {
- fprintf(stderr, "sync: waking 1 from %p failed with %d: %s\n",
- m, -ret, strerror(-ret));
- printf("see stderr\n");
- abort();
+ PELOG(FATAL, -ret, "waking 1 from %p failed", m);
} else {
break;
}
}
default:
- fprintf(stderr, "sync: got a garbage value from mutex %p. aborting\n",
- m);
- printf("see stderr\n");
- abort();
+ LOG(FATAL, "got a garbage value from mutex %p\n", m);
}
}
int mutex_trylock(mutex *m) {
@@ -232,11 +223,8 @@
if (__builtin_expect(*c == wait_start, 0)) {
// Try again if it was because of a signal.
if (ret == -EINTR) continue;
- fprintf(stderr, "FUTEX_WAIT(%p, %"PRIu32", NULL, NULL, 0) failed"
- " with %d: %s\n",
- c, wait_start, -ret, strerror(-ret));
- printf("see stderr\n");
- abort();
+ PELOG(FATAL, -ret, "FUTEX_WAIT(%p, %" PRIu32 ", NULL, NULL, 0) failed",
+ c, wait_start);
}
}
// Relock the mutex now that we're done waiting.
@@ -251,11 +239,7 @@
// Try again if it was because of a signal or somebody else unlocked it
// before we went to sleep.
if (ret == -EINTR || ret == -EWOULDBLOCK) continue;
- fprintf(stderr, "sync: FUTEX_WAIT(%p, 2, NULL, NULL, 0)"
- " failed with %d: %s\n",
- m, -ret, strerror(-ret));
- printf("see stderr\n");
- abort();
+ PELOG(FATAL, -ret, "FUTEX_WAIT(%p, 2, NULL, NULL, 0) failed", m);
}
}
return;
@@ -270,11 +254,7 @@
// Wake at most 1 person who is waiting in the kernel.
const int ret = sys_futex_wake(c, 1);
if (ret < 0) {
- fprintf(stderr, "sync: FUTEX_WAKE(%p, 1, NULL, NULL, 0)"
- " failed with %d: %s\n",
- c, -ret, strerror(-ret));
- printf("see stderr\n");
- abort();
+ PELOG(FATAL, -ret, "FUTEX_WAKE(%p, 1, NULL, NULL, 0) failed", c);
}
}
@@ -285,10 +265,6 @@
// mutex anyways.
const int ret = sys_futex_requeue(c, 1, INT_MAX, m);
if (ret < 0) {
- fprintf(stderr, "sync: FUTEX_REQUEUE(%p, 1, INT_MAX, %p, 0)"
- " failed with %d: %s\n",
- c, m, -ret, strerror(-ret));
- printf("see stderr\n");
- abort();
+ PELOG(FATAL, -ret, "FUTEX_REQUEUE(%p, 1, INT_MAX, %p, 0) failed", c, m);
}
}
diff --git a/aos/linux_code/ipc_lib/ipc_lib.gyp b/aos/linux_code/ipc_lib/ipc_lib.gyp
index fe8b2e0..9b1e712 100644
--- a/aos/linux_code/ipc_lib/ipc_lib.gyp
+++ b/aos/linux_code/ipc_lib/ipc_lib.gyp
@@ -6,6 +6,9 @@
'sources': [
'aos_sync.c',
],
+ 'dependencies': [
+ '<(AOS)/build/aos.gyp:logging_interface',
+ ],
},
{
'target_name': 'core_lib',
@@ -29,6 +32,7 @@
],
'dependencies': [
'aos_sync',
+ '<(AOS)/build/aos.gyp:logging_interface',
],
'export_dependent_settings': [
'aos_sync',
diff --git a/aos/linux_code/ipc_lib/ipc_stress_test.cc b/aos/linux_code/ipc_lib/ipc_stress_test.cc
index 3067a20..c1dcb83 100644
--- a/aos/linux_code/ipc_lib/ipc_stress_test.cc
+++ b/aos/linux_code/ipc_lib/ipc_stress_test.cc
@@ -82,20 +82,16 @@
void __attribute__((noreturn)) DoRunTest(
Shared *shared, const char *(*test)[kTestMaxArgs], int pipes[2]) {
if (close(pipes[0]) == -1) {
- Die("close(%d) of read end of pipe failed with %d: %s\n",
- pipes[0], errno, strerror(errno));
+ PDie("close(%d) of read end of pipe failed", pipes[0]);
}
if (close(STDIN_FILENO) == -1) {
- Die("close(STDIN_FILENO(=%d)) failed with %d: %s\n",
- STDIN_FILENO, errno, strerror(errno));
+ PDie("close(STDIN_FILENO(=%d)) failed", STDIN_FILENO);
}
if (dup2(pipes[1], STDOUT_FILENO) == -1) {
- Die("dup2(%d, STDOUT_FILENO(=%d)) failed with %d: %s\n",
- pipes[1], STDOUT_FILENO, errno, strerror(errno));
+ PDie("dup2(%d, STDOUT_FILENO(=%d)) failed", pipes[1], STDOUT_FILENO);
}
if (dup2(pipes[1], STDERR_FILENO) == -1) {
- Die("dup2(%d, STDERR_FILENO(=%d)) failed with %d: %s\n",
- pipes[1], STDERR_FILENO, errno, strerror(errno));
+ PDie("dup2(%d, STDERR_FILENO(=%d)) failed", pipes[1], STDERR_FILENO);
}
size_t size = kTestMaxArgs;
@@ -120,8 +116,7 @@
}
args[size] = NULL;
execv(executable.c_str(), const_cast<char *const *>(args));
- Die("execv(%s, %p) failed with %d: %s\n",
- executable.c_str(), args, errno, strerror(errno));
+ PDie("execv(%s, %p) failed", executable.c_str(), args);
}
void DoRun(Shared *shared) {
@@ -133,18 +128,17 @@
int pipes[2];
while (time::Time::Now() < shared->stop_time) {
if (pipe(pipes) == -1) {
- Die("pipe(%p) failed with %d: %s\n", &pipes, errno, strerror(errno));
+ PDie("pipe(%p) failed", &pipes);
}
switch (fork()) {
case 0: // in runner
DoRunTest(shared, test, pipes);
case -1:
- Die("fork() failed with %d: %s\n", errno, strerror(errno));
+ PDie("fork() failed");
}
if (close(pipes[1]) == -1) {
- Die("close(%d) of write end of pipe failed with %d: %s\n",
- pipes[1], errno, strerror(errno));
+ PDie("close(%d) of write end of pipe failed", pipes[1]);
}
::std::string output;
@@ -153,13 +147,11 @@
ssize_t ret = read(pipes[0], &buffer, sizeof(buffer));
if (ret == 0) { // EOF
if (close(pipes[0]) == -1) {
- Die("close(%d) of pipe at EOF failed with %d: %s\n",
- pipes[0], errno, strerror(errno));
+ PDie("close(%d) of pipe at EOF failed", pipes[0]);
}
break;
} else if (ret == -1) {
- Die("read(%d, %p, %zd) failed with %d: %s\n",
- pipes[0], &buffer, sizeof(buffer), errno, strerror(errno));
+ PDie("read(%d, %p, %zd) failed", pipes[0], &buffer, sizeof(buffer));
}
output += ::std::string(buffer, ret);
}
@@ -168,8 +160,7 @@
while (true) {
if (wait(&status) == -1) {
if (errno == EINTR) continue;
- Die("wait(%p) in child failed with %d: %s\n",
- &status, errno, strerror(errno));
+ PDie("wait(%p) in child failed", &status);
} else {
break;
}
@@ -207,7 +198,7 @@
DoRun(shared);
_exit(EXIT_SUCCESS);
case -1:
- Die("fork() of child failed with %d: %s\n", errno, strerror(errno));
+ PDie("fork() of child failed");
}
}
@@ -222,7 +213,7 @@
char *temp = strdup(argv[0]);
if (asprintf(const_cast<char **>(&shared->path),
"%s/../tests", dirname(temp)) == -1) {
- Die("asprintf failed with %d: %s\n", errno, strerror(errno));
+ PDie("asprintf failed");
}
free(temp);
@@ -237,7 +228,7 @@
if (errno == EINTR) {
--i;
} else {
- Die("wait(%p) failed with %d: %s\n", &status, errno, strerror(errno));
+ PDie("wait(%p) failed", &status);
}
}
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
diff --git a/aos/linux_code/ipc_lib/mutex.cpp b/aos/linux_code/ipc_lib/mutex.cpp
index 47fc92a..8c98204 100644
--- a/aos/linux_code/ipc_lib/mutex.cpp
+++ b/aos/linux_code/ipc_lib/mutex.cpp
@@ -1,7 +1,6 @@
#include "aos/common/mutex.h"
#include <inttypes.h>
-#include <errno.h>
#include <stdio.h>
#include <string.h>
@@ -20,8 +19,7 @@
void Mutex::Lock() {
if (mutex_grab(&impl_) != 0) {
- LOG(FATAL, "mutex_grab(%p(=%" PRIu32 ")) failed because of %d: %s\n",
- &impl_, impl_, errno, strerror(errno));
+ PLOG(FATAL, "mutex_grab(%p(=%" PRIu32 ")) failed", &impl_, impl_);
}
}
diff --git a/aos/linux_code/ipc_lib/raw_queue_test.cc b/aos/linux_code/ipc_lib/raw_queue_test.cc
index 67b687a..4196286 100644
--- a/aos/linux_code/ipc_lib/raw_queue_test.cc
+++ b/aos/linux_code/ipc_lib/raw_queue_test.cc
@@ -89,8 +89,7 @@
if (errno == ESRCH) {
printf("process %jd was already dead\n", static_cast<intmax_t>(pid_));
} else {
- fprintf(stderr, "kill(SIGKILL, %jd) failed with %d: %s\n",
- static_cast<intmax_t>(pid_), errno, strerror(errno));
+ PLOG(FATAL, "kill(SIGKILL, %jd) failed", static_cast<intmax_t>(pid_));
}
return;
}
@@ -174,7 +173,7 @@
mutex_unlock(lock);
exit(EXIT_SUCCESS);
case -1: // parent failure
- LOG(ERROR, "fork() failed with %d: %s\n", errno, strerror(errno));
+ PLOG(ERROR, "fork() failed");
return std::unique_ptr<ForkedProcess>();
default: // parent
return std::unique_ptr<ForkedProcess>(new ForkedProcess(pid, lock));
diff --git a/aos/linux_code/ipc_lib/shared_mem.c b/aos/linux_code/ipc_lib/shared_mem.c
index c368e38..a132233 100644
--- a/aos/linux_code/ipc_lib/shared_mem.c
+++ b/aos/linux_code/ipc_lib/shared_mem.c
@@ -10,6 +10,7 @@
#include <stdlib.h>
#include "aos/linux_code/ipc_lib/core_lib.h"
+#include "aos/common/logging/logging.h"
// the path for the shared memory segment. see shm_open(3) for restrictions
#define AOS_SHM_NAME "/aos_shared_mem"
@@ -37,7 +38,7 @@
struct aos_core *global_core = NULL;
// TODO(brians): madvise(2) it to put this shm in core dumps.
-int aos_core_create_shared_mem(enum aos_core_create to_create) {
+void aos_core_create_shared_mem(enum aos_core_create to_create) {
static struct aos_core global_core_data;
global_core = &global_core_data;
@@ -59,8 +60,7 @@
if (shm == -1 && errno == EEXIST) {
printf("shared_mem: going to shm_unlink(%s)\n", global_core->shm_name);
if (shm_unlink(global_core->shm_name) == -1) {
- fprintf(stderr, "shared_mem: shm_unlink(%s) failed with of %d: %s\n",
- global_core->shm_name, errno, strerror(errno));
+ PLOG(WARNING, "shm_unlink(%s) failed", global_core->shm_name);
break;
}
} else {
@@ -73,46 +73,35 @@
global_core->owner = 0;
}
if (shm == -1) {
- fprintf(stderr, "shared_mem:"
- " shm_open(%s, O_RDWR [| O_CREAT | O_EXCL, 0|0666)"
- " failed with %d: %s\n",
- global_core->shm_name, errno, strerror(errno));
- return -1;
+ PLOG(FATAL, "shm_open(%s, O_RDWR [| O_CREAT | O_EXCL, 0|0666) failed",
+ global_core->shm_name);
}
if (global_core->owner) {
if (ftruncate(shm, SIZEOFSHMSEG) == -1) {
- fprintf(stderr, "shared_mem: fruncate(%d, 0x%zx) failed with %d: %s\n",
- shm, (size_t)SIZEOFSHMSEG, errno, strerror(errno));
- return -1;
+ PLOG(FATAL, "fruncate(%d, 0x%zx) failed", shm, (size_t)SIZEOFSHMSEG);
}
}
void *shm_address = mmap(
(void *)SHM_START, SIZEOFSHMSEG, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_FIXED | MAP_LOCKED | MAP_POPULATE, shm, 0);
if (shm_address == MAP_FAILED) {
- fprintf(stderr, "shared_mem: mmap(%p, 0x%zx, stuff, stuff, %d, 0) failed"
- " with %d: %s\n",
- (void *)SHM_START, (size_t)SIZEOFSHMSEG, shm,
- errno, strerror(errno));
- return -1;
+ PLOG(FATAL, "shared_mem: mmap(%p, 0x%zx, stuff, stuff, %d, 0) failed",
+ (void *)SHM_START, (size_t)SIZEOFSHMSEG, shm);
}
printf("shared_mem: shm at: %p\n", shm_address);
if (close(shm) == -1) {
- printf("shared_mem: close(%d(=shm) failed with %d: %s\n",
- shm, errno, strerror(errno));
+ PLOG(WARNING, "close(%d(=shm) failed", shm);
}
if (shm_address != (void *)SHM_START) {
- fprintf(stderr, "shared_mem: shm isn't at hard-coded %p. at %p instead\n",
+ LOG(FATAL, "shm isn't at hard-coded %p. at %p instead\n",
(void *)SHM_START, shm_address);
- return -1;
}
- int r = aos_core_use_address_as_shared_mem(shm_address, SIZEOFSHMSEG);
- fprintf(stderr, "shared_mem: end of create_shared_mem owner=%d r=%d\n",
- global_core->owner, r);
- return r;
+ aos_core_use_address_as_shared_mem(shm_address, SIZEOFSHMSEG);
+ LOG(INFO, "shared_mem: end of create_shared_mem owner=%d\n",
+ global_core->owner);
}
-int aos_core_use_address_as_shared_mem(void *address, size_t size) {
+void aos_core_use_address_as_shared_mem(void *address, size_t size) {
global_core->mem_struct = address;
global_core->size = size;
global_core->shared_mem =
@@ -123,28 +112,22 @@
futex_set(&global_core->mem_struct->creation_condition);
} else {
if (futex_wait(&global_core->mem_struct->creation_condition) != 0) {
- fprintf(stderr, "waiting on creation_condition failed\n");
- return -1;
+ LOG(FATAL, "waiting on creation_condition failed\n");
}
}
- return 0;
}
-int aos_core_free_shared_mem(){
+void aos_core_free_shared_mem() {
void *shm_address = global_core->shared_mem;
- if (munmap((void *)SHM_START, SIZEOFSHMSEG) == -1) {
- fprintf(stderr, "shared_mem: munmap(%p, 0x%zx) failed with %d: %s\n",
- shm_address, (size_t)SIZEOFSHMSEG, errno, strerror(errno));
- return -1;
- }
- if (global_core->owner) {
- if (shm_unlink(global_core->shm_name)) {
- fprintf(stderr, "shared_mem: shm_unlink(%s) failed with %d: %s\n",
- global_core->shm_name, errno, strerror(errno));
- return -1;
- }
+ if (munmap((void *)SHM_START, SIZEOFSHMSEG) == -1) {
+ PLOG(FATAL, "munmap(%p, 0x%zx) failed", shm_address,
+ (size_t)SIZEOFSHMSEG);
}
- return 0;
+ if (global_core->owner) {
+ if (shm_unlink(global_core->shm_name)) {
+ PLOG(FATAL, "shared_mem: shm_unlink(%s) failed", global_core->shm_name);
+ }
+ }
}
int aos_core_is_init(void) {
diff --git a/aos/linux_code/ipc_lib/shared_mem.h b/aos/linux_code/ipc_lib/shared_mem.h
index edab4d0..6968df7 100644
--- a/aos/linux_code/ipc_lib/shared_mem.h
+++ b/aos/linux_code/ipc_lib/shared_mem.h
@@ -66,10 +66,10 @@
// should be set correctly there.
// The owner should verify that the first sizeof(mutex) of data is set to 0
// before passing the memory to this function.
-int aos_core_use_address_as_shared_mem(void *address, size_t size);
+void aos_core_use_address_as_shared_mem(void *address, size_t size);
-int aos_core_create_shared_mem(enum aos_core_create to_create);
-int aos_core_free_shared_mem(void);
+void aos_core_create_shared_mem(enum aos_core_create to_create);
+void aos_core_free_shared_mem(void);
// Returns whether or not the shared memory system is active.
int aos_core_is_init(void);