diff mbox series

[19/32] bsd-user: Implement getpriority(2) and setpriority(2).

Message ID 20230827155746.84781-20-kariem.taha2.7@gmail.com
State New
Headers show
Series bsd-user: Implement freebsd process related system calls. | expand

Commit Message

Karim Taha Aug. 27, 2023, 3:57 p.m. UTC
From: Stacey Son <sson@FreeBSD.org>

Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Karim Taha <kariem.taha2.7@gmail.com>
---
 bsd-user/bsd-proc.h           | 27 +++++++++++++++++++++++++++
 bsd-user/freebsd/os-syscall.c |  8 ++++++++
 2 files changed, 35 insertions(+)

Comments

Richard Henderson Aug. 29, 2023, 8:10 p.m. UTC | #1
On 8/27/23 08:57, Karim Taha wrote:
> +static inline abi_long do_bsd_getpriority(abi_long which, abi_long who)
> +{
> +    abi_long ret;
> +    /*
> +     * Note that negative values are valid for getpriority, so we must
> +     * differentiate based on errno settings.
> +     */
> +    errno = 0;
> +    ret = getpriority(which, who);
> +    if (ret == -1 && errno != 0) {
> +        ret = -host_to_target_errno(errno);
> +        return ret;
> +    }
> +    /* Return value is a biased priority to avoid negative numbers. */
> +    ret = 20 - ret;

This appears to be a linux-ism.

There is no such bias in sys/kern/kern_resource.c, kern_getpriority(), but there is in 
sys/compat/linux/linux_misc.c, linux_getpriority().


r~
diff mbox series

Patch

diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h
index c9b5a4cbb6..c7769e3560 100644
--- a/bsd-user/bsd-proc.h
+++ b/bsd-user/bsd-proc.h
@@ -390,4 +390,31 @@  static inline abi_long do_bsd_ptrace(abi_long arg1, abi_long arg2,
     return -TARGET_ENOSYS;
 }
 
+/* getpriority(2) */
+static inline abi_long do_bsd_getpriority(abi_long which, abi_long who)
+{
+    abi_long ret;
+    /*
+     * Note that negative values are valid for getpriority, so we must
+     * differentiate based on errno settings.
+     */
+    errno = 0;
+    ret = getpriority(which, who);
+    if (ret == -1 && errno != 0) {
+        ret = -host_to_target_errno(errno);
+        return ret;
+    }
+    /* Return value is a biased priority to avoid negative numbers. */
+    ret = 20 - ret;
+
+    return ret;
+}
+
+/* setpriority(2) */
+static inline abi_long do_bsd_setpriority(abi_long which, abi_long who,
+                                          abi_long prio)
+{
+    return get_errno(setpriority(which, who, prio));
+}
+
 #endif /* !BSD_PROC_H_ */
diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c
index 7c5c17e70b..0f25187d63 100644
--- a/bsd-user/freebsd/os-syscall.c
+++ b/bsd-user/freebsd/os-syscall.c
@@ -356,6 +356,14 @@  static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_bsd_ptrace(arg1, arg2, arg3, arg4);
         break;
 
+    case TARGET_FREEBSD_NR_getpriority: /* getpriority(2) */
+        ret = do_bsd_getpriority(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_setpriority: /* setpriority(2) */
+        ret = do_bsd_setpriority(arg1, arg2, arg3);
+        break;
+
 
         /*
          * File system calls.