From ded7475569f0949b934e602d23751a02632b5456 Mon Sep 17 00:00:00 2001 From: liujia178 Date: Wed, 10 Apr 2024 16:40:45 +0800 Subject: [PATCH] Supplement hilog for network time ldso. Issue: https://gitee.com/openharmony/third_party_musl/issues/I9FGDT?from=project-issue Test: libc-test Signed-off-by: liujia178 --- ldso/linux/cfi.c | 4 +- ldso/linux/dynlink.c | 159 +++++++++++++++++++++++++++----- ldso/linux/namespace.c | 16 ++++ ldso/linux/ns_config.c | 44 +++++++-- ldso/linux/strops.c | 21 ++++- src/hilog/linux/hilog_adapter.c | 4 +- src/network/accept4.c | 7 +- src/network/dn_comp.c | 20 +++- src/network/dn_expand.c | 8 +- src/network/dns_parse.c | 15 ++- src/network/ether.c | 6 +- src/network/freeaddrinfo.c | 1 + src/network/getaddrinfo.c | 5 +- src/network/gethostbyaddr.c | 2 + src/network/gethostbyaddr_r.c | 2 + src/network/gethostbyname2.c | 2 + src/network/gethostbyname2_r.c | 2 + src/network/getifaddrs.c | 9 +- src/network/getnameinfo.c | 26 +++++- src/network/getservbyname.c | 5 +- src/network/getservbyname_r.c | 10 +- src/network/getservbyport.c | 5 +- src/network/getservbyport_r.c | 16 +++- src/network/getsockopt.c | 7 +- src/network/if_indextoname.c | 8 +- src/network/if_nameindex.c | 17 +++- src/network/if_nametoindex.c | 7 +- src/network/inet_addr.c | 8 +- src/network/inet_ntoa.c | 2 + src/network/inet_ntop.c | 4 + src/network/inet_pton.c | 3 + src/network/linux/getaddrinfo.c | 11 ++- src/network/lookup.h | 21 +++++ src/network/lookup_name.c | 30 +++++- src/network/lookup_serv.c | 22 ++++- src/network/netlink.c | 11 ++- src/network/ns_parse.c | 39 ++++++-- src/network/proto.c | 11 ++- src/network/recvmmsg.c | 3 +- src/network/recvmsg.c | 2 + src/network/res_mkquery.c | 8 +- src/network/res_msend.c | 6 +- src/network/res_query.c | 12 ++- src/network/res_querydomain.c | 7 +- src/network/res_send.c | 4 + src/network/sendmmsg.c | 8 +- src/network/sendmsg.c | 3 + src/network/setsockopt.c | 3 + src/network/sockatmark.c | 6 +- src/network/socketpair.c | 8 +- src/sigchain/linux/sigchain.c | 14 +-- src/time/__map_file.c | 7 +- src/time/__month_to_secs.c | 4 + src/time/__secs_to_tm.c | 8 +- src/time/__tm_to_secs.c | 1 + src/time/asctime_r.c | 11 +++ src/time/clock.c | 11 ++- src/time/clock_getcpuclockid.c | 7 +- src/time/ctime.c | 6 +- src/time/getdate.c | 4 +- src/time/gettimeofday.c | 6 +- src/time/gmtime_r.c | 1 + src/time/localtime_r.c | 2 + src/time/strftime.c | 1 + src/time/strptime.c | 5 + src/time/time_impl.h | 25 +++++ src/time/timegm.c | 2 + src/time/timer_create.c | 13 ++- src/time/timer_delete.c | 2 + src/time/timer_getoverrun.c | 2 + src/time/timer_settime.c | 3 + src/time/timespec_get.c | 8 +- src/time/utime.c | 3 + src/time/wcsftime.c | 6 +- 74 files changed, 690 insertions(+), 122 deletions(-) diff --git a/ldso/linux/cfi.c b/ldso/linux/cfi.c index 4f180f018..4b72d24c1 100644 --- a/ldso/linux/cfi.c +++ b/ldso/linux/cfi.c @@ -496,7 +496,7 @@ void __cfi_slowpath(uint64_t call_site_type_id, void *func_ptr) LD_LOGD("[CFI] [%{public}s] called from dso[%{public}s] to dso[%{public}s] func_ptr[%{public}p]\n", __FUNCTION__, ((struct dso *)addr2dso((size_t)__builtin_return_address(0)))->name, - ((struct dso *)addr2dso((size_t)func_ptr))->name, + ((struct dso *)addr2dso((size_t)func_ptr)) ? ((struct dso *)addr2dso((size_t)func_ptr))->name : NULL, func_ptr); cfi_slowpath_common(call_site_type_id, func_ptr, NULL); @@ -508,7 +508,7 @@ void __cfi_slowpath_diag(uint64_t call_site_type_id, void *func_ptr, void *diag_ LD_LOGD("[CFI] [%{public}s] called from dso[%{public}s] to dso[%{public}s] func_ptr[%{public}p]\n", __FUNCTION__, ((struct dso *)addr2dso((size_t)__builtin_return_address(0)))->name, - ((struct dso *)addr2dso((size_t)func_ptr))->name, + ((struct dso *)addr2dso((size_t)func_ptr)) ? ((struct dso *)addr2dso((size_t)func_ptr))->name : NULL, func_ptr); cfi_slowpath_common(call_site_type_id, func_ptr, diag_data); diff --git a/ldso/linux/dynlink.c b/ldso/linux/dynlink.c index b56887b6b..ced9cb971 100644 --- a/ldso/linux/dynlink.c +++ b/ldso/linux/dynlink.c @@ -221,10 +221,11 @@ static void get_sys_path(ns_configor *conf); static void dlclose_ns(struct dso *p); static bool get_app_path(char *path, size_t size) { + LD_LOGD("get_app_path path:%{public}s size:%{public}zu", path, size); int l = 0; l = readlink("/proc/self/exe", path, size); if (l < 0 || l >= size) { - LD_LOGD("get_app_path readlink failed!"); + LD_LOGE("get_app_path: readlink failed!"); return false; } path[l] = 0; @@ -254,6 +255,7 @@ static void init_default_namespace(struct dso *app) UT_STATIC void set_ns_attrs(ns_t *ns, ns_configor *conf) { if (!ns || !conf) { + LD_LOGE("set_ns_attrs: ns or conf is null."); return; } @@ -291,6 +293,7 @@ UT_STATIC void set_ns_attrs(ns_t *ns, ns_configor *conf) UT_STATIC void set_ns_inherits(ns_t *ns, ns_configor *conf) { if (!ns || !conf) { + LD_LOGE("set_ns_attrs: ns or conf is null."); return; } @@ -318,6 +321,7 @@ UT_STATIC void set_ns_inherits(ns_t *ns, ns_configor *conf) static void init_namespace(struct dso *app) { + LD_LOGD("init_namespace name:%{public}s", app ? app->name : "NULL"); char app_path[PATH_MAX + 1]; if (!get_app_path(app_path, sizeof app_path)) { strcpy(app_path, app->name); @@ -604,10 +608,6 @@ static Sym *gnu_lookup(struct sym_info_pair s_info_p, uint32_t *hashtab, struct if (h2 & 1) break; } - - LD_LOGD("gnu_lookup symbol not found, " - "so:%{public}s s:%{public}s v:%{public}s use_vna_hash:%{public}d vna_hash:%{public}x", - dso->name, verinfo->s, verinfo->v, verinfo->use_vna_hash, verinfo->vna_hash); return 0; } @@ -667,6 +667,7 @@ static void add_dso_parent(struct dso *p, struct dso *parent) if (p->parents_capacity == 0) { p->parents = (struct dso **)malloc(sizeof(struct dso *) * PARENTS_BASE_CAPACITY); if (!p->parents) { + LD_LOGE("add_dso_parent: malloc failed!"); return; } p->parents_capacity = PARENTS_BASE_CAPACITY; @@ -674,6 +675,7 @@ static void add_dso_parent(struct dso *p, struct dso *parent) struct dso ** realloced = (struct dso **)realloc( p->parents, sizeof(struct dso *) * (p->parents_capacity + PARENTS_BASE_CAPACITY)); if (!realloced) { + LD_LOGE("add_dso_parent: realloc failed!"); return; } p->parents = realloced; @@ -688,6 +690,7 @@ static void remove_dso_parent(struct dso *p, struct dso *parent) { int index = find_dso_parent(p, parent); if (index == -1) { + LD_LOGD("remove_dso_parent: find dso parent failed!"); return; } int i; @@ -707,6 +710,7 @@ static void add_reloc_can_search_dso(struct dso *p, struct dso *can_search_so) p->reloc_can_search_dso_list = (struct dso **)malloc(sizeof(struct dso *) * RELOC_CAN_SEARCH_DSO_BASE_CAPACITY); if (!p->reloc_can_search_dso_list) { + LD_LOGE("add_reloc_can_search_dso: malloc failed!"); return; } p->reloc_can_search_dso_capacity = RELOC_CAN_SEARCH_DSO_BASE_CAPACITY; @@ -715,6 +719,7 @@ static void add_reloc_can_search_dso(struct dso *p, struct dso *can_search_so) p->reloc_can_search_dso_list, sizeof(struct dso *) * (p->reloc_can_search_dso_capacity + RELOC_CAN_SEARCH_DSO_BASE_CAPACITY)); if (!realloced) { + LD_LOGE("add_reloc_can_search_dso: realloc failed!"); return; } p->reloc_can_search_dso_list = realloced; @@ -827,6 +832,7 @@ struct symdef find_sym_impl( static inline struct symdef find_sym2(struct dso *dso, struct verinfo *verinfo, int need_def, int use_deps, ns_t *ns) { + LD_LOGD("find_sym2 need_def:%{public}d use_deps:%{public}d", need_def, use_deps); struct sym_info_pair s_info_g = gnu_hash(verinfo->s); struct sym_info_pair s_info_s = {0, 0}; uint32_t gh = s_info_g.sym_h, gho = gh / (8 * sizeof(size_t)), *ght; @@ -865,6 +871,7 @@ static inline struct symdef find_sym2(struct dso *dso, struct verinfo *verinfo, static inline struct symdef find_sym_by_deps(struct dso *dso, struct verinfo *verinfo, int need_def, ns_t *ns) { + LD_LOGD("find_sym_by_deps need_def:%{public}d verinfo->s:%{public}s", need_def, verinfo ? verinfo->s : "NULL"); struct sym_info_pair s_info_g = gnu_hash(verinfo->s); struct sym_info_pair s_info_s = {0, 0}; uint32_t h = 0, gh = s_info_g.sym_h, gho = gh / (8 * sizeof(size_t)), *ght; @@ -941,6 +948,7 @@ static inline struct symdef find_sym_by_saved_so_list( static struct symdef find_sym(struct dso *dso, const char *s, int need_def) { + LD_LOGD("find_sym dso:%{public}s s:%{public}s need_def:%{public}d", dso ? dso->name : "NULL", s, need_def); struct verinfo verinfo = { .s = s, .v = "", .use_vna_hash = false }; return find_sym2(dso, &verinfo, need_def, 0, NULL); } @@ -984,6 +992,7 @@ static bool get_vna_hash(struct dso *dso, int sym_index, uint32_t *vna_hash) verneed = (Verneed *)((char *)verneed + verneed->vn_next); } + LD_LOGD("get_vna_hash result:%{public}d", result); return result; } @@ -1295,10 +1304,16 @@ UT_STATIC void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t return p; } ssize_t r; - if (lseek(fd, off, SEEK_SET) < 0) return MAP_FAILED; + if (lseek(fd, off, SEEK_SET) < 0) { + LD_LOGE("mmap_fixed: lseek failed!"); + return MAP_FAILED; + } for (q=p; n; q+=r, off+=r, n-=r) { r = read(fd, q, n); - if (r < 0 && errno != EINTR) return MAP_FAILED; + if (r < 0 && errno != EINTR) { + LD_LOGE("mmap_fixed: read failed!"); + return MAP_FAILED; + } if (!r) { memset(q, 0, n); break; @@ -1309,6 +1324,7 @@ UT_STATIC void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t UT_STATIC void unmap_library(struct dso *dso) { + LD_LOGD("unmap_library name:%{public}s", dso ? dso->name: "NULL"); if (dso->loadmap) { size_t i; for (i=0; iloadmap->nsegs; i++) { @@ -1337,11 +1353,13 @@ UT_STATIC bool get_random(void *buf, size_t buflen) int ret; int fd = open("/dev/urandom", O_RDONLY); if (fd < 0) { + LD_LOGD("get_random: open /dev/urandom failed!"); return false; } ret = read(fd, buf, buflen); if (ret < 0) { + LD_LOGD("get_random: read failed!"); close(fd); return false; } @@ -1381,8 +1399,10 @@ static bool get_transparent_hugepages_supported(void) goto done; read_size = read(fd, buf, HUGEPAGES_SUPPORTED_STR_SIZE - 1); - if (read_size < 0) + if (read_size < 0) { + LD_LOGE("get_transparent_hugepages_supported: read failed!"); goto close_fd; + } buf[HUGEPAGES_SUPPORTED_STR_SIZE - 1] = '\0'; if (strstr(buf, "[never]") == NULL) @@ -1410,7 +1430,7 @@ static size_t phdr_table_get_maxinum_alignment(Phdr *phdr_table, size_t phdr_cou if (phdr->p_align > maxinum_alignment) maxinum_alignment = phdr->p_align; } - + LD_LOGD("phdr_table_get_maxinum_alignment maxinum_alignment:%{public}zu", maxinum_alignment); return maxinum_alignment; #else return PAGE_SIZE; @@ -1419,6 +1439,7 @@ static size_t phdr_table_get_maxinum_alignment(Phdr *phdr_table, size_t phdr_cou UT_STATIC void *map_library(int fd, struct dso *dso, struct reserved_address_params *reserved_params) { + LD_LOGD("map_library fd:%{public}d dso_name:%{public}s", fd, dso ? dso->name : "NULL"); Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)]; void *allocated_buf=0; size_t phsize; @@ -1440,13 +1461,19 @@ UT_STATIC void *map_library(int fd, struct dso *dso, struct reserved_address_par ssize_t l = read(fd, buf, sizeof buf); eh = buf; - if (l<0) return 0; + if (l<0) { + LD_LOGE("map_library: read failed!"); + return 0; + } if (le_type != ET_DYN && eh->e_type != ET_EXEC)) goto noexec; phsize = eh->e_phentsize * eh->e_phnum; if (phsize > sizeof buf - sizeof *eh) { allocated_buf = malloc(phsize); - if (!allocated_buf) return 0; + if (!allocated_buf) { + LD_LOGE("map_library: malloc failed!"); + return 0; + } l = pread(fd, allocated_buf, phsize, eh->e_phoff); if (l < 0) goto error; if (l != phsize) goto noexec; @@ -1494,7 +1521,10 @@ UT_STATIC void *map_library(int fd, struct dso *dso, struct reserved_address_par if (DL_FDPIC && !(eh->e_flags & FDPIC_CONSTDISP_FLAG)) { dso->loadmap = calloc(1, sizeof *dso->loadmap + nsegs * sizeof *dso->loadmap->segs); - if (!dso->loadmap) goto error; + if (!dso->loadmap) { + LD_LOGE("map_library: calloc failed!"); + goto error; + } dso->loadmap->nsegs = nsegs; for (ph=ph0, i=0; ie_phentsize)) { if (ph->p_type != PT_LOAD) continue; @@ -1568,6 +1598,7 @@ UT_STATIC void *map_library(int fd, struct dso *dso, struct reserved_address_par ? mmap((void *)start_addr, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) : mmap((void *)start_addr, map_len, prot, map_flags, fd, off_start); if (map == MAP_FAILED) { + LD_LOGE("map_library: reserved_params exist, map failed!"); goto error; } if (reserved_params && map_len < reserved_params->reserved_size) { @@ -1579,6 +1610,7 @@ UT_STATIC void *map_library(int fd, struct dso *dso, struct reserved_address_par /* use tmp_map_len to mmap enough space for the dso with anonymous mapping */ unsigned char *temp_map = mmap((void *)NULL, tmp_map_len, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (temp_map == MAP_FAILED) { + LD_LOGE("map_library: reserved_params does not exist, map failed!"); goto error; } @@ -1593,6 +1625,7 @@ UT_STATIC void *map_library(int fd, struct dso *dso, struct reserved_address_par /* use map_len to mmap correct space for the dso with file mapping */ : mmap(real_map, map_len, prot, map_flags, fd, off_start); if (map == MAP_FAILED) { + LD_LOGE("map_library: use map_len to mmap correct space, map failed!"); goto error; } } @@ -1602,6 +1635,7 @@ UT_STATIC void *map_library(int fd, struct dso *dso, struct reserved_address_par * not available, then the load operation must fail. */ if (eh->e_type != ET_DYN && addr_min && map!=(void *)addr_min) { errno = EBUSY; + LD_LOGE("map_library: load operation failed!"); goto error; } base = map - addr_min; @@ -1667,6 +1701,7 @@ error: static int path_open(const char *name, const char *s, char *buf, size_t buf_size) { + LD_LOGD("path_open name:%{public}s s:%{public}s", name, s); size_t l; int fd; for (;;) { @@ -1693,6 +1728,7 @@ static int path_open(const char *name, const char *s, char *buf, size_t buf_size UT_STATIC int fixup_rpath(struct dso *p, char *buf, size_t buf_size) { + LD_LOGD("fixup_rpath rpath:%{public}s rpath_orig:%{public}s", p ? p->rpath : "NULL", p ? p->rpath_orig : "NULL"); size_t n, l; const char *s, *t, *origin; char *d; @@ -1727,6 +1763,7 @@ UT_STATIC int fixup_rpath(struct dso *p, char *buf, size_t buf_size) case EACCES: break; default: + LD_LOGE("fixup_rpath: readlink failed!"); return -1; } if (l >= buf_size) @@ -1751,7 +1788,10 @@ UT_STATIC int fixup_rpath(struct dso *p, char *buf, size_t buf_size) if (libc.secure && *origin != '/') return 0; p->rpath = malloc(strlen(p->rpath_orig) + n*l + 1); - if (!p->rpath) return -1; + if (!p->rpath) { + LD_LOGE("fixup_rpath: malloc failed!"); + return -1; + } d = p->rpath; s = p->rpath_orig; @@ -1920,6 +1960,7 @@ static inline int app_has_same_name_so(const char *so_name, const ns_t *ns) char tmp_buf[PATH_MAX + 1]; fd = path_open(so_name, ns->lib_paths, tmp_buf, sizeof tmp_buf); } + LD_LOGD("app_has_same_name_so fd:%{public}d", fd); return fd; } @@ -1968,6 +2009,11 @@ UT_STATIC struct dso *find_library_by_fstat(const struct stat *st, const ns_t *n struct dso *load_library( const char *name, struct dso *needed_by, ns_t *namespace, bool check_inherited, struct reserved_address_params *reserved_params) { + LD_LOGD("load_library name:%{public}s needed_by_name:%{public}s ns_name:%{public}s check_inherited:%{public}d", + name, + needed_by ? needed_by->name : "NULL", + namespace ? namespace->ns_name : "NULL", + check_inherited); char buf[PATH_MAX + 1]; const char *pathname; unsigned char *map; @@ -1980,6 +2026,7 @@ struct dso *load_library( if (!*name) { errno = EINVAL; + LD_LOGE("load_library: name is null."); return 0; } @@ -2101,7 +2148,10 @@ struct dso *load_library( } map = noload ? 0 : map_library(fd, &temp_dso, reserved_params); close(fd); - if (!map) return 0; + if (!map) { + LD_LOGE("load_library: map is null."); + return 0; + } /* Avoid the danger of getting two versions of libc mapped into the * same process when an absolute pathname was used. The symbols @@ -2134,6 +2184,7 @@ struct dso *load_library( p = calloc(1, alloc_size); if (!p) { unmap_library(&temp_dso); + LD_LOGE("load_library: calloc failed!"); return 0; } memcpy(p, &temp_dso, sizeof temp_dso); @@ -2226,7 +2277,10 @@ static void load_direct_deps(struct dso *p, ns_t *namespace, struct reserved_add static void load_deps(struct dso *p, struct reserved_address_params *reserved_params) { - if (p->deps) return; + if (p->deps) { + LD_LOGW("load_deps: deps already loaded."); + return; + } for (; p; p = p->next) load_direct_deps(p, p->namespace, reserved_params); } @@ -2286,6 +2340,7 @@ static void extend_bfs_deps(struct dso *p) #ifndef LOAD_ORDER_RANDOMIZATION static void load_preload(char *s, ns_t *ns) { + LD_LOGD("load_preload s:%{public}s", s); int tmp; char *z; for (z=s; *z; s=z) { @@ -2349,6 +2404,7 @@ static uint8_t* sleb128_decoder(uint8_t* current, uint8_t* end, size_t* value) do { if (current >= end) { + LD_LOGE("sleb128_decoder: current >= end, a_crash!"); a_crash(); } @@ -2569,6 +2625,7 @@ void __libc_exit_fini() void __ldso_atfork(int who) { + LD_LOGD("__ldso_atfork who:%{public}d", who); if (who<0) { pthread_rwlock_wrlock(&lock); pthread_mutex_lock(&init_fini_lock); @@ -2823,7 +2880,10 @@ hidden void __dls2(unsigned char *base, size_t *sp) apply_addends_to = rel; for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t)) if (!IS_RELATIVE(rel[1], ldso.syms)) symbolic_rel_cnt++; - if (symbolic_rel_cnt >= ADDEND_LIMIT) a_crash(); + if (symbolic_rel_cnt >= ADDEND_LIMIT) { + LD_LOGE("__dls2: symbolic_rel_cnt>= ADDEND_LIMIT, a_crash!"); + a_crash(); + } size_t addends[symbolic_rel_cnt+1]; saved_addends = addends; @@ -2856,6 +2916,7 @@ void __dls2b(size_t *sp, size_t *auxv, size_t *aux) libc.tls_size = sizeof builtin_tls; libc.tls_align = tls_align; if (__init_tp(__copy_tls((void *)builtin_tls)) < 0) { + LD_LOGE("__dls2b:__init_tp failed, a_crash!"); a_crash(); } __pthread_self()->stack = (void *)(sp + 1); @@ -2974,11 +3035,13 @@ void __dls3(size_t *sp, size_t *auxv, size_t *aux) } fd = open(argv[0], O_RDONLY); if (fd < 0) { + LD_LOGE("__dls3: open failed!"); dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno)); _exit(1); } Ehdr *ehdr = map_library(fd, &app, NULL); if (!ehdr) { + LD_LOGE("__dls3: map_library failed!"); dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]); _exit(1); } @@ -3052,6 +3115,7 @@ void __dls3(size_t *sp, size_t *auxv, size_t *aux) #ifdef LOAD_ORDER_RANDOMIZATION struct loadtasks *tasks = create_loadtasks(); if (!tasks) { + LD_LOGE("__dls3: create_loadtasks failed!"); _exit(1); } if (env_preload) { @@ -3129,6 +3193,7 @@ void __dls3(size_t *sp, size_t *auxv, size_t *aux) if (libc.tls_size > sizeof builtin_tls || tls_align > MIN_TLS_ALIGN) { initial_tls = calloc(libc.tls_size, 1); if (!initial_tls) { + LD_LOGE("__dls3: calloc failed!"); dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n", argv[0], libc.tls_size); _exit(127); @@ -3150,6 +3215,7 @@ void __dls3(size_t *sp, size_t *auxv, size_t *aux) pthread_t self = __pthread_self(); pthread_t td = __copy_tls(initial_tls); if (__init_tp(td) < 0) { + LD_LOGE("__dls3: __init_tp(td) failed, a_crash!"); a_crash(); } td->tsd = self->tsd; @@ -3165,6 +3231,7 @@ void __dls3(size_t *sp, size_t *auxv, size_t *aux) } if (init_cfi_shadow(head, &ldso) == CFI_FAILED) { + LD_LOGE("__dls3: init_cfi_shadow failed!"); error("[%s] init_cfi_shadow failed: %m", __FUNCTION__); } @@ -3214,6 +3281,7 @@ static void prepare_lazy(struct dso *p) p->lazy = calloc(n, 3*sizeof(size_t)); if (!p->lazy) { error("Error preparing lazy relocation for %s: %m", p->name); + LD_LOGE("prepare_lazy: calloc failed!"); longjmp(*rtld_fail, 1); } p->lazy_next = lazy_head; @@ -3222,6 +3290,7 @@ static void prepare_lazy(struct dso *p) static void *dlopen_post(struct dso* p, int mode) { if (p == NULL) { + LD_LOGE("dlopen_post: so is null."); return p; } bool is_dlclose_debug = false; @@ -3304,6 +3373,7 @@ static bool is_permitted(const void *caller_addr, char *target) static void *dlopen_impl( const char *file, int mode, const char *namespace, const void *caller_addr, const dl_extinfo *extinfo) { + LD_LOGD("dlopen_impl file:%{public}s mode:%{public}d namespace:%{public}s", file, mode, namespace); struct dso *volatile p, *orig_tail, *orig_syms_tail, *orig_lazy_head, *next; struct tls_module *orig_tls_tail; size_t orig_tls_cnt, orig_tls_offset, orig_tls_align; @@ -3580,15 +3650,18 @@ void *dlopen(const char *file, int mode) void dlns_init(Dl_namespace *dlns, const char *name) { if (!dlns) { + LD_LOGE("dlns_init: dlns is null."); return; } if (!name) { dlns->name[0] = 0; + LD_LOGE("dlns_init: name is null."); return; } const void *caller_addr = __builtin_return_address(0); if (is_permitted(caller_addr, name) == false) { + LD_LOGE("dlns_init: no permission!"); return; } @@ -3630,6 +3703,7 @@ void *dlopen_ns(Dl_namespace *dlns, const char *file, int mode) { const void *caller_addr = __builtin_return_address(0); if (is_permitted(caller_addr, dlns->name) == false) { + LD_LOGE("dlopen_ns: no permission!"); return NULL; } @@ -3647,6 +3721,7 @@ void *dlopen_ns_ext(Dl_namespace *dlns, const char *file, int mode, const dl_ext { const void *caller_addr = __builtin_return_address(0); if (is_permitted(caller_addr, dlns->name) == false) { + LD_LOGE("dlopen_ns_ext: no permission!"); return NULL; } @@ -3664,6 +3739,10 @@ void *dlopen_ns_ext(Dl_namespace *dlns, const char *file, int mode, const dl_ext int dlns_create2(Dl_namespace *dlns, const char *lib_path, int flags) { + LD_LOGD("dlns_create2 name:%{public}s lib_path:%{public}s flags:%{public}d", + dlns ? dlns->name: "NULL", + lib_path, + flags); if (!dlns) { LD_LOGE("dlns_create2 dlns is null."); return EINVAL; @@ -3673,6 +3752,7 @@ int dlns_create2(Dl_namespace *dlns, const char *lib_path, int flags) pthread_rwlock_wrlock(&lock); const void *caller_addr = __builtin_return_address(0); if (is_permitted(caller_addr, dlns->name) == false) { + LD_LOGE("dlns_create2: no permission!"); pthread_rwlock_unlock(&lock); return EPERM; } @@ -3726,6 +3806,10 @@ int dlns_create(Dl_namespace *dlns, const char *lib_path) int dlns_inherit(Dl_namespace *dlns, Dl_namespace *inherited, const char *shared_libs) { + LD_LOGD("dlns_inherit dlns_name:%{public}s, inherited:%{public}s, shared_libs:%{public}s", + dlns ? dlns->name : "NULL", + inherited ? inherited->name : "NULL", + shared_libs); if (!dlns || !inherited) { LD_LOGE("dlns_inherit dlns or inherited is null."); return EINVAL; @@ -3734,6 +3818,7 @@ int dlns_inherit(Dl_namespace *dlns, Dl_namespace *inherited, const char *shared pthread_rwlock_wrlock(&lock); const void *caller_addr = __builtin_return_address(0); if (is_permitted(caller_addr, dlns->name) == false) { + LD_LOGE("dlns_inherit: no permission!"); pthread_rwlock_unlock(&lock); return EPERM; } @@ -3753,9 +3838,15 @@ int dlns_inherit(Dl_namespace *dlns, Dl_namespace *inherited, const char *shared static void dlclose_ns(struct dso *p) { - if (!p) return; + if (!p) { + LD_LOGW("dlclose_ns: dso is null."); + return; + } ns_t * ns = p->namespace; - if (!ns || !ns->ns_dsos) return; + if (!ns || !ns->ns_dsos) { + LD_LOGW("dlclose_ns: namespace or dso list is null."); + return; + } for (size_t i = 0; i < ns->ns_dsos->num; i++) { if (p == ns->ns_dsos->dsos[i]) { for (size_t j = i + 1; j < ns->ns_dsos->num; j++) { @@ -3898,6 +3989,7 @@ static int dlclose_post(struct dso *p) static int dlclose_impl(struct dso *p) { + LD_LOGD("dlclose_impl dso_name:%{public}s", p ? p->name : "NULL"); struct dso *d; trace_marker_reset(); @@ -4216,7 +4308,10 @@ int dladdr(const void *addr_arg, Dl_info *info) p = addr2dso(addr); pthread_rwlock_unlock(&lock); - if (!p) return 0; + if (!p) { + LD_LOGE("dladdr: no matched dso found!"); + return 0; + } strings = p->strings; size_t addr_offset_so = addr - (size_t)p->base; @@ -4249,6 +4344,7 @@ hidden void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra if ((p != RTLD_DEFAULT) && (p != RTLD_NEXT)) { struct dso *dso = find_dso_by_handle(p); if (dso == NULL) { + LD_LOGE("__dlsym: dso not found!"); pthread_rwlock_unlock(&lock); return 0; } @@ -4271,6 +4367,7 @@ hidden void *__dlvsym(void *restrict p, const char *restrict s, const char *rest if ((p != RTLD_DEFAULT) && (p != RTLD_NEXT)) { struct dso *dso = find_dso_by_handle(p); if (dso == NULL) { + LD_LOGE("__dlvsym: dso not found!"); pthread_rwlock_unlock(&lock); return 0; } @@ -4356,8 +4453,9 @@ static void error(const char *fmt, ...) int dlns_set_namespace_lib_path(const char * name, const char * lib_path) { + LD_LOGD("dlns_set_namespace_lib_path name:%{public}s lib_path:%{public}s", name, lib_path); if (!name || !lib_path) { - LD_LOGE("dlns_set_namespace_lib_path name or lib_path is null."); + LD_LOGE("dlns_set_namespace_lib_path: name or lib_path is null."); return EINVAL; } @@ -4365,6 +4463,7 @@ int dlns_set_namespace_lib_path(const char * name, const char * lib_path) const void *caller_addr = __builtin_return_address(0); if (is_permitted(caller_addr, name) == false) { pthread_rwlock_unlock(&lock); + LD_LOGE("dlns_set_namespace_lib_path: no permission!"); return EPERM; } @@ -4382,8 +4481,9 @@ int dlns_set_namespace_lib_path(const char * name, const char * lib_path) int dlns_set_namespace_separated(const char * name, const bool separated) { + LD_LOGD("dlns_set_namespace_separated name:%{public}s separated:%{public}d", name, separated); if (!name) { - LD_LOGE("dlns_set_namespace_separated name is null."); + LD_LOGE("dlns_set_namespace_separated: name is null."); return EINVAL; } @@ -4391,6 +4491,7 @@ int dlns_set_namespace_separated(const char * name, const bool separated) const void *caller_addr = __builtin_return_address(0); if (is_permitted(caller_addr, name) == false) { pthread_rwlock_unlock(&lock); + LD_LOGE("dlns_set_namespace_separated: no permission!"); return EPERM; } @@ -4408,6 +4509,7 @@ int dlns_set_namespace_separated(const char * name, const bool separated) int dlns_set_namespace_permitted_paths(const char * name, const char * permitted_paths) { + LD_LOGD("dlns_set_namespace_permitted_paths name:%s permitted_paths:%s", name, permitted_paths); if (!name || !permitted_paths) { LD_LOGE("dlns_set_namespace_permitted_paths name or permitted_paths is null."); return EINVAL; @@ -4417,6 +4519,7 @@ int dlns_set_namespace_permitted_paths(const char * name, const char * permitted const void *caller_addr = __builtin_return_address(0); if (is_permitted(caller_addr, name) == false) { pthread_rwlock_unlock(&lock); + LD_LOGE("dlns_set_namespace_permitted_paths: no permission!"); return EPERM; } @@ -4434,6 +4537,7 @@ int dlns_set_namespace_permitted_paths(const char * name, const char * permitted int dlns_set_namespace_allowed_libs(const char * name, const char * allowed_libs) { + LD_LOGD("dlns_set_namespace_allowed_libs name:%{public}s allowed_libs:%{public}s", name, allowed_libs); if (!name || !allowed_libs) { LD_LOGE("dlns_set_namespace_allowed_libs name or allowed_libs is null."); return EINVAL; @@ -4443,6 +4547,7 @@ int dlns_set_namespace_allowed_libs(const char * name, const char * allowed_libs const void *caller_addr = __builtin_return_address(0); if (is_permitted(caller_addr, name) == false) { pthread_rwlock_unlock(&lock); + LD_LOGE("dlns_set_namespace_allowed_libs: no permission!"); return EPERM; } @@ -4879,6 +4984,7 @@ static bool task_map_library(struct loadtask *task, struct reserved_address_para if (DL_FDPIC && !(task->eh->e_flags & FDPIC_CONSTDISP_FLAG)) { task->p->loadmap = calloc(1, sizeof(struct fdpic_loadmap) + nsegs * sizeof(struct fdpic_loadseg)); if (!task->p->loadmap) { + LD_LOGE("task_map_library: calloc failed!"); goto error; } task->p->loadmap->nsegs = nsegs; @@ -4893,6 +4999,7 @@ static bool task_map_library(struct loadtask *task, struct reserved_address_para prot, MAP_PRIVATE, task->fd, ph->p_offset & -PAGE_SIZE + task->file_offset); if (map == MAP_FAILED) { + LD_LOGE("task_map_library: mmap failed!"); unmap_library(task->p); goto error; } @@ -4958,6 +5065,7 @@ static bool task_map_library(struct loadtask *task, struct reserved_address_para ? mmap((void *)start_addr, map_len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0) : mmap((void *)start_addr, map_len, prot, map_flags, task->fd, off_start + task->file_offset); if (map == MAP_FAILED) { + LD_LOGE("task_map_library: reserved_params exists, mmap failed!"); goto error; } if (reserved_params && map_len < reserved_params->reserved_size) { @@ -4969,6 +5077,7 @@ static bool task_map_library(struct loadtask *task, struct reserved_address_para /* use tmp_map_len to mmap enough space for the dso with anonymous mapping */ unsigned char *temp_map = mmap((void *)NULL, tmp_map_len, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (temp_map == MAP_FAILED) { + LD_LOGE("task_map_library: reserved_params does not exist, mmap failed!"); goto error; } @@ -4983,6 +5092,7 @@ static bool task_map_library(struct loadtask *task, struct reserved_address_para /* use map_len to mmap correct space for the dso with file mapping */ : mmap(real_map, map_len, prot, map_flags, task->fd, off_start + task->file_offset); if (map == MAP_FAILED) { + LD_LOGE("task_map_library: use map_len to mmap correct space, mmap failed!"); goto error; } } @@ -5094,10 +5204,12 @@ static bool resolve_fd_to_realpath(struct loadtask *task) int ret = snprintf(proc_self_fd, sizeof(proc_self_fd), "/proc/self/fd/%d", task->fd); if (ret < 0 || ret >= sizeof(proc_self_fd)) { + LD_LOGE("resolve_fd_to_realpath: snprintf failed!"); return false; } ssize_t len = readlink(proc_self_fd, resolved_path, sizeof(resolved_path) - 1); if (len < 0) { + LD_LOGE("resolve_fd_to_realpath: readlink failed!"); return false; } resolved_path[len] = '\0'; @@ -5108,6 +5220,7 @@ static bool resolve_fd_to_realpath(struct loadtask *task) static bool load_library_header(struct loadtask *task) { + LD_LOGD("load_library_header task:%{public}s", task ? task->name : "NULL"); const char *name = task->name; struct dso *needed_by = task->needed_by; ns_t *namespace = task->namespace; @@ -5121,6 +5234,7 @@ static bool load_library_header(struct loadtask *task) int is_self = 0; if (!*name) { + LD_LOGE("load_library_header: name is null."); errno = EINVAL; return false; } @@ -5517,7 +5631,9 @@ static void unmap_preloaded_sections(struct loadtasks *tasks) static void preload_deps(struct dso *p, struct loadtasks *tasks) { + LD_LOGD("preload_deps p_name:%{public}s", p ? p->name : "NULL"); if (p->deps) { + LD_LOGD("preload_deps: deps already loaded."); return; } for (; p; p = p->next) { @@ -5569,6 +5685,7 @@ UT_STATIC void assign_tls(struct dso *p) UT_STATIC void load_preload(char *s, ns_t *ns, struct loadtasks *tasks) { + LD_LOGD("load_preload s:%{public}s ns_name:%{public}s", s, ns ? ns->ns_name : "NULL"); int tmp; char *z; diff --git a/ldso/linux/namespace.c b/ldso/linux/namespace.c index a68028450..69710c84f 100644 --- a/ldso/linux/namespace.c +++ b/ldso/linux/namespace.c @@ -72,6 +72,7 @@ static void nsinherits_free(ns_inherit_list *nsinl) UT_STATIC void nsinherits_realloc(ns_inherit_list *nsinl) { if (!nsinl) { + LD_LOGE("nsinherits_realloc: ns inherit list is null."); return; } size_t size = 2 * nsinl->size; @@ -108,6 +109,7 @@ static dsolist *dsolist_alloc() static void dsolist_realloc(dsolist *dsol) { if (!dsol) { + LD_LOGE("dsolist_realloc: dso list is null."); return; } size_t size = 2 * dsol->size; @@ -171,12 +173,14 @@ void ns_free(ns_t *ns) void ns_add_dso(ns_t *ns, struct dso *dso) { if (!ns || !dso) { + LD_LOGE("ns_add_dso: namespace or dso is null."); return; } if (!ns->ns_dsos) { ns->ns_dsos = dsolist_alloc(); } if (!ns->ns_dsos) { + LD_LOGE("ns_add_dso: dsolist_alloc failed!"); return; } if (ns->ns_dsos->num == ns->ns_dsos->size) { @@ -222,6 +226,7 @@ static void nslist_realloc() void nslist_add_ns(ns_t *ns) { if (!ns) { + LD_LOGE("nslist_add_ns: namespace is null."); return; } @@ -246,6 +251,7 @@ ns_t *get_default_ns() void ns_set_name(ns_t *ns, const char *name) { if (!ns || !name) { + LD_LOGE("ns_set_name: namespace or name is null."); return; } if (ns->ns_name) __libc_free(ns->ns_name); @@ -257,6 +263,7 @@ void ns_set_name(ns_t *ns, const char *name) void ns_set_env_paths(ns_t *ns, const char *env_paths) { if (!ns) { + LD_LOGE("ns_set_env_paths: namespace is null."); return; } if (ns->env_paths) __libc_free(ns->env_paths); @@ -272,6 +279,7 @@ void ns_set_env_paths(ns_t *ns, const char *env_paths) void ns_set_lib_paths(ns_t *ns, const char *lib_paths) { if (!ns) { + LD_LOGE("ns_set_lib_paths: namespace is null."); return; } if (ns->lib_paths) __libc_free(ns->lib_paths); @@ -287,6 +295,7 @@ void ns_set_lib_paths(ns_t *ns, const char *lib_paths) void ns_set_asan_lib_paths(ns_t *ns, const char *asan_lib_paths) { if (!ns) { + LD_LOGE("ns_set_asan_lib_paths: namespace is null."); return; } if (ns->asan_lib_paths) { @@ -304,6 +313,7 @@ void ns_set_asan_lib_paths(ns_t *ns, const char *asan_lib_paths) void ns_set_permitted_paths(ns_t *ns, const char *permitted_paths) { if (!ns) { + LD_LOGE("ns_set_permitted_paths: namespace is null."); return; } if (ns->permitted_paths) strlist_free(ns->permitted_paths); @@ -314,6 +324,7 @@ void ns_set_permitted_paths(ns_t *ns, const char *permitted_paths) void ns_set_asan_permitted_paths(ns_t *ns, const char *asan_permitted_paths) { if (!ns) { + LD_LOGE("ns_set_asan_permitted_paths: namespace is null."); return; } if (ns->asan_permitted_paths) { @@ -328,6 +339,7 @@ void ns_set_asan_permitted_paths(ns_t *ns, const char *asan_permitted_paths) void ns_set_separated(ns_t *ns, bool separated) { if (!ns) { + LD_LOGE("ns_set_separated: namespace is null."); return; } ns->separated = separated; @@ -337,6 +349,7 @@ void ns_set_separated(ns_t *ns, bool separated) void ns_set_allowed_libs(ns_t *ns, const char *allowed_libs) { if (!ns) { + LD_LOGE("ns_set_allowed_libs: namespace is null."); return; } @@ -372,6 +385,7 @@ ns_t *find_ns_by_name(const char *ns_name) static ns_inherit *find_ns_inherit(ns_t *ns, ns_t *inherited) { if (!ns || !inherited) { + LD_LOGE("find_ns_inherit: namespace or inherited is null."); return NULL; } if (ns->ns_inherits) { @@ -388,6 +402,7 @@ void ns_add_inherit(ns_t *ns, ns_t *ns_inherited, const char *shared_libs) { bool need_add = false; if (!ns || !ns_inherited) { + LD_LOGE("ns_add_inherit: namespace or ns_inherited is null."); return; } @@ -606,6 +621,7 @@ bool is_sharable(ns_inherit *inherit, const char *lib_name) void ns_set_flag(ns_t *ns, int flag) { if (!ns) { + LD_LOGE("ns_set_flag: namespace is null."); return; } ns->flag = flag; diff --git a/ldso/linux/ns_config.c b/ldso/linux/ns_config.c index e0d8c365e..5dee399cd 100644 --- a/ldso/linux/ns_config.c +++ b/ldso/linux/ns_config.c @@ -139,10 +139,16 @@ UT_STATIC void kvlist_realloc(kvlist *kvs) if (size) { char **keys, **vals; keys = (char **)__libc_realloc(kvs->key, size * (sizeof *kvs->key)); - if (!keys) return; + if (!keys) { + LD_LOGE("kvlist_realloc: __libc_realloc keys failed!"); + return; + } kvs->key = keys; vals = (char **)__libc_realloc(kvs->val, size * (sizeof *kvs->val)); - if (!vals) return; + if (!vals) { + LD_LOGD("kvlist_realloc: __libc_realloc vals failed!"); + return; + } kvs->val = vals; kvs->size = size; } @@ -153,7 +159,10 @@ UT_STATIC void kvlist_realloc(kvlist *kvs) static void kvlist_free(kvlist *kvs) { size_t i; - if (!kvs) return; + if (!kvs) { + LD_LOGD("kvlist_free: kv list is null."); + return; + } for (i = 0; i < kvs->num; i++) { __libc_free(kvs->key[i]); __libc_free(kvs->val[i]); @@ -187,16 +196,25 @@ static section_list *sections_alloc(size_t size) UT_STATIC void sections_realloc(section_list *sections) { - if (!sections) return; + if (!sections) { + LD_LOGE("sections_realloc: section list is null"); + return; + } size_t size = 2 * sections->size; if (size) { char **names; kvlist **kvs; names = (char **)__libc_realloc(sections->names, size * (sizeof *sections->names)); - if (!names) return; + if (!names) { + LD_LOGE("sections_realloc: __libc_realloc names failed!"); + return; + } sections->names = names; kvs = (kvlist **)__libc_realloc(sections->kvs, size * (sizeof *sections->kvs)); - if (!kvs) return; + if (!kvs) { + LD_LOGE("sections_realloc: __libc_realloc kvs failed!"); + return; + } sections->kvs = kvs; sections->size = size; } @@ -218,7 +236,10 @@ static void sections_free(section_list *sections) static void kvlist_set(kvlist *kvs, const char *key, const char *val) { size_t i; - if (!kvs || !key || !val) return; + if (!kvs || !key || !val) { + LD_LOGE("kvlist_set: invailed argument!"); + return; + } for (i = 0; i < kvs->num; i++) { if (!strcmp(kvs->key[i], key)) { @@ -253,7 +274,10 @@ static void kvlist_set(kvlist *kvs, const char *key, const char *val) static void sections_set(section_list *sections, const char *name, const char *key, const char *val) { kvlist* kvs = NULL; - if (!sections || !name || !key || !val) return; + if (!sections || !name || !key || !val) { + LD_LOGE("sections_set sections: invailed argument!"); + return; + } for(size_t i = 0; i < sections->num; i++) { if (!strcmp(sections->names[i], name)) { @@ -305,6 +329,7 @@ static section_list *config_load(const char *filepath) sections = sections_alloc(0); if (!sections) { fclose(file); + LD_LOGE("config_load: sections_alloc failed!"); return NULL; } @@ -389,6 +414,7 @@ static kvlist *config_get_kvs(const char *sname) static char *config_get_value_by_acquiescence(kvlist *acquiescence_kvs, const char *key) { if (!acquiescence_kvs) { + LD_LOGE("config_get_value_by_acquiescence: kv list is null."); return NULL; } size_t i; @@ -404,6 +430,7 @@ static char *config_get_value_by_acquiescence(kvlist *acquiescence_kvs, const ch static char *config_get_acquiescence_lib_path(kvlist *acquiescence_kvs) { if (!acquiescence_kvs) { + LD_LOGE("config_get_acquiescence_lib_path: kv list is null."); return NULL; } config_key_join(ATTR_NS_PREFIX, true); @@ -418,6 +445,7 @@ static char *config_get_acquiescence_lib_path(kvlist *acquiescence_kvs) static char *config_get_acquiescence_asan_lib_path(kvlist *acquiescence_kvs) { if (!acquiescence_kvs) { + LD_LOGE("config_get_acquiescence_asan_lib_path: kv list is null."); return NULL; } config_key_join(ATTR_NS_PREFIX, true); diff --git a/ldso/linux/strops.c b/ldso/linux/strops.c index 47915f8c7..98628349b 100644 --- a/ldso/linux/strops.c +++ b/ldso/linux/strops.c @@ -15,6 +15,7 @@ #include #include "strops.h" +#include "ld_log.h" /* string to lower */ void strlwc(char *str) @@ -95,7 +96,10 @@ strlist *strlist_alloc(size_t size) static void strlist_realloc(strlist *strs) { - if (!strs) return; + if(!strs) { + LD_LOGE("strlist_realloc: strlist is null."); + return; + } size_t size = 2 * strs->size; if (size) { char **ss = (char **)__libc_realloc(strs->strs, size * (sizeof *strs->strs)); @@ -109,7 +113,10 @@ static void strlist_realloc(strlist *strs) void strlist_free(strlist *strs) { - if (!strs) return; + if (!strs) { + LD_LOGE("strlist_free: strlist is null."); + return; + } for (size_t i = 0; i < strs->num; i++) { __libc_free(strs->strs[i]); } @@ -119,7 +126,10 @@ void strlist_free(strlist *strs) void strlist_set(strlist *strs, const char *str) { - if (!strs || !str) return; + if (!strs || !str) { + LD_LOGE("strlist_set: invailed argument!"); + return; + } if (strs->num == strs->size) { strlist_realloc(strs); } @@ -133,6 +143,9 @@ char *ld_strdup(const char *s) { size_t l = strlen(s); char *d = __libc_malloc(l + 1); - if (!d) return NULL; + if (!d) { + LD_LOGE("ld_strdup: __libc_malloc failed!"); + return NULL; + } return memcpy(d, s, l + 1); } \ No newline at end of file diff --git a/src/hilog/linux/hilog_adapter.c b/src/hilog/linux/hilog_adapter.c index ad94d7434..0cda706ce 100644 --- a/src/hilog/linux/hilog_adapter.c +++ b/src/hilog/linux/hilog_adapter.c @@ -53,7 +53,7 @@ static bool musl_log_enable = false; static const char *param_name = "musl.log.enable"; static const char *g_logLevelParam = "musl.log.level"; #endif -static int g_logLevel = LOG_ERROR; +static int g_logLevel = LOG_INFO; static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER; static volatile int g_socketFd = INVALID_SOCKET; @@ -208,7 +208,7 @@ void resetLogLevel() { static CachedHandle muslLogLevelHandle = NULL; if (muslLogLevelHandle == NULL) { - muslLogLevelHandle = CachedParameterCreate(g_logLevelParam, "ERROR"); + muslLogLevelHandle = CachedParameterCreate(g_logLevelParam, "INFO"); } const char *value = CachedParameterGet(muslLogLevelHandle); if (value != NULL) { diff --git a/src/network/accept4.c b/src/network/accept4.c index 59ab1726b..18787e972 100644 --- a/src/network/accept4.c +++ b/src/network/accept4.c @@ -3,14 +3,19 @@ #include #include #include "syscall.h" +#include "lookup.h" int accept4(int fd, struct sockaddr *restrict addr, socklen_t *restrict len, int flg) { + NETWORK_LOGD("accept4 fd:%{public}d len:%{public}d flg:%{public}d", fd, len, flg); if (!flg) return accept(fd, addr, len); int ret = socketcall_cp(accept4, fd, addr, len, flg, 0, 0); if (ret>=0 || (errno != ENOSYS && errno != EINVAL)) return ret; ret = accept(fd, addr, len); - if (ret<0) return ret; + if (ret<0) { + NETWORK_LOGD("accept4: accept failed!"); + return ret; + } if (flg & SOCK_CLOEXEC) __syscall(SYS_fcntl, ret, F_SETFD, FD_CLOEXEC); if (flg & SOCK_NONBLOCK) diff --git a/src/network/dn_comp.c b/src/network/dn_comp.c index f0ccd160f..cf010a58e 100644 --- a/src/network/dn_comp.c +++ b/src/network/dn_comp.c @@ -1,6 +1,6 @@ #include #include - +#include "lookup.h" /* RFC 1035 message compression */ /* label start offsets of a compressed domain name s */ @@ -56,21 +56,28 @@ static int match(int *offset, const unsigned char *base, const unsigned char *dn int dn_comp(const char *src, unsigned char *dst, int space, unsigned char **dnptrs, unsigned char **lastdnptr) { + NETWORK_LOGD("dn_comp src:%{public}s space:%{public}d", src, space); int i, j, n, m=0, offset, bestlen=0, bestoff; unsigned char lens[127]; unsigned char **p; const char *end; size_t l = strnlen(src, 255); if (l && src[l-1] == '.') l--; - if (l>253 || space<=0) return -1; + if (l>253 || space<=0) { + NETWORK_LOGE("dn_comp: dns name length exceeds 253 or space is 0."); + return -1; + } if (!l) { *dst = 0; + NETWORK_LOGW("dn_comp: dns name length is 0."); return 1; } end = src+l; n = getlens(lens, src, l); - if (!n) return -1; - + if (!n) { + NETWORK_LOGE("dn_comp: getlens failed."); + return -1; + } p = dnptrs; if (p && *p) for (p++; *p; p++) { m = match(&offset, *dnptrs, *p, end, lens, n); @@ -83,7 +90,10 @@ int dn_comp(const char *src, unsigned char *dst, int space, unsigned char **dnpt } /* encode unmatched part */ - if (space < l-bestlen+2+(bestlen-1 < l-1)) return -1; + if (space < l-bestlen+2+(bestlen-1 < l-1)) { + NETWORK_LOGE("dn_comp: space is not enough."); + return -1; + } memcpy(dst+1, src, l-bestlen); for (i=j=0; i +#include "lookup.h" int __dn_expand(const unsigned char *base, const unsigned char *end, const unsigned char *src, char *dest, int space) { + NETWORK_LOGD("__dn_expand base:%{public}s end:%{public}s src:%{public}s space:%{public}d", + base, end, src, space); const unsigned char *p = src; char *dend, *dbegin = dest; int len = -1, i, j; - if (p==end || space <= 0) return -1; + if (p==end || space <= 0) { + NETWORK_LOGE("__dn_expand: invalid paramter."); + return -1; + } dend = dest + (space > 254 ? 254 : space); /* detect reference loop using an iteration counter */ for (i=0; i < end-base; i+=2) { diff --git a/src/network/dns_parse.c b/src/network/dns_parse.c index e6ee19d9a..601b44b8c 100644 --- a/src/network/dns_parse.c +++ b/src/network/dns_parse.c @@ -7,12 +7,21 @@ int __dns_parse(const unsigned char *r, int rlen, int (*callback)(void *, int, c const unsigned char *p; int len; - if (rlen<12) return -1; - if ((r[3]&15)) return 0; + if (rlen<12) { + NETWORK_LOGE("__dns_parse: rlen less than 12."); + return -1; + } + if ((r[3]&15)) { + NETWORK_LOGW("__dns_parse: not a response message."); + return 0; + } p = r+12; qdcount = r[4]*256 + r[5]; ancount = r[6]*256 + r[7]; - if (qdcount+ancount > 64) return -1; + if (qdcount+ancount > 64) { + NETWORK_LOGE("__dns_parse: the count of question and answer exceeds 64."); + return -1; + } while (qdcount--) { while (p-r < rlen && *p-1U < 127) p++; if (*p>193 || (*p==193 && p[1]>254) || p>r+rlen-6) diff --git a/src/network/ether.c b/src/network/ether.c index 2e8d55db6..707b7d861 100644 --- a/src/network/ether.c +++ b/src/network/ether.c @@ -2,6 +2,7 @@ #include #include #include +#include "lookup.h" #ifdef __LITEOS_A__ #include #include @@ -25,7 +26,10 @@ struct ether_addr *ether_aton_r (const char *x, struct ether_addr *p_a) if (n > 0xFF) return 0; /* bad byte */ a.ether_addr_octet[ii] = n; } - if (x[0] != 0) return 0; /* bad format */ + if (x[0] != 0) { + NETWORK_LOGE("ether_aton_r: bad format"); + return 0; /* bad format */ + } *p_a = a; return p_a; } diff --git a/src/network/freeaddrinfo.c b/src/network/freeaddrinfo.c index 3b6f83375..404f90cb4 100644 --- a/src/network/freeaddrinfo.c +++ b/src/network/freeaddrinfo.c @@ -7,6 +7,7 @@ void freeaddrinfo(struct addrinfo *p) { if (!p) { + NETWORK_LOGE("freeaddrinfo: addrinfo is null."); return; } size_t cnt; diff --git a/src/network/getaddrinfo.c b/src/network/getaddrinfo.c index 0d0a6222d..98264cc39 100644 --- a/src/network/getaddrinfo.c +++ b/src/network/getaddrinfo.c @@ -101,7 +101,10 @@ int getaddrinfo(const char *restrict host, const char *restrict serv, const stru nais = nservs * naddrs; canon_len = strlen(canon); out = calloc(1, nais * sizeof(*out) + canon_len + 1); - if (!out) return EAI_MEMORY; + if (!out) { + NETWORK_LOGE("getaddrinfo: calloc falied!"); + return EAI_MEMORY; + } if (canon_len) { outcanon = (void *)&out[nais]; diff --git a/src/network/gethostbyaddr.c b/src/network/gethostbyaddr.c index 598e2241a..68c7d8b12 100644 --- a/src/network/gethostbyaddr.c +++ b/src/network/gethostbyaddr.c @@ -3,6 +3,7 @@ #include #include #include +#include "lookup.h" struct hostent *gethostbyaddr(const void *a, socklen_t l, int af) { @@ -14,6 +15,7 @@ struct hostent *gethostbyaddr(const void *a, socklen_t l, int af) free(h); h = malloc(size+=size+1); if (!h) { + NETWORK_LOGE("gethostbyaddr: malloc failed!"); h_errno = NO_RECOVERY; return 0; } diff --git a/src/network/gethostbyaddr_r.c b/src/network/gethostbyaddr_r.c index 0f1e61aa0..0659444fb 100644 --- a/src/network/gethostbyaddr_r.c +++ b/src/network/gethostbyaddr_r.c @@ -6,11 +6,13 @@ #include #include #include +#include "lookup.h" int gethostbyaddr_r(const void *a, socklen_t l, int af, struct hostent *h, char *buf, size_t buflen, struct hostent **res, int *err) { + NETWORK_LOGD("gethostbyaddr_r l:%{public}d af:%{public}d buf:%{public}s buflen:%{public}zu", l, af, buf, buflen); union { struct sockaddr_in sin; struct sockaddr_in6 sin6; diff --git a/src/network/gethostbyname2.c b/src/network/gethostbyname2.c index dc9d6621b..72ec106f4 100644 --- a/src/network/gethostbyname2.c +++ b/src/network/gethostbyname2.c @@ -4,6 +4,7 @@ #include #include #include +#include "lookup.h" struct hostent *gethostbyname2(const char *name, int af) { @@ -15,6 +16,7 @@ struct hostent *gethostbyname2(const char *name, int af) free(h); h = malloc(size+=size+1); if (!h) { + NETWORK_LOGE("gethostbyname2: malloc failed!"); h_errno = NO_RECOVERY; return 0; } diff --git a/src/network/gethostbyname2_r.c b/src/network/gethostbyname2_r.c index fc8948776..3fdb16f39 100644 --- a/src/network/gethostbyname2_r.c +++ b/src/network/gethostbyname2_r.c @@ -12,6 +12,8 @@ int gethostbyname2_r(const char *name, int af, struct hostent *h, char *buf, size_t buflen, struct hostent **res, int *err) { + NETWORK_LOGD("gethostbyname2_r name:%{public}s af:%{public}d buf:%{public}s buflen:%{public}zu", + name, af, buf, buflen); struct address addrs[MAXADDRS]; char canon[256]; int i, cnt; diff --git a/src/network/getifaddrs.c b/src/network/getifaddrs.c index fed75bd8d..fbaa2c90c 100644 --- a/src/network/getifaddrs.c +++ b/src/network/getifaddrs.c @@ -8,7 +8,7 @@ #include #include #include "netlink.h" - +#include "lookup.h" #define IFADDRS_HASH_SIZE 64 /* getifaddrs() reports hardware addresses with PF_PACKET that implies @@ -125,8 +125,11 @@ static int netlink_msg_to_ifaddr(void *pctx, struct nlmsghdr *h) } ifs = calloc(1, sizeof(struct ifaddrs_storage) + stats_len); - if (ifs == 0) return -1; - + if (ifs == 0) { + NETWORK_LOGE("netlink_msg_to_ifaddr: calloc failed!"); + return -1; + } + if (h->nlmsg_type == RTM_NEWLINK) { ifs->index = ifi->ifi_index; ifs->ifa.ifa_flags = ifi->ifi_flags; diff --git a/src/network/getnameinfo.c b/src/network/getnameinfo.c index a9a0aa65f..32e373856 100644 --- a/src/network/getnameinfo.c +++ b/src/network/getnameinfo.c @@ -147,6 +147,8 @@ int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl, char *restrict serv, socklen_t servlen, int flags) { + NETWORK_LOGD("getnameinfo sa:%{public}p sl:%{public}d nodelen:%{public}d servlen:%{public}d, flags:%{public}d", + sa, sl, nodelen, servlen, flags); char ptr[PTR_MAX]; char buf[256], num[3*sizeof(int)+1]; int af = sa->sa_family; @@ -156,13 +158,19 @@ int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl, switch (af) { case AF_INET: a = (void *)&((struct sockaddr_in *)sa)->sin_addr; - if (sl < sizeof(struct sockaddr_in)) return EAI_FAMILY; + if (sl < sizeof(struct sockaddr_in)) { + NETWORK_LOGE("getnameinfo: the socklen is not enough(IPv4)."); + return EAI_FAMILY; + } mkptr4(ptr, a); scopeid = 0; break; case AF_INET6: a = (void *)&((struct sockaddr_in6 *)sa)->sin6_addr; - if (sl < sizeof(struct sockaddr_in6)) return EAI_FAMILY; + if (sl < sizeof(struct sockaddr_in6)) { + NETWORK_LOGE("getnameinfo: the socklen is not enough(IPv6)."); + return EAI_FAMILY; + } if (memcmp(a, "\0\0\0\0\0\0\0\0\0\0\xff\xff", 12)) mkptr6(ptr, a); else @@ -189,7 +197,10 @@ int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl, __dns_parse(reply, rlen, dns_parse_callback, buf); } if (!*buf) { - if (flags & NI_NAMEREQD) return EAI_NONAME; + if (flags & NI_NAMEREQD) { + NETWORK_LOGE("getnameinfo: __dns_parse failed(NI_NAMEREQD)!"); + return EAI_NONAME; + } inet_ntop(af, a, buf, sizeof buf); if (scopeid) { char *p = 0, tmp[IF_NAMESIZE+1]; @@ -203,7 +214,10 @@ int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl, strcat(buf, p); } } - if (strlen(buf) >= nodelen) return EAI_OVERFLOW; + if (strlen(buf) >= nodelen) { + NETWORK_LOGE("getnameinfo: the nodelen is not enough."); + return EAI_OVERFLOW; + } strcpy(node, buf); } @@ -215,8 +229,10 @@ int getnameinfo(const struct sockaddr *restrict sa, socklen_t sl, reverse_services(buf, port, flags & NI_DGRAM); if (!*p) p = itoa(num, port); - if (strlen(p) >= servlen) + if (strlen(p) >= servlen) { + NETWORK_LOGE("getnameinfo: the servlen is not enough."); return EAI_OVERFLOW; + } strcpy(serv, p); } diff --git a/src/network/getservbyname.c b/src/network/getservbyname.c index dd3037678..1eb18c63d 100644 --- a/src/network/getservbyname.c +++ b/src/network/getservbyname.c @@ -1,12 +1,15 @@ #define _GNU_SOURCE #include +#include "lookup.h" struct servent *getservbyname(const char *name, const char *prots) { static struct servent se; static char *buf[2]; struct servent *res; - if (getservbyname_r(name, prots, &se, (void *)buf, sizeof buf, &res)) + if (getservbyname_r(name, prots, &se, (void *)buf, sizeof buf, &res)) { + NETWORK_LOGE("getservbyname: getservbyname_r failed."); return 0; + } return &se; } diff --git a/src/network/getservbyname_r.c b/src/network/getservbyname_r.c index cad6317ab..4626ede91 100644 --- a/src/network/getservbyname_r.c +++ b/src/network/getservbyname_r.c @@ -13,6 +13,7 @@ int getservbyname_r(const char *name, const char *prots, struct servent *se, char *buf, size_t buflen, struct servent **res) { + NETWORK_LOGD("getservbyname_r name:%{public}s prots:%{public}s buflen:%{public}lu", name, prots, buflen); struct service servs[MAXSERVS]; int cnt, proto, align; @@ -21,12 +22,17 @@ int getservbyname_r(const char *name, const char *prots, /* Don't treat numeric port number strings as service records. */ char *end = ""; strtoul(name, &end, 10); - if (!*end) return ENOENT; + if (!*end) { + NETWORK_LOGE("getservbyname_r: service name is invalid."); + return ENOENT; + } /* Align buffer */ align = -(uintptr_t)buf & ALIGN-1; - if (buflen < 2*sizeof(char *)+align) + if (buflen < 2*sizeof(char *)+align) { + NETWORK_LOGE("getservbyname_r: buflen is not enough."); return ERANGE; + } buf += align; if (!prots) proto = 0; diff --git a/src/network/getservbyport.c b/src/network/getservbyport.c index c9ecbb11c..f88be061d 100644 --- a/src/network/getservbyport.c +++ b/src/network/getservbyport.c @@ -1,12 +1,15 @@ #define _GNU_SOURCE #include +#include "lookup.h" struct servent *getservbyport(int port, const char *prots) { static struct servent se; static long buf[32/sizeof(long)]; struct servent *res; - if (getservbyport_r(port, prots, &se, (void *)buf, sizeof buf, &res)) + if (getservbyport_r(port, prots, &se, (void *)buf, sizeof buf, &res)) { + NETWORK_LOGE("getservbyname: getservbyport_r failed."); return 0; + } return &se; } diff --git a/src/network/getservbyport_r.c b/src/network/getservbyport_r.c index b7f21c6b3..6444af3e2 100644 --- a/src/network/getservbyport_r.c +++ b/src/network/getservbyport_r.c @@ -6,10 +6,12 @@ #include #include #include +#include "lookup.h" int getservbyport_r(int port, const char *prots, struct servent *se, char *buf, size_t buflen, struct servent **res) { + NETWORK_LOGD("getservbyport_r port:%{public}d prots:%{public}s buflen:%{public}lu", port, prots, buflen); int i; struct sockaddr_in sin = { .sin_family = AF_INET, @@ -26,12 +28,17 @@ int getservbyport_r(int port, const char *prots, /* Align buffer */ i = (uintptr_t)buf & sizeof(char *)-1; if (!i) i = sizeof(char *); - if (buflen < 3*sizeof(char *)-i) + if (buflen < 3*sizeof(char *)-i) { + NETWORK_LOGE("getservbyport_r: buflen is not enough."); return ERANGE; + } buf += sizeof(char *)-i; buflen -= sizeof(char *)-i; - if (strcmp(prots, "tcp") && strcmp(prots, "udp")) return EINVAL; + if (strcmp(prots, "tcp") && strcmp(prots, "udp")) { + NETWORK_LOGE("getservbyport_r: invalid protocol type!"); + return EINVAL; + } se->s_port = port; se->s_proto = (char *)prots; @@ -53,7 +60,10 @@ int getservbyport_r(int port, const char *prots, } /* A numeric port string is not a service record. */ - if (strtol(buf, 0, 10)==ntohs(port)) return ENOENT; + if (strtol(buf, 0, 10)==ntohs(port)) { + NETWORK_LOGE("getservbyport_r: A numeric port string is not a service record."); + return ENOENT; + } *res = se; return 0; diff --git a/src/network/getsockopt.c b/src/network/getsockopt.c index d3640d9c9..54c0474c6 100644 --- a/src/network/getsockopt.c +++ b/src/network/getsockopt.c @@ -2,9 +2,11 @@ #include #include #include "syscall.h" +#include "lookup.h" int getsockopt(int fd, int level, int optname, void *restrict optval, socklen_t *restrict optlen) { + NETWORK_LOGD("getsockopt fd:%{public}d level:%{public}d optname:%{public}d", fd, level, optname); long tv32[2]; struct timeval *tv; @@ -16,7 +18,10 @@ int getsockopt(int fd, int level, int optname, void *restrict optval, socklen_t case SO_RCVTIMEO: case SO_SNDTIMEO: if (SO_RCVTIMEO == SO_RCVTIMEO_OLD) break; - if (*optlen < sizeof *tv) return __syscall_ret(-EINVAL); + if (*optlen < sizeof *tv) { + NETWORK_LOGE("getsockopt: optlen is not enough."); + return __syscall_ret(-EINVAL); + } if (optname==SO_RCVTIMEO) optname=SO_RCVTIMEO_OLD; if (optname==SO_SNDTIMEO) optname=SO_SNDTIMEO_OLD; r = __socketcall(getsockopt, fd, level, optname, diff --git a/src/network/if_indextoname.c b/src/network/if_indextoname.c index 6bcc9819f..d986775cb 100644 --- a/src/network/if_indextoname.c +++ b/src/network/if_indextoname.c @@ -4,23 +4,29 @@ #include #include #include +#include "lookup.h" #include "syscall.h" char *if_indextoname(unsigned index, char *name) { + NETWORK_LOGD("if_indextoname index:%{public}d", index); struct ifreq ifr; int fd, r; #ifdef __LITEOS_A__ if ((fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0; #else - if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0; + if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) { + NETWORK_LOGE("if_indextoname: create socket failed!"); + return 0; + } #endif ifr.ifr_ifindex = index; r = ioctl(fd, SIOCGIFNAME, &ifr); __syscall(SYS_close, fd); if (r < 0) { if (errno == ENODEV) errno = ENXIO; + NETWORK_LOGE("if_indextoname: ioctl SIOCGIFNAME failed!"); return 0; } return strncpy(name, ifr.ifr_name, IF_NAMESIZE); diff --git a/src/network/if_nameindex.c b/src/network/if_nameindex.c index 2deaef769..226c1e2a6 100644 --- a/src/network/if_nameindex.c +++ b/src/network/if_nameindex.c @@ -6,7 +6,7 @@ #include #include #include "netlink.h" - +#include "lookup.h" #define IFADDRS_HASH_SIZE 64 struct ifnamemap { @@ -63,7 +63,10 @@ static int netlink_msg_to_nameindex(void *pctx, struct nlmsghdr *h) size_t a = ctx->allocated ? ctx->allocated * 2 + 1 : 8; if (a > SIZE_MAX/sizeof *map) return -1; map = realloc(ctx->list, a * sizeof *map); - if (!map) return -1; + if (!map) { + NETWORK_LOGE("netlink_msg_to_nameindex: realloc failed!"); + return -1; + } ctx->list = map; ctx->allocated = a; } @@ -91,10 +94,16 @@ struct if_nameindex *if_nameindex() pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); memset(ctx, 0, sizeof(*ctx)); - if (__rtnetlink_enumerate(AF_UNSPEC, AF_INET, netlink_msg_to_nameindex, ctx) < 0) goto err; + if (__rtnetlink_enumerate(AF_UNSPEC, AF_INET, netlink_msg_to_nameindex, ctx) < 0) { + NETWORK_LOGE("if_nameindex: __rtnetlink_enumerate failed."); + goto err; + } ifs = malloc(sizeof(struct if_nameindex[ctx->num+1]) + ctx->str_bytes); - if (!ifs) goto err; + if (!ifs) { + NETWORK_LOGD("if_nameindex: malloc failed!"); + goto err; + } p = (char*)(ifs + ctx->num + 1); for (i = ctx->num, d = ifs, s = ctx->list; i; i--, s++, d++) { diff --git a/src/network/if_nametoindex.c b/src/network/if_nametoindex.c index dc11a69b5..87542b51b 100644 --- a/src/network/if_nametoindex.c +++ b/src/network/if_nametoindex.c @@ -4,15 +4,20 @@ #include #include #include "syscall.h" +#include "lookup.h" unsigned if_nametoindex(const char *name) { + NETWORK_LOGD("if_nametoindex name:%{public}s", name); struct ifreq ifr; int fd, r; #ifdef __LITEOS_A__ if ((fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0; #else - if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0; + if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) { + NETWORK_LOGE("if_nametoindex: create socket failed!"); + return 0; + } #endif strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); r = ioctl(fd, SIOCGIFINDEX, &ifr); diff --git a/src/network/inet_addr.c b/src/network/inet_addr.c index 11ece3d6f..54102f346 100644 --- a/src/network/inet_addr.c +++ b/src/network/inet_addr.c @@ -1,10 +1,16 @@ #include #include #include +#include "lookup.h" in_addr_t inet_addr(const char *p) { + NETWORK_LOGD("inet_addr p:%{public}s", p); struct in_addr a; - if (!__inet_aton(p, &a)) return -1; + if (!__inet_aton(p, &a)) { + NETWORK_LOGE("inet_addr: __inet_aton failed."); + return -1; + } + NETWORK_LOGD("inet_addr s_addr:%{public}u", a.s_addr); return a.s_addr; } diff --git a/src/network/inet_ntoa.c b/src/network/inet_ntoa.c index 71411e0b5..fe0cfa47d 100644 --- a/src/network/inet_ntoa.c +++ b/src/network/inet_ntoa.c @@ -1,10 +1,12 @@ #include #include +#include "lookup.h" char *inet_ntoa(struct in_addr in) { static char buf[16]; unsigned char *a = (void *)∈ snprintf(buf, sizeof buf, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); + NETWORK_LOGD("inet_ntoa buf:%{public}s", buf); return buf; } diff --git a/src/network/inet_ntop.c b/src/network/inet_ntop.c index 4bfef2c55..78d8a6c0b 100644 --- a/src/network/inet_ntop.c +++ b/src/network/inet_ntop.c @@ -3,9 +3,11 @@ #include #include #include +#include "lookup.h" const char *inet_ntop(int af, const void *restrict a0, char *restrict s, socklen_t l) { + NETWORK_LOGD("inet_ntop af:%{public}d l:%{public}u", af, l); const unsigned char *a = a0; int i, j, max, best; char buf[100]; @@ -47,8 +49,10 @@ const char *inet_ntop(int af, const void *restrict a0, char *restrict s, socklen break; default: errno = EAFNOSUPPORT; + NETWORK_LOGE("inet_ntop: address family not supported by protocol."); return 0; } errno = ENOSPC; + NETWORK_LOGE("inet_ntop: the length of converted string exceeds the buffer size."); return 0; } diff --git a/src/network/inet_pton.c b/src/network/inet_pton.c index d36c36891..ae8c0a5be 100644 --- a/src/network/inet_pton.c +++ b/src/network/inet_pton.c @@ -3,6 +3,7 @@ #include #include #include +#include "lookup.h" static int hexval(unsigned c) { @@ -14,6 +15,7 @@ static int hexval(unsigned c) int inet_pton(int af, const char *restrict s, void *restrict a0) { + NETWORK_LOGD("inet_pton af:%{public}d, s:%{public}s", af, s); uint16_t ip[8]; unsigned char *a = a0; int i, j, v, d, brk=-1, need_v4=0; @@ -31,6 +33,7 @@ int inet_pton(int af, const char *restrict s, void *restrict a0) return 0; } else if (af!=AF_INET6) { errno = EAFNOSUPPORT; + NETWORK_LOGE("inet_pton: address family not supported by protocol."); return -1; } diff --git a/src/network/linux/getaddrinfo.c b/src/network/linux/getaddrinfo.c index 040ff61be..fdf263933 100644 --- a/src/network/linux/getaddrinfo.c +++ b/src/network/linux/getaddrinfo.c @@ -69,6 +69,7 @@ int getaddrinfo_hook(const char* host, const char* serv, const struct addrinfo* int getaddrinfo(const char *restrict host, const char *restrict serv, const struct addrinfo *restrict hint, struct addrinfo **restrict res) { + NETWORK_LOGD("getaddrinfo host:%{public}s serv:%{public}s", host, serv); struct queryparam param = {0, 0, 0, 0, NULL}; return getaddrinfo_ext(host, serv, hint, res, ¶m); } @@ -81,7 +82,10 @@ int getaddrinfo_ext(const char *restrict host, const char *restrict serv, const int usedtime = 0; time_t t_start, t_end; - if (!host && !serv) return EAI_NONAME; + if (!host && !serv) { + NETWORK_LOGE("getaddrinfo: host and service are null."); + return EAI_NONAME; + } if (!param) { netid = 0; type = 0; @@ -203,7 +207,10 @@ int getaddrinfo_ext(const char *restrict host, const char *restrict serv, const nais = nservs * naddrs; canon_len = strlen(canon); out = calloc(1, nais * sizeof(*out) + canon_len + 1); - if (!out) return EAI_MEMORY; + if (!out) { + NETWORK_LOGE("getaddrinfo_ext: calloc falied!"); + return EAI_MEMORY; + } if (canon_len) { outcanon = (void *)&out[nais]; diff --git a/src/network/lookup.h b/src/network/lookup.h index ad01b26d0..062fc870e 100644 --- a/src/network/lookup.h +++ b/src/network/lookup.h @@ -6,6 +6,9 @@ #include #include #include +#ifndef __LITEOS__ +#include +#endif #if OHOS_DNS_PROXY_BY_NETSYS @@ -20,6 +23,24 @@ #endif #endif +#define NETWORK_LOG_TAG "MUSL-NETWORK" + +#if (defined(OHOS_ENABLE_PARAMETER) || defined(ENABLE_MUSL_LOG)) && (!defined(__LITEOS__)) +#define NETWORK_LOGE(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_ERROR, \ + MUSL_LOG_DOMAIN, NETWORK_LOG_TAG, __VA_ARGS__)) +#define NETWORK_LOGI(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_INFO, \ + MUSL_LOG_DOMAIN, NETWORK_LOG_TAG, __VA_ARGS__)) +#define NETWORK_LOGD(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_DEBUG, \ + MUSL_LOG_DOMAIN, NETWORK_LOG_TAG, __VA_ARGS__)) +#define NETWORK_LOGW(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_WARN, \ + MUSL_LOG_DOMAIN, NETWORK_LOG_TAG, __VA_ARGS__)) +#else +#define NETWORK_LOGE(...) +#define NETWORK_LOGI(...) +#define NETWORK_LOGD(...) +#define NETWORK_LOGW(...) +#endif + struct aibuf { struct addrinfo ai; union sa { diff --git a/src/network/lookup_name.c b/src/network/lookup_name.c index de464e574..70a23fabc 100644 --- a/src/network/lookup_name.c +++ b/src/network/lookup_name.c @@ -241,6 +241,7 @@ static int name_from_dns_search(struct address buf[static MAXADDRS], char canon[ { #if OHOS_PERMISSION_INTERNET if (is_allow_internet() == 0) { + NETWORK_LOGE("name_from_dns_search: do not allow internet access."); errno = EPERM; return -1; } @@ -252,7 +253,10 @@ static int name_from_dns_search(struct address buf[static MAXADDRS], char canon[ char *p, *z; int res = get_resolv_conf_ext(&conf, search, sizeof search, netid); - if (res < 0) return res; + if (res < 0) { + NETWORK_LOGD("name_from_dns_search: get_resolv_conf_ext falied."); + return res; + } /* Count dots, suppress search when >=ndots or name ends in * a dot, which is an explicit request for global scope. */ @@ -376,8 +380,11 @@ int lookup_name_ext(struct address buf[static MAXADDRS], char canon[static 256], if (name) { /* reject empty name and check len so it fits into temp bufs */ size_t l = strnlen(name, 255); - if (l-1 >= 254) + if (l-1 >= 254) { + NETWORK_LOGE("lookup_name_ext: invalid name length."); return EAI_NONAME; + } + memcpy(canon, name, l+1); } @@ -692,23 +699,27 @@ int predefined_host_lookup_ip(const char* host, const char* serv, int predefined_host_set_hosts(const char* host_ips) { if (host_ips == NULL) { + NETWORK_LOGE("predefined_host_set_hosts: host_ips is NULL"); return EAI_NONAME; } int len = strlen(host_ips); if (len == 0) { + NETWORK_LOGE("predefined_host_set_hosts: len is 0"); return EAI_NONAME; } if (host_ips_list_ == NULL) { host_ips_list_ = create_linklist(); if (host_ips_list_ == NULL) { + NETWORK_LOGE("predefined_host_set_hosts: create_linklist failed!"); return EAI_MEMORY; } } char *host_ips_str = calloc(1, len + 1); if (host_ips_str == NULL) { + NETWORK_LOGE("predefined_host_set_hosts: calloc failed!"); return EAI_MEMORY; } @@ -724,17 +735,20 @@ int predefined_host_set_hosts(const char* host_ips) int predefined_host_set_host(const char* host, const char* ip) { if (host == NULL || strlen(host) == 0 || ip == NULL || strlen(ip) == 0) { + NETWORK_LOGD("predefined_host_set_host: invalid argument!"); return -1; } if (host_ips_list_ == NULL) { host_ips_list_ = create_linklist(); if (host_ips_list_ == NULL) { + NETWORK_LOGE("predefined_host_set_host: create_linklist failed!"); return EAI_NONAME; } } if (_predefined_host_is_contain_host_ip(host, ip, 1)) { + NETWORK_LOGW("predefined_host_set_host: host IP already exists."); return 0; } @@ -778,6 +792,7 @@ static linknode *create_linknode(void *data) { linknode *pnode = (linknode *)calloc(1, sizeof(linknode)); if (NULL == pnode) { + NETWORK_LOGE("create_linknode: calloc failed!"); return NULL; } pnode->_data = data; @@ -790,6 +805,7 @@ static linkList *create_linklist(void) { linkList *plist = (linkList *)calloc(1, sizeof(linkList)); if (NULL == plist) { + NETWORK_LOGE("create_linklist: calloc failed!"); return NULL; } plist->_phead = NULL; @@ -801,6 +817,7 @@ static linkList *create_linklist(void) static int destory_linklist(linkList *plist) { if (NULL == plist) { + NETWORK_LOGE("destory_linklist: link list is null"); return -1; } @@ -821,6 +838,7 @@ static int destory_linklist(linkList *plist) static int linklist_size(linkList *plist) { if (NULL == plist) { + NETWORK_LOGE("linklist_size: link list is null"); return 0; } @@ -830,6 +848,7 @@ static int linklist_size(linkList *plist) static linknode *get_linknode(linkList *plist, int index) { if (index < 0 || index >= plist->_count) { + NETWORK_LOGE("get_linknode: invalid index."); return NULL; } @@ -845,11 +864,13 @@ static linknode *get_linknode(linkList *plist, int index) static int linklist_append_last(linkList *plist, void *pdata) { if (NULL == plist) { + NETWORK_LOGE("linklist_append_last: link list is null"); return -1; } linknode *pnode = create_linknode(pdata); if (NULL == pnode) { + NETWORK_LOGE("linklist_append_last: create_linknode failed!"); return -1; } @@ -868,6 +889,7 @@ static int linklist_append_last(linkList *plist, void *pdata) static int linklist_delete(linkList *plist, int index) { if (NULL == plist || NULL == plist->_phead || plist->_count <= 0) { + NETWORK_LOGD("linklist_delete: plist is null"); return -1; } @@ -878,10 +900,12 @@ static int linklist_delete(linkList *plist, int index) } else { linknode *pindex = get_linknode(plist, index); if (NULL == pindex) { + NETWORK_LOGE("linklist_delete: get_linknode index failed!"); return -1; } linknode *preindex = get_linknode(plist, index - 1); if (NULL == preindex) { + NETWORK_LOGE("linklist_delete: get_linknode index - 1 failed!"); return -1; } @@ -897,6 +921,7 @@ static int linklist_delete(linkList *plist, int index) static int linklist_delete_first(linkList *plist) { if (NULL == plist || NULL == plist->_phead || plist->_count <= 0) { + NETWORK_LOGE("linklist_delete_first: link list is null."); return -1; } @@ -912,6 +937,7 @@ static int linklist_delete_first(linkList *plist) static int linklist_delete_last(linkList *plist) { if (NULL == plist || NULL == plist->_phead || plist->_count <= 0) { + NETWORK_LOGD("linklist_delete_last: link list is null."); return -1; } diff --git a/src/network/lookup_serv.c b/src/network/lookup_serv.c index 5833f0b1c..270c31356 100644 --- a/src/network/lookup_serv.c +++ b/src/network/lookup_serv.c @@ -25,6 +25,7 @@ int __lookup_serv(struct service buf[static MAXSERVS], const char *name, int pro case IPPROTO_TCP: break; default: + NETWORK_LOGE("__lookup_serv: invalid proto for SOCK_STREAM."); return EAI_SERVICE; } break; @@ -35,12 +36,16 @@ int __lookup_serv(struct service buf[static MAXSERVS], const char *name, int pro case IPPROTO_UDP: break; default: + NETWORK_LOGE("__lookup_serv: invalid proto for SOCK_DGRAM."); return EAI_SERVICE; } case 0: break; default: - if (name) return EAI_SERVICE; + if (name) { + NETWORK_LOGE("__lookup_serv: invalid socktype"); + return EAI_SERVICE; + } buf[0].port = 0; buf[0].proto = proto; buf[0].socktype = socktype; @@ -48,11 +53,17 @@ int __lookup_serv(struct service buf[static MAXSERVS], const char *name, int pro } if (name) { - if (!*name) return EAI_SERVICE; + if (!*name) { + NETWORK_LOGE("__lookup_serv: empty name."); + return EAI_SERVICE; + } port = strtoul(name, &z, 10); } if (!*z) { - if (port > 65535) return EAI_SERVICE; + if (port > 65535) { + NETWORK_LOGE("__lookup_serv: invalid port number."); + return EAI_SERVICE; + } if (proto != IPPROTO_UDP) { buf[cnt].port = port; buf[cnt].socktype = SOCK_STREAM; @@ -66,7 +77,10 @@ int __lookup_serv(struct service buf[static MAXSERVS], const char *name, int pro return cnt; } - if (flags & AI_NUMERICSERV) return EAI_NONAME; + if (flags & AI_NUMERICSERV) { + NETWORK_LOGE("__lookup_serv: numerical service name not supported."); + return EAI_NONAME; + } size_t l = strlen(name); diff --git a/src/network/netlink.c b/src/network/netlink.c index 94dba7f5c..d9c9d9ec8 100644 --- a/src/network/netlink.c +++ b/src/network/netlink.c @@ -3,6 +3,7 @@ #include #include #include "netlink.h" +#include "lookup.h" static int __netlink_enumerate(int fd, unsigned int seq, int type, int af, int (*cb)(void *ctx, struct nlmsghdr *h), void *ctx) @@ -25,7 +26,10 @@ static int __netlink_enumerate(int fd, unsigned int seq, int type, int af, u.req.nlh.nlmsg_seq = seq; u.req.g.rtgen_family = af; r = send(fd, &u.req, sizeof(u.req), 0); - if (r < 0) return r; + if (r < 0) { + NETWORK_LOGE("__netlink_enumerate: send failed."); + return r; + } while (1) { r = recv(fd, u.buf, sizeof(u.buf), MSG_DONTWAIT); @@ -44,7 +48,10 @@ int __rtnetlink_enumerate(int link_af, int addr_af, int (*cb)(void *ctx, struct int fd, r; fd = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_ROUTE); - if (fd < 0) return -1; + if (fd < 0) { + NETWORK_LOGE("__rtnetlink_enumerate: create socket failed!"); + return -1; + } r = __netlink_enumerate(fd, 1, RTM_GETLINK, link_af, cb, ctx); if (!r) r = __netlink_enumerate(fd, 2, RTM_GETADDR, addr_af, cb, ctx); __syscall(SYS_close,fd); diff --git a/src/network/ns_parse.c b/src/network/ns_parse.c index d01da47a3..c87231272 100644 --- a/src/network/ns_parse.c +++ b/src/network/ns_parse.c @@ -3,6 +3,7 @@ #include #include #include +#include "lookup.h" const struct _ns_flagdata _ns_flagdata[16] = { { 0x8000, 15 }, @@ -49,11 +50,15 @@ void ns_put32(unsigned long l, unsigned char *cp) int ns_initparse(const unsigned char *msg, int msglen, ns_msg *handle) { + NETWORK_LOGD("ns_initparse msg:%{public}s msglen:%{public}d", msg, msglen); int i, r; handle->_msg = msg; handle->_eom = msg + msglen; - if (msglen < (2 + ns_s_max) * NS_INT16SZ) goto bad; + if (msglen < (2 + ns_s_max) * NS_INT16SZ) { + NETWORK_LOGE("ns_initparse: the length of dns message is not enough."); + goto bad; + } NS_GET16(handle->_id, msg); NS_GET16(handle->_flags, msg); for (i = 0; i < ns_s_max; i++) NS_GET16(handle->_counts[i], msg); @@ -67,7 +72,10 @@ int ns_initparse(const unsigned char *msg, int msglen, ns_msg *handle) handle->_sections[i] = NULL; } } - if (msg != handle->_eom) goto bad; + if (msg != handle->_eom) { + NETWORK_LOGE("ns_initparse: the message has not been fully parsed."); + goto bad; + } handle->_sect = ns_s_max; handle->_rrnum = -1; handle->_msg_ptr = NULL; @@ -98,6 +106,7 @@ int ns_skiprr(const unsigned char *ptr, const unsigned char *eom, ns_sect sectio return p - ptr; bad: errno = EMSGSIZE; + NETWORK_LOGE("ns_initparse: message too long."); return -1; } @@ -105,26 +114,38 @@ int ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) { int r; - if (section < 0 || section >= ns_s_max) goto bad; + if (section < 0 || section >= ns_s_max) { + NETWORK_LOGE("ns_parserr: invailed section"); + goto bad; + } if (section != handle->_sect) { handle->_sect = section; handle->_rrnum = 0; handle->_msg_ptr = handle->_sections[section]; } if (rrnum == -1) rrnum = handle->_rrnum; - if (rrnum < 0 || rrnum >= handle->_counts[section]) goto bad; + if (rrnum < 0 || rrnum >= handle->_counts[section]) { + NETWORK_LOGE("ns_parserr: invailed rrnum"); + goto bad; + } if (rrnum < handle->_rrnum) { handle->_rrnum = 0; handle->_msg_ptr = handle->_sections[section]; } if (rrnum > handle->_rrnum) { r = ns_skiprr(handle->_msg_ptr, handle->_eom, section, rrnum - handle->_rrnum); - if (r < 0) return -1; + if (r < 0) { + NETWORK_LOGE("ns_parserr: ns_skiprr failed."); + return -1; + } handle->_msg_ptr += r; handle->_rrnum = rrnum; } r = ns_name_uncompress(handle->_msg, handle->_eom, handle->_msg_ptr, rr->name, NS_MAXDNAME); - if (r < 0) return -1; + if (r < 0) { + NETWORK_LOGD("ns_parserr: ns_name_uncompress failed."); + return -1; + } handle->_msg_ptr += r; if (2 * NS_INT16SZ > handle->_eom - handle->_msg_ptr) goto size; NS_GET16(rr->type, handle->_msg_ptr); @@ -157,6 +178,7 @@ bad: return -1; size: errno = EMSGSIZE; + NETWORK_LOGD("ns_parserr: message too long."); return -1; } @@ -165,7 +187,10 @@ int ns_name_uncompress(const unsigned char *msg, const unsigned char *eom, { int r; r = dn_expand(msg, eom, src, dst, dstsiz); - if (r < 0) errno = EMSGSIZE; + if (r < 0) { + NETWORK_LOGE("ns_name_uncompress: uncompress failed, dst size is not enough."); + errno = EMSGSIZE; + } return r; } diff --git a/src/network/proto.c b/src/network/proto.c index c4fd34efb..508422b9e 100644 --- a/src/network/proto.c +++ b/src/network/proto.c @@ -1,6 +1,6 @@ #include #include - +#include "lookup.h" /* do we really need all these?? */ static int idx; @@ -57,7 +57,10 @@ struct protoent *getprotoent(void) { static struct protoent p; static const char *aliases; - if (idx >= sizeof protos) return NULL; + if (idx >= sizeof protos) { + NETWORK_LOGE("getprotoent: idx exceeds the size of the protos array."); + return NULL; + } p.p_proto = protos[idx]; p.p_name = (char *)&protos[idx+1]; p.p_aliases = (char **)&aliases; @@ -67,18 +70,22 @@ struct protoent *getprotoent(void) struct protoent *getprotobyname(const char *name) { + NETWORK_LOGD("getprotobyname name:%{public}s", name); struct protoent *p; endprotoent(); do p = getprotoent(); while (p && strcmp(name, p->p_name)); + NETWORK_LOGD("getprotobyname p_name:%{public}s p_proto:%{public}d", p ? p->p_name : NULL, p ? p->p_proto : 0); return p; } struct protoent *getprotobynumber(int num) { + NETWORK_LOGD("getprotobynumber num:%{public}d", num); struct protoent *p; endprotoent(); do p = getprotoent(); while (p && p->p_proto != num); + NETWORK_LOGD("getprotobynumber p_name:%{public}s p_proto:%{public}d", p ? p->p_name : NULL, p ? p->p_proto : 0); return p; } diff --git a/src/network/recvmmsg.c b/src/network/recvmmsg.c index 2978e2f64..1d97715b8 100644 --- a/src/network/recvmmsg.c +++ b/src/network/recvmmsg.c @@ -4,7 +4,7 @@ #include #include #include "syscall.h" - +#include "lookup.h" #define IS32BIT(x) !((x)+0x80000000ULL>>32) #define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63)) @@ -12,6 +12,7 @@ hidden void __convert_scm_timestamps(struct msghdr *, socklen_t); int recvmmsg(int fd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags, struct timespec *timeout) { + NETWORK_LOGD("recvmmsg fd:%{public}d vlen:%{public}u flags:%{public}u,", fd, vlen, flags); #if LONG_MAX > INT_MAX struct mmsghdr *mh = msgvec; unsigned int i; diff --git a/src/network/recvmsg.c b/src/network/recvmsg.c index 979c6eabe..1e540f7dc 100644 --- a/src/network/recvmsg.c +++ b/src/network/recvmsg.c @@ -1,5 +1,6 @@ #include #include +#include "lookup.h" #include #include #include @@ -52,6 +53,7 @@ void __convert_scm_timestamps(struct msghdr *msg, socklen_t csize) ssize_t recvmsg(int fd, struct msghdr *msg, int flags) { + NETWORK_LOGD("recvmsg fd:%{public}d flags:%{public}d", fd, flags); ssize_t r; #ifdef __LITEOS_A__ if (!msg) { diff --git a/src/network/res_mkquery.c b/src/network/res_mkquery.c index 33f50cb93..567234580 100644 --- a/src/network/res_mkquery.c +++ b/src/network/res_mkquery.c @@ -1,11 +1,14 @@ #include #include #include +#include "lookup.h" int __res_mkquery(int op, const char *dname, int class, int type, const unsigned char *data, int datalen, const unsigned char *newrr, unsigned char *buf, int buflen) { + NETWORK_LOGD("__res_mkquery: op:%{public}d dname:%{public}s class:%{public}d type:%{public}d buflen:%{public}d", + op, dname, class, type, buflen); int id, i, j; unsigned char q[280]; struct timespec ts; @@ -14,8 +17,11 @@ int __res_mkquery(int op, const char *dname, int class, int type, if (l && dname[l-1]=='.') l--; n = 17+l+!!l; - if (l>253 || buflen15u || class>255u || type>255u) + if (l>253 || buflen15u || class>255u || type>255u) { + NETWORK_LOGE("__res_mkquery: invalid argument."); return -1; + } + /* Construct query template - ID will be filled later */ memset(q, 0, n); diff --git a/src/network/res_msend.c b/src/network/res_msend.c index c63fca1d3..806215ad1 100644 --- a/src/network/res_msend.c +++ b/src/network/res_msend.c @@ -86,6 +86,7 @@ int res_msend_rc_ext(int netid, int nqueries, const unsigned char *const *querie if (fd < 0 || bind(fd, (void *)&sa, sl) < 0) { if (fd >= 0) close(fd); pthread_setcancelstate(cs, 0); + NETWORK_LOGE("res_msend_rc_ext: create socket failed or bind failed!"); return -1; } @@ -199,6 +200,9 @@ int __res_msend(int nqueries, const unsigned char *const *queries, const int *qlens, unsigned char *const *answers, int *alens, int asize) { struct resolvconf conf; - if (__get_resolv_conf(&conf, 0, 0) < 0) return -1; + if (__get_resolv_conf(&conf, 0, 0) < 0) { + NETWORK_LOGE("__res_msend: __get_resolv_conf failed."); + return -1; + } return __res_msend_rc(nqueries, queries, qlens, answers, alens, asize, &conf); } diff --git a/src/network/res_query.c b/src/network/res_query.c index 506dc2312..874228e9c 100644 --- a/src/network/res_query.c +++ b/src/network/res_query.c @@ -1,25 +1,35 @@ #define _BSD_SOURCE #include #include +#include "lookup.h" int res_query(const char *name, int class, int type, unsigned char *dest, int len) { + NETWORK_LOGD("res_query name:%{public}s class:%{public}d type:%{public}d len:%{public}d", + name, class, type, len); unsigned char q[280]; int ql = __res_mkquery(0, name, class, type, 0, 0, 0, q, sizeof q); - if (ql < 0) return ql; + if (ql < 0) { + NETWORK_LOGE("res_query: __res_mkquery failed, ql:%{public}d.", ql); + return ql; + } int r = __res_send(q, ql, dest, len); if (r<12) { + NETWORK_LOGE("res_query: response message is incomplete or formatted incorrectly."); h_errno = TRY_AGAIN; return -1; } if ((dest[3] & 15) == 3) { + NETWORK_LOGE("res_query: domain name not found."); h_errno = HOST_NOT_FOUND; return -1; } if ((dest[3] & 15) == 0 && !dest[6] && !dest[7]) { + NETWORK_LOGE("res_query: domain name found but no data."); h_errno = NO_DATA; return -1; } + NETWORK_LOGD("res_query r:%{public}d", r); return r; } diff --git a/src/network/res_querydomain.c b/src/network/res_querydomain.c index 727e6f6ba..b6cbfac92 100644 --- a/src/network/res_querydomain.c +++ b/src/network/res_querydomain.c @@ -1,12 +1,17 @@ #include #include +#include "lookup.h" int res_querydomain(const char *name, const char *domain, int class, int type, unsigned char *dest, int len) { + NETWORK_LOGD("res_querydomain name:%{public}s domain:%{public}s", name, domain); char tmp[255]; size_t nl = strnlen(name, 255); size_t dl = strnlen(domain, 255); - if (nl+dl+1 > 254) return -1; + if (nl+dl+1 > 254) { + NETWORK_LOGE("res_querydomain: the length of name and domain exceeds limit."); + return -1; + } memcpy(tmp, name, nl); tmp[nl] = '.'; memcpy(tmp+nl+1, domain, dl+1); diff --git a/src/network/res_send.c b/src/network/res_send.c index ee4abf1f1..d9925e274 100644 --- a/src/network/res_send.c +++ b/src/network/res_send.c @@ -1,8 +1,12 @@ #include +#include "lookup.h" int __res_send(const unsigned char *msg, int msglen, unsigned char *answer, int anslen) { + NETWORK_LOGD("__res_send msg:%{public}s msglen:%{public}d anslen:%{public}d", + msg, msglen, anslen); int r = __res_msend(1, &msg, &msglen, &answer, &anslen, anslen); + NETWORK_LOGD("__res_send r:%{public}d", r); return r<0 || !anslen ? -1 : anslen; } diff --git a/src/network/sendmmsg.c b/src/network/sendmmsg.c index eeae1d0a5..792cefc73 100644 --- a/src/network/sendmmsg.c +++ b/src/network/sendmmsg.c @@ -3,16 +3,21 @@ #include #include #include "syscall.h" +#include "lookup.h" int sendmmsg(int fd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags) { + NETWORK_LOGD("sendmmsg fd:%{public}d flags:%{public}u", fd, flags); #if LONG_MAX > INT_MAX /* Can't use the syscall directly because the kernel has the wrong * idea for the types of msg_iovlen, msg_controllen, and cmsg_len, * and the cmsg blocks cannot be modified in-place. */ int i; if (vlen > IOV_MAX) vlen = IOV_MAX; /* This matches the kernel. */ - if (!vlen) return 0; + if (!vlen) { + NETWORK_LOGE("sendmmsg: vlen is 0."); + return 0; + } for (i=0; i #include #include "syscall.h" +#include "lookup.h" ssize_t sendmsg(int fd, const struct msghdr *msg, int flags) { + NETWORK_LOGD("sendmsg fd:%{public}d flags:%{public}d", fd, flags); #if LONG_MAX > INT_MAX struct msghdr h; struct cmsghdr chbuf[1024/sizeof(struct cmsghdr)+1], *c; @@ -16,6 +18,7 @@ ssize_t sendmsg(int fd, const struct msghdr *msg, int flags) if (h.msg_controllen) { if (h.msg_controllen > 1024) { errno = ENOMEM; + NETWORK_LOGE("sendmsg: message control length is too long."); return -1; } memcpy(chbuf, h.msg_control, h.msg_controllen); diff --git a/src/network/setsockopt.c b/src/network/setsockopt.c index 612a1947a..b009e8b67 100644 --- a/src/network/setsockopt.c +++ b/src/network/setsockopt.c @@ -2,12 +2,15 @@ #include #include #include "syscall.h" +#include "lookup.h" #define IS32BIT(x) !((x)+0x80000000ULL>>32) #define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63)) int setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen) { + NETWORK_LOGD("setsockopt fd:%{public}d level:%{public}d optname:%{public}d," + "optlen:%{public}d", fd, level, optname, optlen); const struct timeval *tv; time_t s; suseconds_t us; diff --git a/src/network/sockatmark.c b/src/network/sockatmark.c index c56c7d79a..017cbe7a1 100644 --- a/src/network/sockatmark.c +++ b/src/network/sockatmark.c @@ -1,12 +1,16 @@ #include #include #include +#include "lookup.h" int sockatmark(int s) { + NETWORK_LOGD("sockatmark s:%{public}d", s); int ret; UNSUPPORTED_API_VOID(LITEOS_A); - if (ioctl(s, SIOCATMARK, &ret) < 0) + if (ioctl(s, SIOCATMARK, &ret) < 0) { + NETWORK_LOGE("sockatmark: ioctl failed."); return -1; + } return ret; } diff --git a/src/network/socketpair.c b/src/network/socketpair.c index f34896211..7a0ece817 100644 --- a/src/network/socketpair.c +++ b/src/network/socketpair.c @@ -2,16 +2,21 @@ #include #include #include "syscall.h" +#include "lookup.h" int socketpair(int domain, int type, int protocol, int fd[2]) { + NETWORK_LOGD("socketpair domain:%{public}d type:%{public}d protocol:%{public}d,", domain, type, protocol); int r = socketcall(socketpair, domain, type, protocol, fd, 0, 0); if (r<0 && (errno==EINVAL || errno==EPROTONOSUPPORT) && (type&(SOCK_CLOEXEC|SOCK_NONBLOCK))) { r = socketcall(socketpair, domain, type & ~(SOCK_CLOEXEC|SOCK_NONBLOCK), protocol, fd, 0, 0); - if (r < 0) return r; + if (r < 0) { + NETWORK_LOGE("socketpair: socketpair failed!"); + return r; + } if (type & SOCK_CLOEXEC) { __syscall(SYS_fcntl, fd[0], F_SETFD, FD_CLOEXEC); __syscall(SYS_fcntl, fd[1], F_SETFD, FD_CLOEXEC); @@ -21,5 +26,6 @@ int socketpair(int domain, int type, int protocol, int fd[2]) __syscall(SYS_fcntl, fd[1], F_SETFL, O_NONBLOCK); } } + NETWORK_LOGD("socketpair r:%{public}d fd[0]:%{public}d fd[1]:%{public}d", r, fd[0], fd[1]); return r; } diff --git a/src/sigchain/linux/sigchain.c b/src/sigchain/linux/sigchain.c index da6d61e33..aa7f07a87 100644 --- a/src/sigchain/linux/sigchain.c +++ b/src/sigchain/linux/sigchain.c @@ -170,11 +170,11 @@ static void signal_chain_handler(int signo, siginfo_t* siginfo, void* ucontext_r if (!noreturn) { set_handling_signal(true); } - SIGCHAIN_PRINT_ERROR("%{public}s call %{public}d rd sigchain action for signal: %{public}d", __func__, i, signo); + SIGCHAIN_PRINT_DEBUG("%{public}s call %{public}d rd sigchain action for signal: %{public}d", __func__, i, signo); if (sig_chains[signo - 1].sca_special_actions[i].sca_sigaction(signo, siginfo, ucontext_raw)) { set_handling_signal(previous_value); - SIGCHAIN_PRINT_ERROR("%{public}s call %{public}d rd sigchain action for signal: %{public}d directly return", __func__, i, signo); + SIGCHAIN_PRINT_DEBUG("%{public}s call %{public}d rd sigchain action for signal: %{public}d directly return", __func__, i, signo); return; } @@ -196,22 +196,22 @@ static void signal_chain_handler(int signo, siginfo_t* siginfo, void* ucontext_r sigchain_sigmask(SIG_SETMASK, &mask, NULL); if ((sa_flags & SA_SIGINFO)) { - SIGCHAIN_PRINT_ERROR("%{public}s call usr sigaction for signal: %{public}d", __func__, signo); + SIGCHAIN_PRINT_DEBUG("%{public}s call usr sigaction for signal: %{public}d", __func__, signo); sig_chains[signo - 1].sig_action.sa_sigaction(signo, siginfo, ucontext_raw); } else { if (sig_chains[signo - 1].sig_action.sa_handler == SIG_IGN) { - SIGCHAIN_PRINT_ERROR("%{public}s SIG_IGN handler for signal: %{public}d", __func__, signo); + SIGCHAIN_PRINT_DEBUG("%{public}s SIG_IGN handler for signal: %{public}d", __func__, signo); return; } else if (sig_chains[signo - 1].sig_action.sa_handler == SIG_DFL) { - SIGCHAIN_PRINT_ERROR("%{public}s SIG_DFL handler for signal: %{public}d", __func__, signo); + SIGCHAIN_PRINT_DEBUG("%{public}s SIG_DFL handler for signal: %{public}d", __func__, signo); remove_all_special_handler(signo); if (__syscall(SYS_rt_tgsigqueueinfo, __syscall(SYS_getpid), __syscall(SYS_gettid), signo, siginfo) != 0) { SIGCHAIN_PRINT_ERROR("Failed to rethrow sig(%{public}d), errno(%{public}d).", signo, errno); } else { - SIGCHAIN_PRINT_ERROR("pid(%{public}d) rethrow sig(%{public}d) success.", __syscall(SYS_getpid), signo); + SIGCHAIN_PRINT_DEBUG("pid(%{public}d) rethrow sig(%{public}d) success.", __syscall(SYS_getpid), signo); } } else { - SIGCHAIN_PRINT_ERROR("%{public}s call usr sa_handler: %{public}p for signal: %{public}d", __func__, signo); + SIGCHAIN_PRINT_DEBUG("%{public}s call usr sa_handler: %{public}p for signal: %{public}d", __func__, signo); sig_chains[signo - 1].sig_action.sa_handler(signo); } } diff --git a/src/time/__map_file.c b/src/time/__map_file.c index 2b0a854cf..5ae06a03e 100644 --- a/src/time/__map_file.c +++ b/src/time/__map_file.c @@ -3,13 +3,18 @@ #include #include "syscall.h" #include "kstat.h" +#include "time_impl.h" const char unsigned *__map_file(const char *pathname, size_t *size) { + TIME_LOGD("__map_file pathname:%{public}s", pathname); struct kstat st; const unsigned char *map = MAP_FAILED; int fd = sys_open(pathname, O_RDONLY|O_CLOEXEC|O_NONBLOCK); - if (fd < 0) return 0; + if (fd < 0) { + TIME_LOGE("__map_file: sys_open is failed!"); + return 0; + } if (!syscall(SYS_fstat, fd, &st)) { map = __mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); *size = st.st_size; diff --git a/src/time/__month_to_secs.c b/src/time/__month_to_secs.c index 43248fb3c..7e5d67d0f 100644 --- a/src/time/__month_to_secs.c +++ b/src/time/__month_to_secs.c @@ -1,10 +1,14 @@ +#include "time_impl.h" + int __month_to_secs(int month, int is_leap) { + TIME_LOGD("__month_to_secs month:%{public}d is_leap:%{public}d", month, is_leap); static const int secs_through_month[] = { 0, 31*86400, 59*86400, 90*86400, 120*86400, 151*86400, 181*86400, 212*86400, 243*86400, 273*86400, 304*86400, 334*86400 }; int t = secs_through_month[month]; if (is_leap && month >= 2) t+=86400; + TIME_LOGD("__month_to_secs t:%{public}d", t); return t; } diff --git a/src/time/__secs_to_tm.c b/src/time/__secs_to_tm.c index 093d9021a..7a5712064 100644 --- a/src/time/__secs_to_tm.c +++ b/src/time/__secs_to_tm.c @@ -18,8 +18,10 @@ int __secs_to_tm(long long t, struct tm *tm) static const char days_in_month[] = {31,30,31,30,31,31,30,31,30,31,31,29}; /* Reject time_t values whose year would overflow int */ - if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL) + if (t < INT_MIN * 31622400LL || t > INT_MAX * 31622400LL) { + TIME_LOGE("__secs_to_tm: time is invalid."); return -1; + } secs = t - LEAPOCH; days = secs / 86400; @@ -65,8 +67,10 @@ int __secs_to_tm(long long t, struct tm *tm) years++; } - if (years+100 > INT_MAX || years+100 < INT_MIN) + if (years+100 > INT_MAX || years+100 < INT_MIN) { + TIME_LOGE("__secs_to_tm: years is invalid."); return -1; + } tm->tm_year = years + 100; tm->tm_mon = months + 2; diff --git a/src/time/__tm_to_secs.c b/src/time/__tm_to_secs.c index c29fa985a..b2d4a652c 100644 --- a/src/time/__tm_to_secs.c +++ b/src/time/__tm_to_secs.c @@ -20,5 +20,6 @@ long long __tm_to_secs(const struct tm *tm) t += 3600LL * tm->tm_hour; t += 60LL * tm->tm_min; t += tm->tm_sec; + TIME_LOGD("__tm_to_secs t:%{public}lld", t); return t; } diff --git a/src/time/asctime_r.c b/src/time/asctime_r.c index 26809ca26..26d810680 100644 --- a/src/time/asctime_r.c +++ b/src/time/asctime_r.c @@ -3,9 +3,18 @@ #include #include "locale_impl.h" #include "atomic.h" +#include "time_impl.h" char *__asctime_r(const struct tm *restrict tm, char *restrict buf) { + TIME_LOGD("__asctime_r tm_wday:%{public}d", + "tm_mon:%{public}d" + "tm_mday:%{public}d" + "tm_hour:%{public}d" + "tm_min:%{public}d" + "tm_sec:%{public}d" + "tm_year:%{public}d", + tm->tm_wday, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,tm->tm_year); if (snprintf(buf, 26, "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n", __nl_langinfo_l(ABDAY_1+tm->tm_wday, C_LOCALE), __nl_langinfo_l(ABMON_1+tm->tm_mon, C_LOCALE), @@ -20,8 +29,10 @@ char *__asctime_r(const struct tm *restrict tm, char *restrict buf) * application developers that they may not be so lucky * on other implementations (e.g. stack smashing..). */ + TIME_LOGE("__asctime_r: the fields in tm are too large, a_crash!"); a_crash(); } + TIME_LOGD("__asctime_r buf:%{public}s", buf); return buf; } diff --git a/src/time/clock.c b/src/time/clock.c index 1748b55ab..6d0a1a06e 100644 --- a/src/time/clock.c +++ b/src/time/clock.c @@ -1,5 +1,6 @@ #include #include +#include "time_impl.h" #include clock_t clock() @@ -8,12 +9,14 @@ clock_t clock() UNSUPPORTED_API_VOID(LITEOS_A); - if (__clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)) + if (__clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts)) { + TIME_LOGE("clock: __clock_gettime call failed!"); return -1; - + } if (ts.tv_sec > LONG_MAX/1000000 - || ts.tv_nsec/1000 > LONG_MAX-1000000*ts.tv_sec) + || ts.tv_nsec/1000 > LONG_MAX-1000000*ts.tv_sec) { + TIME_LOGE("clock: ts is invalid!"); return -1; - + } return ts.tv_sec*1000000 + ts.tv_nsec/1000; } diff --git a/src/time/clock_getcpuclockid.c b/src/time/clock_getcpuclockid.c index 8a0e2d4c3..4267b69e4 100644 --- a/src/time/clock_getcpuclockid.c +++ b/src/time/clock_getcpuclockid.c @@ -2,13 +2,18 @@ #include #include #include "syscall.h" +#include "time_impl.h" int clock_getcpuclockid(pid_t pid, clockid_t *clk) { + TIME_LOGD("clock_getcpuclockid pid:%{public}d", pid); struct timespec ts; clockid_t id = (-pid-1)*8U + 2; int ret = __syscall(SYS_clock_getres, id, &ts); - if (ret) return -ret; + if (ret) { + TIME_LOGE("clock_getcpuclockid: SYS_clock_getres call failed! ret:%{public}d", ret); + return -ret; + } *clk = id; return 0; } diff --git a/src/time/ctime.c b/src/time/ctime.c index 360293151..6a414a893 100644 --- a/src/time/ctime.c +++ b/src/time/ctime.c @@ -1,8 +1,12 @@ #include +#include "time_impl.h" char *ctime(const time_t *t) { struct tm *tm = localtime(t); - if (!tm) return 0; + if (!tm) { + TIME_LOGE("ctime: localtime failed!"); + return 0; + } return asctime(tm); } diff --git a/src/time/getdate.c b/src/time/getdate.c index 420cd8e41..3d2821b01 100644 --- a/src/time/getdate.c +++ b/src/time/getdate.c @@ -3,7 +3,7 @@ #include #include #include - +#include "time_impl.h" int getdate_err; struct tm *getdate(const char *s) @@ -18,12 +18,14 @@ struct tm *getdate(const char *s) pthread_setcancelstate(PTHREAD_CANCEL_DEFERRED, &cs); if (!datemsk) { + TIME_LOGE("getdate: datemsk is null."); getdate_err = 1; goto out; } f = fopen(datemsk, "rbe"); if (!f) { + TIME_LOGE("getdate: fopen failed."); if (errno == ENOMEM) getdate_err = 6; else getdate_err = 2; goto out; diff --git a/src/time/gettimeofday.c b/src/time/gettimeofday.c index c307080fb..ba411e1e6 100644 --- a/src/time/gettimeofday.c +++ b/src/time/gettimeofday.c @@ -1,6 +1,7 @@ #include #include #include "syscall.h" +#include "time_impl.h" #ifndef __LITEOS__ #include @@ -41,7 +42,10 @@ int gettimeofday(struct timeval *restrict tv, void *restrict tz) #endif struct timespec ts; - if (!tv) return 0; + if (!tv) { + TIME_LOGE("gettimeofday: tv is null."); + return 0; + } clock_gettime(CLOCK_REALTIME, &ts); tv->tv_sec = ts.tv_sec; tv->tv_usec = (int)ts.tv_nsec / 1000; diff --git a/src/time/gmtime_r.c b/src/time/gmtime_r.c index ededd7756..200c9119c 100644 --- a/src/time/gmtime_r.c +++ b/src/time/gmtime_r.c @@ -5,6 +5,7 @@ struct tm *__gmtime_r(const time_t *restrict t, struct tm *restrict tm) { if (__secs_to_tm(*t, tm) < 0) { errno = EOVERFLOW; + TIME_LOGE("__gmtime_r: __secs_to_tm is failed."); return 0; } tm->tm_isdst = 0; diff --git a/src/time/localtime_r.c b/src/time/localtime_r.c index 1a15b314d..bb85accad 100644 --- a/src/time/localtime_r.c +++ b/src/time/localtime_r.c @@ -7,11 +7,13 @@ struct tm *__localtime_r(const time_t *restrict t, struct tm *restrict tm) /* Reject time_t values whose year would overflow int because * __secs_to_zone cannot safely handle them. */ if (*t < INT_MIN * 31622400LL || *t > INT_MAX * 31622400LL) { + TIME_LOGE("__localtime_r: time is invalid."); errno = EOVERFLOW; return 0; } __secs_to_zone(*t, 0, &tm->tm_isdst, &tm->__tm_gmtoff, 0, &tm->__tm_zone); if (__secs_to_tm((long long)*t + tm->__tm_gmtoff, tm) < 0) { + TIME_LOGE("__localtime_r: __secs_to_tm failed."); errno = EOVERFLOW; return 0; } diff --git a/src/time/strftime.c b/src/time/strftime.c index 182e79972..302aa693f 100644 --- a/src/time/strftime.c +++ b/src/time/strftime.c @@ -233,6 +233,7 @@ recu_strftime: size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const struct tm *restrict tm, locale_t loc) { + TIME_LOGD("__strftime_l n:%{public}zu, f:%{public}s", n, f); size_t l, k; char buf[100]; char *p; diff --git a/src/time/strptime.c b/src/time/strptime.c index 02ca887cd..5667e4e66 100644 --- a/src/time/strptime.c +++ b/src/time/strptime.c @@ -5,9 +5,11 @@ #include #include #include +#include "time_impl.h" int __getzonename(const char *restrict s, struct tm *restrict tm) { + TIME_LOGD(" __getzonename s:%{public}s", s); const char *p = s; struct tm old; memcpy(&old, tm, sizeof(struct tm)); @@ -35,11 +37,13 @@ int __getzonename(const char *restrict s, struct tm *restrict tm) if (tmp) { tm->tm_isdst = tmp->tm_isdst; } + TIME_LOGD("__getzonename len:%{public}d", len); return len; } int __getgmtoff(const char *restrict s, struct tm *restrict tm) { + TIME_LOGD("__getgmtoff s:%{public}s", s); const char *p = s; int sign = 1; int i; @@ -366,5 +370,6 @@ char *strptime(const char *restrict s, const char *restrict f, struct tm *restri if (want_century & 2) tm->tm_year += century * 100 - 1900; else if (tm->tm_year <= 68) tm->tm_year += 100; } + TIME_LOGD("strptime s:%{public}s", (char *)s); return (char *)s; } diff --git a/src/time/time_impl.h b/src/time/time_impl.h index 44f84228d..6ab4e6680 100644 --- a/src/time/time_impl.h +++ b/src/time/time_impl.h @@ -2,6 +2,9 @@ #define __TIME_IMPL_H__ #include +#ifndef __LITEOS__ +#include +#endif hidden int __days_in_month(int, int); hidden int __month_to_secs(int, int); @@ -14,4 +17,26 @@ hidden const char *__strftime_fmt_1(char (*)[100], size_t *, int, const struct t extern hidden const char __utc[]; extern hidden const char __gmt[]; +#define TIME_LOG_TAG "MUSL-TIME" + +#if ((defined(OHOS_ENABLE_PARAMETER) || defined(ENABLE_MUSL_LOG))) && (!defined(__LITEOS__)) +#define TIME_LOGE(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_ERROR, \ + MUSL_LOG_DOMAIN, TIME_LOG_TAG, __VA_ARGS__)) +#define TIME_LOGI(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_INFO, \ + MUSL_LOG_DOMAIN, TIME_LOG_TAG, __VA_ARGS__)) +#define TIME_LOGD(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_DEBUG, \ + MUSL_LOG_DOMAIN, TIME_LOG_TAG, __VA_ARGS__)) +#define TIME_LOGW(...) ((void)HiLogAdapterPrint(LOG_CORE, LOG_WARN, \ + MUSL_LOG_DOMAIN, TIME_LOG_TAG, __VA_ARGS__)) +#else +#define TIME_LOGE(...) +#define TIME_LOGI(...) +#define TIME_LOGD(...) +#define TIME_LOGW(...) +#endif + #endif + + + + diff --git a/src/time/timegm.c b/src/time/timegm.c index 4e5907d7f..792fdcc5e 100644 --- a/src/time/timegm.c +++ b/src/time/timegm.c @@ -7,6 +7,7 @@ time_t timegm(struct tm *tm) struct tm new; long long t = __tm_to_secs(tm); if (__secs_to_tm(t, &new) < 0) { + TIME_LOGE("timegm: __secs_to_tm failed."); errno = EOVERFLOW; return -1; } @@ -14,5 +15,6 @@ time_t timegm(struct tm *tm) tm->tm_isdst = 0; tm->__tm_gmtoff = 0; tm->__tm_zone = __utc; + TIME_LOGD("timegm t:%{public}lld", t); return t; } diff --git a/src/time/timer_create.c b/src/time/timer_create.c index 09d3d1341..793d314d6 100644 --- a/src/time/timer_create.c +++ b/src/time/timer_create.c @@ -3,6 +3,7 @@ #include #include "pthread_impl.h" #include "atomic.h" +#include "time_impl.h" struct ksigevent { union sigval sigev_value; @@ -61,6 +62,7 @@ static void *start(void *arg) int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict res) { + TIME_LOGD("timer_create clk:%{public}ld, sigev_notify:%{public}d", clk, evp ? evp->sigev_notify : SIGEV_SIGNAL); volatile static int init = 0; pthread_t td; pthread_attr_t attr; @@ -84,8 +86,10 @@ int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict ksev.sigev_tid = 0; ksevp = &ksev; } - if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0) + if (syscall(SYS_timer_create, clk, ksevp, &timerid) < 0) { + TIME_LOGE("timer_create: SYS_timer_create failed."); return -1; + } *res = (void *)(intptr_t)timerid; break; case SIGEV_THREAD: @@ -108,6 +112,7 @@ int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict __restore_sigs(&set); if (r) { errno = r; + TIME_LOGE("timer_create: pthread_create failed."); return -1; } @@ -119,11 +124,15 @@ int timer_create(clockid_t clk, struct sigevent *restrict evp, timer_t *restrict timerid = -1; td->timer_id = timerid; pthread_barrier_wait(&args.b); - if (timerid < 0) return -1; + if (timerid < 0) { + TIME_LOGE("timer_create: timerid is invalid."); + return -1; + } *res = (void *)(INTPTR_MIN | (uintptr_t)td>>1); break; default: errno = EINVAL; + TIME_LOGE("timer_create: sigevent is invalid."); return -1; } diff --git a/src/time/timer_delete.c b/src/time/timer_delete.c index b0bfac096..1c108445b 100644 --- a/src/time/timer_delete.c +++ b/src/time/timer_delete.c @@ -1,6 +1,7 @@ #include #include #include "pthread_impl.h" +#include "time_impl.h" int timer_delete(timer_t t) { @@ -8,6 +9,7 @@ int timer_delete(timer_t t) pthread_t td = (void *)((uintptr_t)t << 1); a_store(&td->timer_id, td->timer_id | INT_MIN); __syscall(SYS_tkill, td->tid, SIGTIMER); + TIME_LOGE("timer_delete: timer is invalid"); return 0; } return __syscall(SYS_timer_delete, t); diff --git a/src/time/timer_getoverrun.c b/src/time/timer_getoverrun.c index e7f891e40..7ce4e9fef 100644 --- a/src/time/timer_getoverrun.c +++ b/src/time/timer_getoverrun.c @@ -1,10 +1,12 @@ #include #include #include "pthread_impl.h" +#include "time_impl.h" int timer_getoverrun(timer_t t) { if ((intptr_t)t < 0) { + TIME_LOGW("timer_getoverrun: timer is invalid"); pthread_t td = (void *)((uintptr_t)t << 1); t = (void *)(uintptr_t)(td->timer_id & INT_MAX); } diff --git a/src/time/timer_settime.c b/src/time/timer_settime.c index 373f00ced..90794feaa 100644 --- a/src/time/timer_settime.c +++ b/src/time/timer_settime.c @@ -1,11 +1,13 @@ #include #include #include "pthread_impl.h" +#include "time_impl.h" #define IS32BIT(x) !((x)+0x80000000ULL>>32) int timer_settime(timer_t t, int flags, const struct itimerspec *restrict val, struct itimerspec *restrict old) { + TIME_LOGD("timer_settime t:%{public}ld, flags:%{public}d, val:%{public}p", t, flags, val); if ((intptr_t)t < 0) { pthread_t td = (void *)((uintptr_t)t << 1); t = (void *)(uintptr_t)(td->timer_id & INT_MAX); @@ -31,6 +33,7 @@ int timer_settime(timer_t t, int flags, const struct itimerspec *restrict val, s old->it_value.tv_sec = old32[2]; old->it_value.tv_nsec = old32[3]; } + TIME_LOGD("timer_settime r:%{public}d", r); return __syscall_ret(r); #endif return syscall(SYS_timer_settime, t, flags, val, old); diff --git a/src/time/timespec_get.c b/src/time/timespec_get.c index 40ea9c1c3..ebdd19bec 100644 --- a/src/time/timespec_get.c +++ b/src/time/timespec_get.c @@ -1,10 +1,14 @@ #include - +#include "time_impl.h" /* There is no other implemented value than TIME_UTC; all other values * are considered erroneous. */ int timespec_get(struct timespec * ts, int base) { - if (base != TIME_UTC) return 0; + if (base != TIME_UTC) { + TIME_LOGE("timespec_get: base is not TIME_UTC"); + return 0; + } int ret = __clock_gettime(CLOCK_REALTIME, ts); + TIME_LOGD("timespec_get ret:%{public}d", ret); return ret < 0 ? 0 : base; } diff --git a/src/time/utime.c b/src/time/utime.c index c251ab69d..ff90dea86 100644 --- a/src/time/utime.c +++ b/src/time/utime.c @@ -3,10 +3,13 @@ #include #include #include +#include "time_impl.h" int utime(const char *path, const struct utimbuf *times) { UNSUPPORTED_API_VOID(LITEOS_A); + TIME_LOGD("utime path:%{public}s actime:%{public}ld modtime:%{public}ld", + path, times ? times->actime: 0, times ? times->modtime: 0); return utimensat(AT_FDCWD, path, times ? ((struct timespec [2]){ { .tv_sec = times->actime }, { .tv_sec = times->modtime }}) : 0, 0); diff --git a/src/time/wcsftime.c b/src/time/wcsftime.c index 8e1437b33..5588b5c59 100644 --- a/src/time/wcsftime.c +++ b/src/time/wcsftime.c @@ -6,6 +6,7 @@ size_t __wcsftime_l(wchar_t *restrict s, size_t n, const wchar_t *restrict f, const struct tm *restrict tm, locale_t loc) { + TIME_LOGD("__wcsftime_l n:%{public}zu, f:%{public}ls", n, f); size_t l, k; char buf[100]; wchar_t wbuf[100]; @@ -36,7 +37,10 @@ size_t __wcsftime_l(wchar_t *restrict s, size_t n, const wchar_t *restrict f, co f = p; if (*f == 'E' || *f == 'O') f++; t_mb = __strftime_fmt_1(&buf, &k, *f, tm, loc, pad); - if (!t_mb) break; + if (!t_mb) { + TIME_LOGD("__strftime_l: t_mb is null, break!"); + break; + } k = mbstowcs(wbuf, t_mb, sizeof wbuf / sizeof *wbuf); if (k == (size_t)-1) return 0; t = wbuf; -- Gitee