blob: 721d8afa329b17e5547a59885aec1a054b5e8ab5 [file] [log] [blame]
Jeff Dikeedea1382008-02-04 22:30:46 -08001/*
2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
6#include <stdlib.h>
Jeff Dikeedea1382008-02-04 22:30:46 -08007#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <errno.h>
Jeff Dikeedea1382008-02-04 22:30:46 -08009#include <fcntl.h>
Al Viro37185b32012-10-08 03:27:32 +010010#include <kern_util.h>
11#include <os.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13struct grantpt_info {
14 int fd;
15 int res;
16 int err;
17};
18
19static 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
27int get_pty(void)
28{
29 struct grantpt_info info;
Jeff Dikeedea1382008-02-04 22:30:46 -080030 int fd, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Jeff Dikeedea1382008-02-04 22:30:46 -080032 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 Torvalds1da177e2005-04-16 15:20:36 -070038 }
39
40 info.fd = fd;
41 initial_thread_cb(grantpt_cb, &info);
42
Jeff Dikeedea1382008-02-04 22:30:46 -080043 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 Torvalds1da177e2005-04-16 15:20:36 -070048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Jeff Dikeedea1382008-02-04 22:30:46 -080050 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;
57out:
58 close(fd);
59 return err;
60}