Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
0004  */
0005 
0006 #include <stdlib.h>
0007 #include <unistd.h>
0008 #include <errno.h>
0009 #include <fcntl.h>
0010 #include <kern_util.h>
0011 #include <os.h>
0012 
0013 struct grantpt_info {
0014     int fd;
0015     int res;
0016     int err;
0017 };
0018 
0019 static void grantpt_cb(void *arg)
0020 {
0021     struct grantpt_info *info = arg;
0022 
0023     info->res = grantpt(info->fd);
0024     info->err = errno;
0025 }
0026 
0027 int get_pty(void)
0028 {
0029     struct grantpt_info info;
0030     int fd, err;
0031 
0032     fd = open("/dev/ptmx", O_RDWR);
0033     if (fd < 0) {
0034         err = -errno;
0035         printk(UM_KERN_ERR "get_pty : Couldn't open /dev/ptmx - "
0036                "err = %d\n", errno);
0037         return err;
0038     }
0039 
0040     info.fd = fd;
0041     initial_thread_cb(grantpt_cb, &info);
0042 
0043     if (info.res < 0) {
0044         err = -info.err;
0045         printk(UM_KERN_ERR "get_pty : Couldn't grant pty - "
0046                "errno = %d\n", -info.err);
0047         goto out;
0048     }
0049 
0050     if (unlockpt(fd) < 0) {
0051         err = -errno;
0052         printk(UM_KERN_ERR "get_pty : Couldn't unlock pty - "
0053                "errno = %d\n", errno);
0054         goto out;
0055     }
0056     return fd;
0057 out:
0058     close(fd);
0059     return err;
0060 }