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  * Some OS specific dribs and drabs (locking etc).
19  */
20 
21 #if !defined(_WIN32)
22 #include <pthread.h>
23 #endif
24 
25 #include <private/android_filesystem_config.h>
26 
27 #include "logger.h"
28 
__android_log_uid()29 LIBLOG_HIDDEN uid_t __android_log_uid()
30 {
31 #if defined(_WIN32)
32     return AID_SYSTEM;
33 #else
34     static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */
35 
36     if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */
37         last_uid = getuid();
38     }
39     return last_uid;
40 #endif
41 }
42 
__android_log_pid()43 LIBLOG_HIDDEN pid_t __android_log_pid()
44 {
45     static pid_t last_pid = (pid_t) -1;
46 
47     if (last_pid == (pid_t) -1) {
48         last_pid = getpid();
49     }
50     return last_pid;
51 }
52 
53 #if !defined(_WIN32)
54 static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
55 #endif
56 
__android_log_lock()57 LIBLOG_HIDDEN void __android_log_lock()
58 {
59 #if !defined(_WIN32)
60     /*
61      * If we trigger a signal handler in the middle of locked activity and the
62      * signal handler logs a message, we could get into a deadlock state.
63      */
64     pthread_mutex_lock(&log_init_lock);
65 #endif
66 }
67 
__android_log_trylock()68 LIBLOG_HIDDEN int __android_log_trylock()
69 {
70 #if !defined(_WIN32)
71     return pthread_mutex_trylock(&log_init_lock);
72 #else
73     return 0;
74 #endif
75 }
76 
__android_log_unlock()77 LIBLOG_HIDDEN void __android_log_unlock()
78 {
79 #if !defined(_WIN32)
80     pthread_mutex_unlock(&log_init_lock);
81 #endif
82 }
83