• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 #include <stdarg.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <fcntl.h>
22 #include <ctype.h>
23 #include <errno.h>
24 
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 
30 /* for ANDROID_SOCKET_* */
31 #include <cutils/sockets.h>
32 
33 #include <private/android_filesystem_config.h>
34 
35 #include "init.h"
36 
37 static int log_fd = -1;
38 /* Inital log level before init.rc is parsed and this this is reset. */
39 static int log_level = LOG_DEFAULT_LEVEL;
40 
41 
log_set_level(int level)42 void log_set_level(int level) {
43     log_level = level;
44 }
45 
log_init(void)46 void log_init(void)
47 {
48     static const char *name = "/dev/__kmsg__";
49     if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) {
50         log_fd = open(name, O_WRONLY);
51         fcntl(log_fd, F_SETFD, FD_CLOEXEC);
52         unlink(name);
53     }
54 }
55 
56 #define LOG_BUF_MAX 512
57 
log_write(int level,const char * fmt,...)58 void log_write(int level, const char *fmt, ...)
59 {
60     char buf[LOG_BUF_MAX];
61     va_list ap;
62 
63     if (level > log_level) return;
64     if (log_fd < 0) return;
65 
66     va_start(ap, fmt);
67     vsnprintf(buf, LOG_BUF_MAX, fmt, ap);
68     buf[LOG_BUF_MAX - 1] = 0;
69     va_end(ap);
70     write(log_fd, buf, strlen(buf));
71 }
72 
73 /*
74  * android_name_to_id - returns the integer uid/gid associated with the given
75  * name, or -1U on error.
76  */
android_name_to_id(const char * name)77 static unsigned int android_name_to_id(const char *name)
78 {
79     struct android_id_info *info = android_ids;
80     unsigned int n;
81 
82     for (n = 0; n < android_id_count; n++) {
83         if (!strcmp(info[n].name, name))
84             return info[n].aid;
85     }
86 
87     return -1U;
88 }
89 
90 /*
91  * decode_uid - decodes and returns the given string, which can be either the
92  * numeric or name representation, into the integer uid or gid. Returns -1U on
93  * error.
94  */
decode_uid(const char * s)95 unsigned int decode_uid(const char *s)
96 {
97     unsigned int v;
98 
99     if (!s || *s == '\0')
100         return -1U;
101     if (isalpha(s[0]))
102         return android_name_to_id(s);
103 
104     errno = 0;
105     v = (unsigned int) strtoul(s, 0, 0);
106     if (errno)
107         return -1U;
108     return v;
109 }
110 
111 /*
112  * create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR
113  * ("/dev/socket") as dictated in init.rc. This socket is inherited by the
114  * daemon. We communicate the file descriptor's value via the environment
115  * variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo").
116  */
create_socket(const char * name,int type,mode_t perm,uid_t uid,gid_t gid)117 int create_socket(const char *name, int type, mode_t perm, uid_t uid, gid_t gid)
118 {
119     struct sockaddr_un addr;
120     int fd, ret;
121 
122     fd = socket(PF_UNIX, type, 0);
123     if (fd < 0) {
124         ERROR("Failed to open socket '%s': %s\n", name, strerror(errno));
125         return -1;
126     }
127 
128     memset(&addr, 0 , sizeof(addr));
129     addr.sun_family = AF_UNIX;
130     snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s",
131              name);
132 
133     ret = unlink(addr.sun_path);
134     if (ret != 0 && errno != ENOENT) {
135         ERROR("Failed to unlink old socket '%s': %s\n", name, strerror(errno));
136         goto out_close;
137     }
138 
139     ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr));
140     if (ret) {
141         ERROR("Failed to bind socket '%s': %s\n", name, strerror(errno));
142         goto out_unlink;
143     }
144 
145     chown(addr.sun_path, uid, gid);
146     chmod(addr.sun_path, perm);
147 
148     INFO("Created socket '%s' with mode '%o', user '%d', group '%d'\n",
149          addr.sun_path, perm, uid, gid);
150 
151     return fd;
152 
153 out_unlink:
154     unlink(addr.sun_path);
155 out_close:
156     close(fd);
157     return -1;
158 }
159 
160 /* reads a file, making sure it is terminated with \n \0 */
read_file(const char * fn,unsigned * _sz)161 void *read_file(const char *fn, unsigned *_sz)
162 {
163     char *data;
164     int sz;
165     int fd;
166 
167     data = 0;
168     fd = open(fn, O_RDONLY);
169     if(fd < 0) return 0;
170 
171     sz = lseek(fd, 0, SEEK_END);
172     if(sz < 0) goto oops;
173 
174     if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
175 
176     data = (char*) malloc(sz + 2);
177     if(data == 0) goto oops;
178 
179     if(read(fd, data, sz) != sz) goto oops;
180     close(fd);
181     data[sz] = '\n';
182     data[sz+1] = 0;
183     if(_sz) *_sz = sz;
184     return data;
185 
186 oops:
187     close(fd);
188     if(data != 0) free(data);
189     return 0;
190 }
191 
list_init(struct listnode * node)192 void list_init(struct listnode *node)
193 {
194     node->next = node;
195     node->prev = node;
196 }
197 
list_add_tail(struct listnode * head,struct listnode * item)198 void list_add_tail(struct listnode *head, struct listnode *item)
199 {
200     item->next = head;
201     item->prev = head->prev;
202     head->prev->next = item;
203     head->prev = item;
204 }
205 
list_remove(struct listnode * item)206 void list_remove(struct listnode *item)
207 {
208     item->next->prev = item->prev;
209     item->prev->next = item->next;
210 }
211 
212