• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007-2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * pmsg write handler
19  */
20 
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <time.h>
28 
29 #include <log/log_properties.h>
30 #include <private/android_filesystem_config.h>
31 #include <private/android_logger.h>
32 
33 #include "config_write.h"
34 #include "log_portability.h"
35 #include "logger.h"
36 #include "uio.h"
37 
38 static int pmsgOpen();
39 static void pmsgClose();
40 static int pmsgAvailable(log_id_t logId);
41 static int pmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr);
42 
43 struct android_log_transport_write pmsgLoggerWrite = {
44     .node = {&pmsgLoggerWrite.node, &pmsgLoggerWrite.node},
45     .context.fd = -1,
46     .name = "pmsg",
47     .available = pmsgAvailable,
48     .open = pmsgOpen,
49     .close = pmsgClose,
50     .write = pmsgWrite,
51 };
52 
pmsgOpen()53 static int pmsgOpen() {
54   int fd = atomic_load(&pmsgLoggerWrite.context.fd);
55   if (fd < 0) {
56     int i;
57 
58     fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY | O_CLOEXEC));
59     i = atomic_exchange(&pmsgLoggerWrite.context.fd, fd);
60     if ((i >= 0) && (i != fd)) {
61       close(i);
62     }
63   }
64 
65   return fd;
66 }
67 
pmsgClose()68 static void pmsgClose() {
69   int fd = atomic_exchange(&pmsgLoggerWrite.context.fd, -1);
70   if (fd >= 0) {
71     close(fd);
72   }
73 }
74 
pmsgAvailable(log_id_t logId)75 static int pmsgAvailable(log_id_t logId) {
76   if (logId > LOG_ID_SECURITY) {
77     return -EINVAL;
78   }
79   if ((logId != LOG_ID_SECURITY) && (logId != LOG_ID_EVENTS) && !__android_log_is_debuggable()) {
80     return -EINVAL;
81   }
82   if (atomic_load(&pmsgLoggerWrite.context.fd) < 0) {
83     if (access("/dev/pmsg0", W_OK) == 0) {
84       return 0;
85     }
86     return -EBADF;
87   }
88   return 1;
89 }
90 
91 /*
92  * Extract a 4-byte value from a byte stream.
93  */
get4LE(const uint8_t * src)94 static inline uint32_t get4LE(const uint8_t* src) {
95   return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
96 }
97 
pmsgWrite(log_id_t logId,struct timespec * ts,struct iovec * vec,size_t nr)98 static int pmsgWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) {
99   static const unsigned headerLength = 2;
100   struct iovec newVec[nr + headerLength];
101   android_log_header_t header;
102   android_pmsg_log_header_t pmsgHeader;
103   size_t i, payloadSize;
104   ssize_t ret;
105 
106   if ((logId == LOG_ID_EVENTS) && !__android_log_is_debuggable()) {
107     if (vec[0].iov_len < 4) {
108       return -EINVAL;
109     }
110 
111     if (SNET_EVENT_LOG_TAG != get4LE(static_cast<uint8_t*>(vec[0].iov_base))) {
112       return -EPERM;
113     }
114   }
115 
116   if (atomic_load(&pmsgLoggerWrite.context.fd) < 0) {
117     return -EBADF;
118   }
119 
120   /*
121    *  struct {
122    *      // what we provide to pstore
123    *      android_pmsg_log_header_t pmsgHeader;
124    *      // what we provide to file
125    *      android_log_header_t header;
126    *      // caller provides
127    *      union {
128    *          struct {
129    *              char     prio;
130    *              char     payload[];
131    *          } string;
132    *          struct {
133    *              uint32_t tag
134    *              char     payload[];
135    *          } binary;
136    *      };
137    *  };
138    */
139 
140   pmsgHeader.magic = LOGGER_MAGIC;
141   pmsgHeader.len = sizeof(pmsgHeader) + sizeof(header);
142   pmsgHeader.uid = __android_log_uid();
143   pmsgHeader.pid = getpid();
144 
145   header.id = logId;
146   header.tid = gettid();
147   header.realtime.tv_sec = ts->tv_sec;
148   header.realtime.tv_nsec = ts->tv_nsec;
149 
150   newVec[0].iov_base = (unsigned char*)&pmsgHeader;
151   newVec[0].iov_len = sizeof(pmsgHeader);
152   newVec[1].iov_base = (unsigned char*)&header;
153   newVec[1].iov_len = sizeof(header);
154 
155   for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
156     newVec[i].iov_base = vec[i - headerLength].iov_base;
157     payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
158 
159     if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
160       newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
161       if (newVec[i].iov_len) {
162         ++i;
163       }
164       payloadSize = LOGGER_ENTRY_MAX_PAYLOAD;
165       break;
166     }
167   }
168   pmsgHeader.len += payloadSize;
169 
170   ret = TEMP_FAILURE_RETRY(writev(atomic_load(&pmsgLoggerWrite.context.fd), newVec, i));
171   if (ret < 0) {
172     ret = errno ? -errno : -ENOTCONN;
173   }
174 
175   if (ret > (ssize_t)(sizeof(header) + sizeof(pmsgHeader))) {
176     ret -= sizeof(header) - sizeof(pmsgHeader);
177   }
178 
179   return ret;
180 }
181 
182 /*
183  * Virtual pmsg filesystem
184  *
185  * Payload will comprise the string "<basedir>:<basefile>\0<content>" to a
186  * maximum of LOGGER_ENTRY_MAX_PAYLOAD, but scaled to the last newline in the
187  * file.
188  *
189  * Will hijack the header.realtime.tv_nsec field for a sequence number in usec.
190  */
191 
strnrchr(const char * buf,size_t len,char c)192 static inline const char* strnrchr(const char* buf, size_t len, char c) {
193   const char* cp = buf + len;
194   while ((--cp > buf) && (*cp != c))
195     ;
196   if (cp <= buf) {
197     return buf + len;
198   }
199   return cp;
200 }
201 
202 /* Write a buffer as filename references (tag = <basedir>:<basename>) */
__android_log_pmsg_file_write(log_id_t logId,char prio,const char * filename,const char * buf,size_t len)203 ssize_t __android_log_pmsg_file_write(log_id_t logId, char prio, const char* filename,
204                                       const char* buf, size_t len) {
205   bool weOpened;
206   size_t length, packet_len;
207   const char* tag;
208   char *cp, *slash;
209   struct timespec ts;
210   struct iovec vec[3];
211 
212   /* Make sure the logId value is not a bad idea */
213   if ((logId == LOG_ID_KERNEL) ||   /* Verbotten */
214       (logId == LOG_ID_EVENTS) ||   /* Do not support binary content */
215       (logId == LOG_ID_SECURITY) || /* Bad idea to allow */
216       ((unsigned)logId >= 32)) {    /* fit within logMask on arch32 */
217     return -EINVAL;
218   }
219 
220   clock_gettime(android_log_clockid(), &ts);
221 
222   cp = strdup(filename);
223   if (!cp) {
224     return -ENOMEM;
225   }
226 
227   tag = cp;
228   slash = strrchr(cp, '/');
229   if (slash) {
230     *slash = ':';
231     slash = strrchr(cp, '/');
232     if (slash) {
233       tag = slash + 1;
234     }
235   }
236 
237   length = strlen(tag) + 1;
238   packet_len = LOGGER_ENTRY_MAX_PAYLOAD - sizeof(char) - length;
239 
240   vec[0].iov_base = &prio;
241   vec[0].iov_len = sizeof(char);
242   vec[1].iov_base = (unsigned char*)tag;
243   vec[1].iov_len = length;
244 
245   weOpened = false;
246   for (ts.tv_nsec = 0, length = len; length; ts.tv_nsec += ANDROID_LOG_PMSG_FILE_SEQUENCE) {
247     ssize_t ret;
248     size_t transfer;
249 
250     if ((ts.tv_nsec / ANDROID_LOG_PMSG_FILE_SEQUENCE) >= ANDROID_LOG_PMSG_FILE_MAX_SEQUENCE) {
251       len -= length;
252       break;
253     }
254 
255     transfer = length;
256     if (transfer > packet_len) {
257       transfer = strnrchr(buf, packet_len - 1, '\n') - buf;
258       if ((transfer < length) && (buf[transfer] == '\n')) {
259         ++transfer;
260       }
261     }
262 
263     vec[2].iov_base = (unsigned char*)buf;
264     vec[2].iov_len = transfer;
265 
266     if (atomic_load(&pmsgLoggerWrite.context.fd) < 0) {
267       if (!weOpened) { /* Impossible for weOpened = true here */
268         __android_log_lock();
269       }
270       weOpened = atomic_load(&pmsgLoggerWrite.context.fd) < 0;
271       if (!weOpened) {
272         __android_log_unlock();
273       } else if (pmsgOpen() < 0) {
274         __android_log_unlock();
275         free(cp);
276         return -EBADF;
277       }
278     }
279 
280     ret = pmsgWrite(logId, &ts, vec, sizeof(vec) / sizeof(vec[0]));
281 
282     if (ret <= 0) {
283       if (weOpened) {
284         pmsgClose();
285         __android_log_unlock();
286       }
287       free(cp);
288       return ret ? ret : (len - length);
289     }
290     length -= transfer;
291     buf += transfer;
292   }
293   if (weOpened) {
294     pmsgClose();
295     __android_log_unlock();
296   }
297   free(cp);
298   return len;
299 }
300