Jeff Dike | edea138 | 2008-02-04 22:30:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <stdlib.h> |
Jeff Dike | edea138 | 2008-02-04 22:30:46 -0800 | [diff] [blame] | 7 | #include <unistd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <errno.h> |
Jeff Dike | edea138 | 2008-02-04 22:30:46 -0800 | [diff] [blame] | 9 | #include <fcntl.h> |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 10 | #include <kern_util.h> |
| 11 | #include <os.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | struct grantpt_info { |
| 14 | int fd; |
| 15 | int res; |
| 16 | int err; |
| 17 | }; |
| 18 | |
| 19 | static void grantpt_cb(void *arg) |
| 20 | { |
| 21 | struct grantpt_info *info = arg; |
| 22 | |
| 23 | info->res = grantpt(info->fd); |
| 24 | info->err = errno; |
| 25 | } |
| 26 | |
| 27 | int get_pty(void) |
| 28 | { |
| 29 | struct grantpt_info info; |
Jeff Dike | edea138 | 2008-02-04 22:30:46 -0800 | [diff] [blame] | 30 | int fd, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Jeff Dike | edea138 | 2008-02-04 22:30:46 -0800 | [diff] [blame] | 32 | fd = open("/dev/ptmx", O_RDWR); |
| 33 | if (fd < 0) { |
| 34 | err = -errno; |
| 35 | printk(UM_KERN_ERR "get_pty : Couldn't open /dev/ptmx - " |
| 36 | "err = %d\n", errno); |
| 37 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | info.fd = fd; |
| 41 | initial_thread_cb(grantpt_cb, &info); |
| 42 | |
Jeff Dike | edea138 | 2008-02-04 22:30:46 -0800 | [diff] [blame] | 43 | if (info.res < 0) { |
| 44 | err = -info.err; |
| 45 | printk(UM_KERN_ERR "get_pty : Couldn't grant pty - " |
| 46 | "errno = %d\n", -info.err); |
| 47 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Jeff Dike | edea138 | 2008-02-04 22:30:46 -0800 | [diff] [blame] | 50 | if (unlockpt(fd) < 0) { |
| 51 | err = -errno; |
| 52 | printk(UM_KERN_ERR "get_pty : Couldn't unlock pty - " |
| 53 | "errno = %d\n", errno); |
| 54 | goto out; |
| 55 | } |
| 56 | return fd; |
| 57 | out: |
| 58 | close(fd); |
| 59 | return err; |
| 60 | } |