fixed syscall errno return.

This commit is contained in:
geniusgogo 2023-02-07 18:53:05 +08:00 committed by Man, Jianting (Meco)
parent 7296117203
commit 890c1c9264
1 changed files with 135 additions and 26 deletions

View File

@ -481,8 +481,14 @@ ssize_t sys_read(int fd, void *buf, size_t nbyte)
lwp_put_to_user(buf, kmem, ret); lwp_put_to_user(buf, kmem, ret);
} }
if (ret < 0)
{
ret = GET_ERRNO();
}
kmem_put(kmem); kmem_put(kmem);
return (ret < 0 ? GET_ERRNO() : ret);
return ret;
#else #else
if (!lwp_user_accessable((void *)buf, nbyte)) 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); lwp_get_from_user(kmem, (void *)buf, nbyte);
ret = write(fd, kmem, nbyte); ret = write(fd, kmem, nbyte);
if (ret < 0)
{
ret = GET_ERRNO();
}
kmem_put(kmem); kmem_put(kmem);
return (ret < 0 ? GET_ERRNO() : ret);
return ret;
#else #else
if (!lwp_user_accessable((void *)buf, nbyte)) 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); lwp_get_from_user(kname, (void *)name, len + 1);
ret = open(kname, flag, 0); ret = open(kname, flag, 0);
if (ret < 0)
{
ret = GET_ERRNO();
}
kmem_put(kname); kmem_put(kname);
return (ret < 0 ? GET_ERRNO() : ret);
return ret;
#else #else
if (!lwp_user_accessable((void *)name, 1)) if (!lwp_user_accessable((void *)name, 1))
{ {
@ -614,7 +630,8 @@ int sys_fstat(int file, struct stat *buf)
{ {
ret = GET_ERRNO(); ret = GET_ERRNO();
} }
return (ret < 0 ? GET_ERRNO() : ret);
return ret;
} }
#else #else
if (!lwp_user_accessable((void *)buf, sizeof(struct stat))) 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); lwp_put_to_user(exceptfds, kexceptfds, sizeof *kexceptfds);
} }
quit: quit:
if (ret < 0)
{
ret = GET_ERRNO();
}
if (kreadfds) if (kreadfds)
{ {
kmem_put(kreadfds); kmem_put(kreadfds);
@ -857,7 +879,7 @@ quit:
{ {
kmem_put(kexceptfds); kmem_put(kexceptfds);
} }
return (ret < 0 ? GET_ERRNO() : ret); return ret;
#else #else
int ret; int ret;
@ -906,9 +928,13 @@ int sys_unlink(const char *pathname)
lwp_get_from_user(kname, (void *)pathname, len + 1); lwp_get_from_user(kname, (void *)pathname, len + 1);
ret = unlink(kname); ret = unlink(kname);
if (ret < 0)
{
ret = GET_ERRNO();
}
kmem_put(kname); kmem_put(kname);
return (ret < 0 ? GET_ERRNO() : ret); return ret;
#else #else
int ret = 0; int ret = 0;
ret = unlink(pathname); ret = unlink(pathname);
@ -1679,6 +1705,7 @@ int _sys_fork(void)
{ {
rt_base_t level; rt_base_t level;
int tid = 0; int tid = 0;
rt_err_t falival = 0;
struct rt_lwp *lwp = RT_NULL; struct rt_lwp *lwp = RT_NULL;
struct rt_lwp *self_lwp = RT_NULL; struct rt_lwp *self_lwp = RT_NULL;
rt_thread_t thread = RT_NULL; rt_thread_t thread = RT_NULL;
@ -1737,6 +1764,7 @@ int _sys_fork(void)
self_thread->init_tick); self_thread->init_tick);
if (!thread) if (!thread)
{ {
SET_ERRNO(ENOMEM);
goto fail; goto fail;
} }
@ -1789,6 +1817,7 @@ int _sys_fork(void)
if (ret < 0) if (ret < 0)
{ {
LOG_E("malloc fail!\n"); LOG_E("malloc fail!\n");
SET_ERRNO(ENOMEM);
goto fail; goto fail;
} }
@ -1798,6 +1827,8 @@ int _sys_fork(void)
rt_thread_startup(thread); rt_thread_startup(thread);
return lwp_to_pid(lwp); return lwp_to_pid(lwp);
fail: fail:
falival = GET_ERRNO();
if (tid != 0) if (tid != 0)
{ {
lwp_tid_put(tid); lwp_tid_put(tid);
@ -1806,7 +1837,7 @@ fail:
{ {
lwp_ref_dec(lwp); lwp_ref_dec(lwp);
} }
return GET_ERRNO(); return falival;
} }
size_t lwp_user_strlen(const char *s, int *err) 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); lwp_put_to_user(mem, kmem, len);
} }
if (ret < 0)
{
ret = GET_ERRNO();
}
kmem_put(kmem); kmem_put(kmem);
return (ret < 0 ? GET_ERRNO() : ret);
return ret;
#else #else
int ret = -1; int ret = -1;
if (from) 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); ret = sendto(socket, kmem, size, flgs, NULL, tolen);
} }
if (ret < 0)
{
ret = GET_ERRNO();
}
kmem_put(kmem); kmem_put(kmem);
return (ret < 0 ? GET_ERRNO() : ret);
return ret;
#else #else
int ret; int ret;
if (to) if (to)
@ -3476,6 +3519,11 @@ int sys_getaddrinfo(const char *nodename,
} }
exit: exit:
if (ret < 0)
{
ret = GET_ERRNO();
}
if (k_nodename) if (k_nodename)
{ {
rt_free(k_nodename); rt_free(k_nodename);
@ -3489,7 +3537,7 @@ exit:
rt_free(k_hints); rt_free(k_hints);
} }
return (ret < 0 ? GET_ERRNO() : ret); return ret;
} }
#define HOSTENT_BUFSZ 512 #define HOSTENT_BUFSZ 512
@ -3587,6 +3635,11 @@ int sys_gethostbyname2_r(const char *name, int af, struct hostent *ret,
ret_val = 0; ret_val = 0;
__exit: __exit:
if (ret_val < 0)
{
ret_val = GET_ERRNO();
}
/* release buffer */ /* release buffer */
if (sal_buf) if (sal_buf)
{ {
@ -3597,7 +3650,7 @@ __exit:
free(k_name); free(k_name);
} }
return (ret_val < 0 ? GET_ERRNO() : ret_val); return ret_val;
} }
#endif #endif
@ -3717,8 +3770,15 @@ int sys_getdents(int fd, struct libc_dirent *dirp, size_t nbytes)
} }
ret = cnt * sizeof(struct libc_dirent); ret = cnt * sizeof(struct libc_dirent);
} }
if (ret < 0)
{
ret = GET_ERRNO();
}
rt_free(rtt_dirp); rt_free(rtt_dirp);
return (ret < 0 ? GET_ERRNO() : ret);
return ret;
} }
rt_err_t sys_get_errno(void) 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); kname = (char *)kmem_get(len + 1);
if (!ret && !kname) if (!kname)
{ {
return -ENOMEM; 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 = GET_ERRNO();
ret = access(kname, mode);
kmem_put(kname);
} }
kmem_put(kname);
return ret;
#else #else
ret = access(filename, mode); ret = access(filename, mode);
#endif
return (ret < 0 ? GET_ERRNO() : ret); return (ret < 0 ? GET_ERRNO() : ret);
#endif
} }
int sys_pipe(int fd[2]) 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); lwp_get_from_user(kts, (void *)ts, size);
ret = clock_settime(clk, kts); ret = clock_settime(clk, kts);
if (ret < 0)
{
ret = GET_ERRNO();
}
kmem_put(kts); kmem_put(kts);
return ret;
#else #else
if (!lwp_user_accessable((void *)ts, sizeof(struct timespec))) if (!lwp_user_accessable((void *)ts, sizeof(struct timespec)))
{ {
return -EFAULT; return -EFAULT;
} }
ret = clock_settime(clk, ts); ret = clock_settime(clk, ts);
#endif
return (ret < 0 ? GET_ERRNO() : ret); return (ret < 0 ? GET_ERRNO() : ret);
#endif
} }
int sys_clock_gettime(clockid_t clk, struct timespec *ts) 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) if (ret != -1)
lwp_put_to_user(ts, kts, size); lwp_put_to_user(ts, kts, size);
if (ret < 0)
{
ret = GET_ERRNO();
}
kmem_put(kts); kmem_put(kts);
return ret;
#else #else
if (!lwp_user_accessable((void *)ts, sizeof(struct timespec))) if (!lwp_user_accessable((void *)ts, sizeof(struct timespec)))
{ {
return -EFAULT; return -EFAULT;
} }
ret = clock_gettime(clk, ts); ret = clock_gettime(clk, ts);
#endif
return (ret < 0 ? GET_ERRNO() : ret); return (ret < 0 ? GET_ERRNO() : ret);
#endif
} }
int sys_clock_nanosleep(clockid_t clk, int flags, const struct timespec *rqtp, struct timespec *rmtp) 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 sys_mq_open(const char *name, int flags, mode_t mode, struct mq_attr *attr)
{ {
mqd_t mqdes; mqd_t mqdes;
rt_err_t ret = 0;
#ifdef ARCH_MM_MMU #ifdef ARCH_MM_MMU
char *kname = RT_NULL; char *kname = RT_NULL;
int a_err = 0; 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(&attr_k, (void *)attr, sizeof(struct mq_attr));
lwp_get_from_user(kname, (void *)name, len + 1); lwp_get_from_user(kname, (void *)name, len + 1);
mqdes = mq_open(kname, flags, mode, &attr_k); 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)); lwp_put_to_user(attr, &attr_k, sizeof(struct mq_attr));
kmem_put(kname); kmem_put(kname);
#else #else
mqdes = mq_open(name, flags, mode, attr); mqdes = mq_open(name, flags, mode, attr);
#endif #endif
if (mqdes == RT_NULL) if (mqdes == RT_NULL)
return (mqd_t)GET_ERRNO(); return (mqd_t)ret;
else else
return mqdes; return mqdes;
} }
@ -4257,11 +4338,16 @@ int sys_mq_unlink(const char *name)
lwp_get_from_user(kname, (void *)name, len + 1); lwp_get_from_user(kname, (void *)name, len + 1);
ret = mq_unlink(kname); ret = mq_unlink(kname);
if (err < 0)
{
ret = GET_ERRNO();
}
kmem_put(kname); kmem_put(kname);
return ret;
#else #else
ret = mq_unlink(name); ret = mq_unlink(name);
#endif
return (ret < 0 ? GET_ERRNO() : ret); 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) 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(&at_k, (void *)at, sizeof(struct timespec));
lwp_get_from_user(kmsg, (void *)msg, len + 1); lwp_get_from_user(kmsg, (void *)msg, len + 1);
ret = mq_timedsend(mqd, kmsg, len, prio, &at_k); ret = mq_timedsend(mqd, kmsg, len, prio, &at_k);
if (ret < 0)
{
ret = GET_ERRNO();
}
kmem_put(kmsg); kmem_put(kmsg);
return ret;
#else #else
ret = mq_timedsend(mqd, msg, len, prio, at); ret = mq_timedsend(mqd, msg, len, prio, at);
#endif
return (ret < 0 ? GET_ERRNO() : ret); 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) 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); ret = mq_timedreceive(mqd, kmsg, len, prio, &at_k);
if (ret > 0) if (ret > 0)
lwp_put_to_user(msg, kmsg, len + 1); lwp_put_to_user(msg, kmsg, len + 1);
if (ret < 0)
{
ret = GET_ERRNO();
}
kmem_put(kmsg); kmem_put(kmsg);
return ret;
#else #else
ret = mq_timedreceive(mqd, msg, len, prio, at); ret = mq_timedreceive(mqd, msg, len, prio, at);
#endif
return (ret < 0 ? GET_ERRNO() : ret); return (ret < 0 ? GET_ERRNO() : ret);
#endif
} }
int sys_mq_notify(mqd_t mqd, const struct sigevent *sev) 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); ret = mq_setattr(mqd, knew, kold);
if (ret != -1) if (ret != -1)
lwp_put_to_user(old, kold, size); lwp_put_to_user(old, kold, size);
if (ret < 0)
{
ret = GET_ERRNO();
}
kmem_put(kold); kmem_put(kold);
if (new != RT_NULL) if (new != RT_NULL)
kmem_put(knew); kmem_put(knew);
return ret;
#else #else
ret = mq_setattr(mqd, new, old); ret = mq_setattr(mqd, new, old);
#endif
return (ret < 0 ? GET_ERRNO() : ret); return (ret < 0 ? GET_ERRNO() : ret);
#endif
} }
int sys_mq_close(mqd_t mqd) int sys_mq_close(mqd_t mqd)