^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <kern_util.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct grantpt_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static void grantpt_cb(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct grantpt_info *info = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) info->res = grantpt(info->fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) info->err = errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int get_pty(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct grantpt_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int fd, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) fd = open("/dev/ptmx", O_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) printk(UM_KERN_ERR "get_pty : Couldn't open /dev/ptmx - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) "err = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) info.fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) initial_thread_cb(grantpt_cb, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (info.res < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) err = -info.err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) printk(UM_KERN_ERR "get_pty : Couldn't grant pty - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) "errno = %d\n", -info.err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (unlockpt(fd) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) err = -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) printk(UM_KERN_ERR "get_pty : Couldn't unlock pty - "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) "errno = %d\n", errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }