From 890c1c9264e425f20c2925de4095fb46ca4e74aa Mon Sep 17 00:00:00 2001 From: geniusgogo Date: Tue, 7 Feb 2023 18:53:05 +0800 Subject: [PATCH] fixed syscall errno return. --- components/lwp/lwp_syscall.c | 161 +++++++++++++++++++++++++++++------ 1 file changed, 135 insertions(+), 26 deletions(-) diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index 171445470c..85e641e395 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -481,8 +481,14 @@ ssize_t sys_read(int fd, void *buf, size_t nbyte) lwp_put_to_user(buf, kmem, ret); } + if (ret < 0) + { + ret = GET_ERRNO(); + } + kmem_put(kmem); - return (ret < 0 ? GET_ERRNO() : ret); + + return ret; #else if (!lwp_user_accessable((void *)buf, nbyte)) { @@ -518,9 +524,14 @@ ssize_t sys_write(int fd, const void *buf, size_t nbyte) lwp_get_from_user(kmem, (void *)buf, nbyte); ret = write(fd, kmem, nbyte); + if (ret < 0) + { + ret = GET_ERRNO(); + } kmem_put(kmem); - return (ret < 0 ? GET_ERRNO() : ret); + + return ret; #else if (!lwp_user_accessable((void *)buf, nbyte)) { @@ -565,9 +576,14 @@ int sys_open(const char *name, int flag, ...) lwp_get_from_user(kname, (void *)name, len + 1); ret = open(kname, flag, 0); + if (ret < 0) + { + ret = GET_ERRNO(); + } kmem_put(kname); - return (ret < 0 ? GET_ERRNO() : ret); + + return ret; #else if (!lwp_user_accessable((void *)name, 1)) { @@ -614,7 +630,8 @@ int sys_fstat(int file, struct stat *buf) { ret = GET_ERRNO(); } - return (ret < 0 ? GET_ERRNO() : ret); + + return ret; } #else if (!lwp_user_accessable((void *)buf, sizeof(struct stat))) @@ -845,6 +862,11 @@ int sys_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, s lwp_put_to_user(exceptfds, kexceptfds, sizeof *kexceptfds); } quit: + if (ret < 0) + { + ret = GET_ERRNO(); + } + if (kreadfds) { kmem_put(kreadfds); @@ -857,7 +879,7 @@ quit: { kmem_put(kexceptfds); } - return (ret < 0 ? GET_ERRNO() : ret); + return ret; #else int ret; @@ -906,9 +928,13 @@ int sys_unlink(const char *pathname) lwp_get_from_user(kname, (void *)pathname, len + 1); ret = unlink(kname); + if (ret < 0) + { + ret = GET_ERRNO(); + } kmem_put(kname); - return (ret < 0 ? GET_ERRNO() : ret); + return ret; #else int ret = 0; ret = unlink(pathname); @@ -1679,6 +1705,7 @@ int _sys_fork(void) { rt_base_t level; int tid = 0; + rt_err_t falival = 0; struct rt_lwp *lwp = RT_NULL; struct rt_lwp *self_lwp = RT_NULL; rt_thread_t thread = RT_NULL; @@ -1737,6 +1764,7 @@ int _sys_fork(void) self_thread->init_tick); if (!thread) { + SET_ERRNO(ENOMEM); goto fail; } @@ -1789,6 +1817,7 @@ int _sys_fork(void) if (ret < 0) { LOG_E("malloc fail!\n"); + SET_ERRNO(ENOMEM); goto fail; } @@ -1798,6 +1827,8 @@ int _sys_fork(void) rt_thread_startup(thread); return lwp_to_pid(lwp); fail: + falival = GET_ERRNO(); + if (tid != 0) { lwp_tid_put(tid); @@ -1806,7 +1837,7 @@ fail: { lwp_ref_dec(lwp); } - return GET_ERRNO(); + return falival; } size_t lwp_user_strlen(const char *s, int *err) @@ -2945,8 +2976,14 @@ int sys_recvfrom(int socket, void *mem, size_t len, int flags, lwp_put_to_user(mem, kmem, len); } + if (ret < 0) + { + ret = GET_ERRNO(); + } + kmem_put(kmem); - return (ret < 0 ? GET_ERRNO() : ret); + + return ret; #else int ret = -1; if (from) @@ -3015,8 +3052,14 @@ int sys_sendto(int socket, const void *dataptr, size_t size, int flags, ret = sendto(socket, kmem, size, flgs, NULL, tolen); } + if (ret < 0) + { + ret = GET_ERRNO(); + } + kmem_put(kmem); - return (ret < 0 ? GET_ERRNO() : ret); + + return ret; #else int ret; if (to) @@ -3476,6 +3519,11 @@ int sys_getaddrinfo(const char *nodename, } exit: + if (ret < 0) + { + ret = GET_ERRNO(); + } + if (k_nodename) { rt_free(k_nodename); @@ -3489,7 +3537,7 @@ exit: rt_free(k_hints); } - return (ret < 0 ? GET_ERRNO() : ret); + return ret; } #define HOSTENT_BUFSZ 512 @@ -3587,6 +3635,11 @@ int sys_gethostbyname2_r(const char *name, int af, struct hostent *ret, ret_val = 0; __exit: + if (ret_val < 0) + { + ret_val = GET_ERRNO(); + } + /* release buffer */ if (sal_buf) { @@ -3597,7 +3650,7 @@ __exit: free(k_name); } - return (ret_val < 0 ? GET_ERRNO() : ret_val); + return ret_val; } #endif @@ -3717,8 +3770,15 @@ int sys_getdents(int fd, struct libc_dirent *dirp, size_t nbytes) } ret = cnt * sizeof(struct libc_dirent); } + + if (ret < 0) + { + ret = GET_ERRNO(); + } + rt_free(rtt_dirp); - return (ret < 0 ? GET_ERRNO() : ret); + + return ret; } rt_err_t sys_get_errno(void) @@ -3779,22 +3839,24 @@ int sys_access(const char *filename, int mode) } kname = (char *)kmem_get(len + 1); - if (!ret && !kname) + if (!kname) { return -ENOMEM; } - if (!ret) + lwp_get_from_user(kname, (void *)filename, len + 1); + ret = access(kname, mode); + if (ret < 0) { - lwp_get_from_user(kname, (void *)filename, len + 1); - ret = access(kname, mode); - kmem_put(kname); + ret = GET_ERRNO(); } + + kmem_put(kname); + return ret; #else ret = access(filename, mode); -#endif - return (ret < 0 ? GET_ERRNO() : ret); +#endif } int sys_pipe(int fd[2]) @@ -3828,15 +3890,22 @@ int sys_clock_settime(clockid_t clk, const struct timespec *ts) lwp_get_from_user(kts, (void *)ts, size); ret = clock_settime(clk, kts); + if (ret < 0) + { + ret = GET_ERRNO(); + } + kmem_put(kts); + + return ret; #else if (!lwp_user_accessable((void *)ts, sizeof(struct timespec))) { return -EFAULT; } ret = clock_settime(clk, ts); -#endif return (ret < 0 ? GET_ERRNO() : ret); +#endif } int sys_clock_gettime(clockid_t clk, struct timespec *ts) @@ -3861,15 +3930,22 @@ int sys_clock_gettime(clockid_t clk, struct timespec *ts) if (ret != -1) lwp_put_to_user(ts, kts, size); + if (ret < 0) + { + ret = GET_ERRNO(); + } + kmem_put(kts); + + return ret; #else if (!lwp_user_accessable((void *)ts, sizeof(struct timespec))) { return -EFAULT; } ret = clock_gettime(clk, ts); -#endif return (ret < 0 ? GET_ERRNO() : ret); +#endif } int sys_clock_nanosleep(clockid_t clk, int flags, const struct timespec *rqtp, struct timespec *rmtp) @@ -4207,6 +4283,7 @@ int sys_fsync(int fd) mqd_t sys_mq_open(const char *name, int flags, mode_t mode, struct mq_attr *attr) { mqd_t mqdes; + rt_err_t ret = 0; #ifdef ARCH_MM_MMU char *kname = RT_NULL; int a_err = 0; @@ -4226,13 +4303,17 @@ mqd_t sys_mq_open(const char *name, int flags, mode_t mode, struct mq_attr *attr lwp_get_from_user(&attr_k, (void *)attr, sizeof(struct mq_attr)); lwp_get_from_user(kname, (void *)name, len + 1); mqdes = mq_open(kname, flags, mode, &attr_k); + if (mqdes == RT_NULL) + { + ret = GET_ERRNO(); + } lwp_put_to_user(attr, &attr_k, sizeof(struct mq_attr)); kmem_put(kname); #else mqdes = mq_open(name, flags, mode, attr); #endif if (mqdes == RT_NULL) - return (mqd_t)GET_ERRNO(); + return (mqd_t)ret; else return mqdes; } @@ -4257,11 +4338,16 @@ int sys_mq_unlink(const char *name) lwp_get_from_user(kname, (void *)name, len + 1); ret = mq_unlink(kname); + if (err < 0) + { + ret = GET_ERRNO(); + } kmem_put(kname); + return ret; #else ret = mq_unlink(name); -#endif return (ret < 0 ? GET_ERRNO() : ret); +#endif } int sys_mq_timedsend(mqd_t mqd, const char *msg, size_t len, unsigned prio, const struct timespec *at) @@ -4282,11 +4368,18 @@ int sys_mq_timedsend(mqd_t mqd, const char *msg, size_t len, unsigned prio, cons lwp_get_from_user(&at_k, (void *)at, sizeof(struct timespec)); lwp_get_from_user(kmsg, (void *)msg, len + 1); ret = mq_timedsend(mqd, kmsg, len, prio, &at_k); + if (ret < 0) + { + ret = GET_ERRNO(); + } + kmem_put(kmsg); + + return ret; #else ret = mq_timedsend(mqd, msg, len, prio, at); -#endif return (ret < 0 ? GET_ERRNO() : ret); +#endif } int sys_mq_timedreceive(mqd_t mqd, char *restrict msg, size_t len, unsigned *restrict prio, const struct timespec *restrict at) @@ -4310,11 +4403,19 @@ int sys_mq_timedreceive(mqd_t mqd, char *restrict msg, size_t len, unsigned *res ret = mq_timedreceive(mqd, kmsg, len, prio, &at_k); if (ret > 0) lwp_put_to_user(msg, kmsg, len + 1); + + if (ret < 0) + { + ret = GET_ERRNO(); + } + kmem_put(kmsg); + + return ret; #else ret = mq_timedreceive(mqd, msg, len, prio, at); -#endif return (ret < 0 ? GET_ERRNO() : ret); +#endif } int sys_mq_notify(mqd_t mqd, const struct sigevent *sev) @@ -4358,13 +4459,21 @@ int sys_mq_getsetattr(mqd_t mqd, const struct mq_attr *restrict new, struct mq_a ret = mq_setattr(mqd, knew, kold); if (ret != -1) lwp_put_to_user(old, kold, size); + + if (ret < 0) + { + ret = GET_ERRNO(); + } + kmem_put(kold); if (new != RT_NULL) kmem_put(knew); + + return ret; #else ret = mq_setattr(mqd, new, old); -#endif return (ret < 0 ? GET_ERRNO() : ret); +#endif } int sys_mq_close(mqd_t mqd)