1 /*
2 
3    nsjail - logging
4    -----------------------------------------
5 
6    Copyright 2014 Google Inc. All Rights Reserved.
7 
8    Licensed under the Apache License, Version 2.0 (the "License");
9    you may not use this file except in compliance with the License.
10    You may obtain a copy of the License at
11 
12      http://www.apache.org/licenses/LICENSE-2.0
13 
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19 
20 */
21 
22 #include "libhfcommon/log.h"
23 
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <getopt.h>
27 #include <limits.h>
28 #include <pthread.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <time.h>
36 #include <unistd.h>
37 
38 #include "libhfcommon/common.h"
39 #include "libhfcommon/util.h"
40 
41 #if defined(_HF_ARCH_LINUX)
42 #include <sys/syscall.h>
43 #define __hf_pid() (pid_t) syscall(__NR_gettid)
44 #elif defined(_HF_ARCH_NETBSD)
45 #include <lwp.h>
46 #define __hf_pid() _lwp_self()
47 #else
48 #define __hf_pid() getpid()
49 #endif
50 
51 static int log_fd = STDERR_FILENO;
52 static bool log_fd_isatty = false;
53 enum llevel_t log_level = INFO;
54 static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
55 
log_init(void)56 __attribute__((constructor)) static void log_init(void) {
57     log_fd = fcntl(log_fd, F_DUPFD_CLOEXEC, 0);
58     if (log_fd == -1) {
59         log_fd = STDERR_FILENO;
60     }
61     log_fd_isatty = isatty(log_fd);
62 }
63 
64 /*
65  * Log to stderr by default. Use a dup()d fd, because in the future we'll associate the
66  * connection socket with fd (0, 1, 2).
67  */
logInitLogFile(const char * logfile,int fd,enum llevel_t ll)68 void logInitLogFile(const char* logfile, int fd, enum llevel_t ll) {
69     log_level = ll;
70 
71     if (logfile) {
72         log_fd = open(logfile, O_CREAT | O_RDWR | O_TRUNC, 0640);
73         if (log_fd == -1) {
74             log_fd = STDERR_FILENO;
75             PLOG_E("Couldn't open logfile open('%s')", logfile);
76         }
77     }
78     if (fd != -1) {
79         log_fd = fd;
80     }
81 
82     log_fd_isatty = (isatty(log_fd) == 1 ? true : false);
83 }
84 
logLog(enum llevel_t ll,const char * fn,int ln,bool perr,const char * fmt,...)85 void logLog(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt, ...) {
86     char strerr[512];
87     if (perr == true) {
88         snprintf(strerr, sizeof(strerr), "%s", strerror(errno));
89     }
90     struct ll_t {
91         const char* descr;
92         const char* prefix;
93         const bool print_funcline;
94         const bool print_time;
95     };
96     static const struct ll_t logLevels[] = {
97         {"F", "\033[7;35m", true, true},
98         {"E", "\033[1;31m", true, true},
99         {"W", "\033[0;33m", true, true},
100         {"I", "\033[1m", false, false},
101         {"D", "\033[0;4m", true, true},
102         {"HR", "\033[0m", false, false},
103         {"HB", "\033[1m", false, false},
104     };
105 
106     time_t ltstamp = time(NULL);
107     struct tm utctime;
108     localtime_r(&ltstamp, &utctime);
109     char timestr[32];
110     if (strftime(timestr, sizeof(timestr) - 1, "%FT%T%z", &utctime) == 0) {
111         timestr[0] = '\0';
112     }
113 
114     /* Start printing logs */
115     {
116         MX_LOCK(&log_mutex);
117 
118         if (log_fd_isatty) {
119             dprintf(log_fd, "%s", logLevels[ll].prefix);
120         }
121         if (logLevels[ll].print_time) {
122             dprintf(log_fd, "[%s][%s][%d] ", timestr, logLevels[ll].descr, __hf_pid());
123         }
124         if (logLevels[ll].print_funcline) {
125             dprintf(log_fd, "%s():%d ", fn, ln);
126         }
127 
128         va_list args;
129         va_start(args, fmt);
130         vdprintf(log_fd, fmt, args);
131         va_end(args);
132 
133         if (perr == true) {
134             dprintf(log_fd, ": %s", strerr);
135         }
136         if (log_fd_isatty) {
137             dprintf(log_fd, "\033[0m");
138         }
139         dprintf(log_fd, "\n");
140 
141         MX_UNLOCK(&log_mutex);
142     }
143     /* End printing logs */
144 
145     if (ll == FATAL) {
146         exit(EXIT_FAILURE);
147     }
148 }
149 
logStop(int sig)150 void logStop(int sig) {
151     LOG_I("Server stops due to fatal signal (%d) caught. Exiting", sig);
152 }
153 
logRedirectLogFD(int fd)154 void logRedirectLogFD(int fd) {
155     log_fd = fd;
156     log_fd_isatty = isatty(log_fd);
157 }
158 
logDirectlyToFD(const char * msg)159 void logDirectlyToFD(const char* msg) {
160     dprintf(log_fd, "%s", msg);
161 }
162 
logMutexGet(void)163 pthread_mutex_t* logMutexGet(void) {
164     return &log_mutex;
165 }
166 
logMutexReset(void)167 void logMutexReset(void) {
168     pthread_mutex_init(&log_mutex, NULL);
169 }
170 
logIsTTY(void)171 bool logIsTTY(void) {
172     return log_fd_isatty;
173 }
174 
logFd(void)175 int logFd(void) {
176     return log_fd;
177 }
178 
logGetLevel(void)179 enum llevel_t logGetLevel(void) {
180     return log_level;
181 }
182