return 0;
}
+int redirectFD()
+{
+ if (__pfd[1] < 0) {
+ _DBG("fail to create pipe for logging");
+ return -1;
+ }
+ // stdout
+ if (dup2(__pfd[1], 1) == -1) {
+ _DBG("fail to duplicate fd to stdout");
+ return -1;
+ }
+
+ // stderr
+ if (dup2(__pfd[1], 2) == -1) {
+ _DBG("fail to duplicate fd to stderr");
+ return-1;
+ }
+
+ return 0;
+}
+
int runLoggingThread()
{
if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) {
_DBG("fail to make stdout line-buffered");
- return 0;
+ return -1;
}
if (setvbuf(stderr, NULL, _IONBF, 0) < 0) {
_DBG("make stderr unbuffered");
- return 0;
+ return -1;
}
/* create the pipe and redirect stdout and stderr */
if (pipe(__pfd) < 0) {
_DBG("fail to create pipe for logging");
- return 0;
- }
-
- if (dup2(__pfd[1], fileno(stdout)) == -1) {
- _DBG("fail to duplicate fd to stdout");
- return 0;
+ return -1;
}
- if (dup2(__pfd[1], fileno(stderr)) == -1) {
- _DBG("fail to duplicate fd to stderr");
- return 0;
+ if (redirectFD() < 0) {
+ _DBG("fail to redirect FD");
+ return -1;
}
- close(__pfd[1]);
-
/* spawn the logging thread */
if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) {
_DBG("fail to create pthread");