Android native开发:systeom/core/libcutils

1. android_filesystem_config.h

定义了很多AID_xxx的宏,build/tools/fs_config用来生成Android文件的。
#define AID_ROOT 0
#define AID_DAEMON
#define AID_BIN 2
#define AID_SYSTEM 1000

2. android_get_control_file.h

获取init管理的文件,path是init.rc中的文件路径,返回fd。参见klog.cpp
int android_get_control_file(const char* path);

3. android_reboot.h

通过设置property,请求init进程重启、关闭系统。
int android_reboot(unsigned cmd, int flags, const char* arg);

4. ashmem.h

封装共享内存相关函数。

	int ashmem_valid(int fd);
	int ashmem_create_region(const char *name, size_t size);
	int ashmem_set_prot_region(int fd, int prot);
	int ashmem_pin_region(int fd, size_t offset, size_t len);
	int ashmem_unpin_region(int fd, size_t offset, size_t len);
	int ashmem_get_size_region(int fd);

5. atomic.h

atomic相关的一些函数,遗留代码,不推荐使用。

6. bitops.h

对__builtin_popcount的封装,计算二进制中1的个数。
static inline int popcount(unsigned int x);

7. compiler.h

定义CC_LIKELY/CC_UNLIKELY宏,封装__builtin_expect。
#define ANDROID_API __attribute__((visibility("default")))

8. config_utils.h

对配置文件的解析、处理。

	void config_load(cnode *root, char *data);
	void config_load_file(cnode *root, const char *fn);
	cnode* config_node(const char *name, const char *value);
	cnode* config_find(cnode *root, const char *name);
	int config_bool(cnode *root, const char *name, int _default);
	const char* config_str(cnode *root, const char *name, const char *_default);
	void config_set(cnode *root, const char *name, const char *value);
	void config_free(cnode *root);

9. fs.h

文件、目录相关方法

扫描二维码关注公众号,回复: 15347700 查看本文章
如果path不存在,则创建。
如果path存在,加查mode和owners,如果不符合,则修改。
extern int fs_prepare_dir(const char* path, mode_t mode, uid_t uid, gid_t gid);
同上,如果不符合,不修改参,返回-1extern int fs_prepare_dir_strict(const char* path, mode_t mode, uid_t uid, gid_t gid);
extern int fs_prepare_file_strict(const char* path, mode_t mode, uid_t uid, gid_t gid);

从文件中读写一个整数。
extern int fs_read_atomic_int(const char* path, int* value);
extern int fs_write_atomic_int(const char* path, int value);

创建path指定的各级目录。
如果最后不是'/'结尾,最后一级会被当做文件名,忽略。
extern int fs_mkdirs(const char* path, mode_t mode);

10. hashmap.h

自定义Hashmap及相关操作函数。

11. iosched_policy.h

设置/获取io schedule policy。
extern int android_set_ioprio(int pid, IoSchedClass clazz, int ioprio);
extern int android_get_ioprio(int pid, IoSchedClass *clazz, int *ioprio);

12. klog.h

提供/dev/kmsg的写log方法。
void klog_set_level(int level);
void klog_write(int level, const char fmt, …) attribute ((format(printf, 2, 3)));
void klog_writev(int level, const struct iovec
iov, int iov_count);

13. list.h

自定义list及相关操作函数。

14. log.h

#include <log/log.h>

15. memory.h

为GLIBC和WIN32声明方法:
size_t strlcpy(char *dst, const char *src, size_t size);

16. misc.h

把文件fn加载到内存中,大小为file-size + 1,通过sz返回内存大小。
extern void *load_file(const char *fn, unsigned *sz);

UID范围。
#define FIRST_APPLICATION_UID 10000
#define LAST_APPLICATION_UID 99999

17. multiuser.h

封装了user id, app id, uid, cache_gid, ext gid, ext cache gid, shared gid, share app gid获取和转换函数。

18. native_handle.h

定义native_handle结构体,以及相关创建、删除等操作函数。
native_handle定义中使用了C99扩展,可变长度数组(0长度数组zero-length-array)。根据numFds和numInts实际值,保存对应个数的data数组。

typedef struct native_handle
{
    
    
    int version;        /* sizeof(native_handle_t) */
    int numFds;         /* number of file-descriptors at &data[0] */
    int numInts;        /* number of ints at &data[numFds] */

    int data[0];        /* numFds + numInts ints */
} native_handle_t;

int native_handle_close(const native_handle_t* h);
native_handle_t* native_handle_init(char* storage, int numFds, int numInts);
native_handle_t* native_handle_create(int numFds, int numInts);
native_handle_t* native_handle_clone(const native_handle_t* handle);
int native_handle_delete(native_handle_t* h);

19. partition_utils.h

检测source指定的设备,前4096字节是否全0或者全0xff。

int partition_wiped(const char* source);

20. properties.h

操作property函数。

intproperty_get(const char* key, char* value, const char* default_value);
int8_t property_get_bool(const char *key, int8_t default_value);
int64_t property_get_int64(const char *key, int64_t default_value);
int32_t property_get_int32(const char *key, int32_t default_value);

int property_set(const char *key, const char *value);
int property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie);

21. qtaguid.h

对libnetd_client.so库几个方法的封装。

extern int qtaguid_tagSocket(int sockfd, int tag, uid_t uid);
extern int qtaguid_untagSocket(int sockfd);
extern int qtaguid_setCounterSet(int counterSetNum, uid_t uid);
extern int qtaguid_deleteTagData(int tag, uid_t uid);
extern int qtaguid_setPacifier(int on);

22. record_stream.h

几个辅助函数,提供从stream中读取固定大小的record功能。

struct RecordStream {
    
    
    int fd;
    size_t maxRecordLen;

    unsigned char *buffer;

    unsigned char *unconsumed;
    unsigned char *read_end;
    unsigned char *buffer_end;
};
extern RecordStream *record_stream_new(int fd, size_t maxRecordLen);
extern void record_stream_free(RecordStream *p_rs);
extern int record_stream_get_next (RecordStream *p_rs, void ** p_outRecord, 
                                    size_t *p_outRecordLen);

23. sched_policy.h

kernel CPUSETS/SCHEDTUNE 功能设置。

#include <processgroup/sched_policy.h>

24. sockets.h

获取init.rc中名为name的socket,返回fd。

int android_get_control_socket(const char* name);

封装常用socket函数。

cutils_socket_t socket_network_client(const char* host, int port, int type);
int socket_network_client_timeout(const char* host, int port, int type,
                                  int timeout, int* getaddrinfo_error);
int socket_local_server(const char* name, int namespaceId, int type);
int socket_local_server_bind(int s, const char* name, int namespaceId);
int socket_local_client_connect(int fd, const char *name, int namespaceId, int type);
int socket_local_client(const char* name, int namespaceId, int type);
cutils_socket_t socket_inaddr_any_server(int port, int type);
int socket_close(cutils_socket_t sock);
int socket_get_local_port(cutils_socket_t sock);
ssize_t socket_send_buffers(cutils_socket_t sock,
                            const cutils_socket_buffer_t* buffers,
                            size_t num_buffers);

25. str_parms.h

利用自定义Hashmap实现str_parms及相关操作。不知道干什么用的。

26. threads.h

废弃:
1)gettid -> android::base::GetThreadId
2)thread_store -> _Thread_local © or thread_local (C++)

// syscall(__NR_gettid);
extern pid_t gettid();

extern void*  thread_store_get(thread_store_t*  store);
extern void   thread_store_set(thread_store_t*          store,
                               void*                    value,
                               thread_store_destruct_t  destroy);

27. trace.h

atrace相关函数,以及ATRACE_TAG_xxx系列宏定义。

void atrace_setup();
void atrace_update_tags();
void atrace_set_debuggable(bool debuggable);
void atrace_set_tracing_enabled(bool enabled);

#define ATRACE_INIT() atrace_init()
#define ATRACE_GET_ENABLED_TAGS() atrace_get_enabled_tags()

void atrace_init();
uint64_t atrace_get_enabled_tags();

#define ATRACE_ENABLED() atrace_is_tag_enabled(ATRACE_TAG)
static inline uint64_t atrace_is_tag_enabled(uint64_t tag)

#define ATRACE_BEGIN(name) atrace_begin(ATRACE_TAG, name)
static inline void atrace_begin(uint64_t tag, const char* name)

#define ATRACE_END() atrace_end(ATRACE_TAG)
static inline void atrace_end(uint64_t tag)

#define ATRACE_ASYNC_BEGIN(name, cookie) atrace_async_begin(ATRACE_TAG, name, cookie)
static inline void atrace_async_begin(uint64_t tag, const char* name, int32_t cookie)

#define ATRACE_ASYNC_END(name, cookie) atrace_async_end(ATRACE_TAG, name, cookie)
static inline void atrace_async_end(uint64_t tag, const char* name, int32_t cookie)

#define ATRACE_INT(name, value) atrace_int(ATRACE_TAG, name, value)
static inline void atrace_int(uint64_t tag, const char* name, int32_t value)

#define ATRACE_INT64(name, value) atrace_int64(ATRACE_TAG, name, value)
static inline void atrace_int64(uint64_t tag, const char* name, int64_t value)

28. uevent.h

封装netlink ipc函数。

int uevent_open_socket(int buf_sz, bool passcred);
ssize_t uevent_kernel_multicast_recv(int socket, void *buffer, size_t length);
ssize_t uevent_kernel_multicast_uid_recv(int socket, void *buffer, size_t length, uid_t *uid);
ssize_t uevent_kernel_recv(int socket, void *buffer, size_t length, bool require_group, uid_t *uid);

猜你喜欢

转载自blog.csdn.net/yinminsumeng/article/details/131167121