diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 18:29:04 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-03-03 18:29:04 -0800 |
commit | e54eebbf1a908d65ee8cf80bab62821c05666d70 (patch) | |
tree | 4b825dc642cb6eb9a060e54bf8d69288fbee4904 /init/util.c | |
parent | a1e1c1b106423de09bc918502e7a51d4ffe5a4ae (diff) | |
download | system_core-e54eebbf1a908d65ee8cf80bab62821c05666d70.tar.gz system_core-e54eebbf1a908d65ee8cf80bab62821c05666d70.tar.bz2 system_core-e54eebbf1a908d65ee8cf80bab62821c05666d70.zip |
auto import from //depot/cupcake/@135843
Diffstat (limited to 'init/util.c')
-rw-r--r-- | init/util.c | 211 |
1 files changed, 0 insertions, 211 deletions
diff --git a/init/util.c b/init/util.c deleted file mode 100644 index 0b7667dae..000000000 --- a/init/util.c +++ /dev/null @@ -1,211 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <stdarg.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <fcntl.h> -#include <ctype.h> -#include <errno.h> - -#include <sys/stat.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <sys/un.h> - -/* for ANDROID_SOCKET_* */ -#include <cutils/sockets.h> - -#include <private/android_filesystem_config.h> - -#include "init.h" - -static int log_fd = -1; -/* Inital log level before init.rc is parsed and this this is reset. */ -static int log_level = LOG_DEFAULT_LEVEL; - - -void log_set_level(int level) { - log_level = level; -} - -void log_init(void) -{ - static const char *name = "/dev/__kmsg__"; - if (mknod(name, S_IFCHR | 0600, (1 << 8) | 11) == 0) { - log_fd = open(name, O_WRONLY); - fcntl(log_fd, F_SETFD, FD_CLOEXEC); - unlink(name); - } -} - -#define LOG_BUF_MAX 512 - -void log_write(int level, const char *fmt, ...) -{ - char buf[LOG_BUF_MAX]; - va_list ap; - - if (level > log_level) return; - if (log_fd < 0) return; - - va_start(ap, fmt); - vsnprintf(buf, LOG_BUF_MAX, fmt, ap); - buf[LOG_BUF_MAX - 1] = 0; - va_end(ap); - write(log_fd, buf, strlen(buf)); -} - -/* - * android_name_to_id - returns the integer uid/gid associated with the given - * name, or -1U on error. - */ -static unsigned int android_name_to_id(const char *name) -{ - struct android_id_info *info = android_ids; - unsigned int n; - - for (n = 0; n < android_id_count; n++) { - if (!strcmp(info[n].name, name)) - return info[n].aid; - } - - return -1U; -} - -/* - * decode_uid - decodes and returns the given string, which can be either the - * numeric or name representation, into the integer uid or gid. Returns -1U on - * error. - */ -unsigned int decode_uid(const char *s) -{ - unsigned int v; - - if (!s || *s == '\0') - return -1U; - if (isalpha(s[0])) - return android_name_to_id(s); - - errno = 0; - v = (unsigned int) strtoul(s, 0, 0); - if (errno) - return -1U; - return v; -} - -/* - * create_socket - creates a Unix domain socket in ANDROID_SOCKET_DIR - * ("/dev/socket") as dictated in init.rc. This socket is inherited by the - * daemon. We communicate the file descriptor's value via the environment - * variable ANDROID_SOCKET_ENV_PREFIX<name> ("ANDROID_SOCKET_foo"). - */ -int create_socket(const char *name, int type, mode_t perm, uid_t uid, gid_t gid) -{ - struct sockaddr_un addr; - int fd, ret; - - fd = socket(PF_UNIX, type, 0); - if (fd < 0) { - ERROR("Failed to open socket '%s': %s\n", name, strerror(errno)); - return -1; - } - - memset(&addr, 0 , sizeof(addr)); - addr.sun_family = AF_UNIX; - snprintf(addr.sun_path, sizeof(addr.sun_path), ANDROID_SOCKET_DIR"/%s", - name); - - ret = unlink(addr.sun_path); - if (ret != 0 && errno != ENOENT) { - ERROR("Failed to unlink old socket '%s': %s\n", name, strerror(errno)); - goto out_close; - } - - ret = bind(fd, (struct sockaddr *) &addr, sizeof (addr)); - if (ret) { - ERROR("Failed to bind socket '%s': %s\n", name, strerror(errno)); - goto out_unlink; - } - - chown(addr.sun_path, uid, gid); - chmod(addr.sun_path, perm); - - INFO("Created socket '%s' with mode '%o', user '%d', group '%d'\n", - addr.sun_path, perm, uid, gid); - - return fd; - -out_unlink: - unlink(addr.sun_path); -out_close: - close(fd); - return -1; -} - -/* reads a file, making sure it is terminated with \n \0 */ -void *read_file(const char *fn, unsigned *_sz) -{ - char *data; - int sz; - int fd; - - data = 0; - fd = open(fn, O_RDONLY); - if(fd < 0) return 0; - - sz = lseek(fd, 0, SEEK_END); - if(sz < 0) goto oops; - - if(lseek(fd, 0, SEEK_SET) != 0) goto oops; - - data = (char*) malloc(sz + 2); - if(data == 0) goto oops; - - if(read(fd, data, sz) != sz) goto oops; - close(fd); - data[sz] = '\n'; - data[sz+1] = 0; - if(_sz) *_sz = sz; - return data; - -oops: - close(fd); - if(data != 0) free(data); - return 0; -} - -void list_init(struct listnode *node) -{ - node->next = node; - node->prev = node; -} - -void list_add_tail(struct listnode *head, struct listnode *item) -{ - item->next = head; - item->prev = head->prev; - head->prev->next = item; - head->prev = item; -} - -void list_remove(struct listnode *item) -{ - item->next->prev = item->prev; - item->prev->next = item->next; -} - |