blob: b09ff66a77eeaf7578d84df8839e2a2961813cd3 [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>
10#include "kern_constants.h"
11#include "kern_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "os.h"
13#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15struct grantpt_info {
16 int fd;
17 int res;
18 int err;
19};
20
21static void grantpt_cb(void *arg)
22{
23 struct grantpt_info *info = arg;
24
25 info->res = grantpt(info->fd);
26 info->err = errno;
27}
28
29int get_pty(void)
30{
31 struct grantpt_info info;
Jeff Dikeedea1382008-02-04 22:30:46 -080032 int fd, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jeff Dikeedea1382008-02-04 22:30:46 -080034 fd = open("/dev/ptmx", O_RDWR);
35 if (fd < 0) {
36 err = -errno;
37 printk(UM_KERN_ERR "get_pty : Couldn't open /dev/ptmx - "
38 "err = %d\n", errno);
39 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 }
41
42 info.fd = fd;
43 initial_thread_cb(grantpt_cb, &info);
44
Jeff Dikeedea1382008-02-04 22:30:46 -080045 if (info.res < 0) {
46 err = -info.err;
47 printk(UM_KERN_ERR "get_pty : Couldn't grant pty - "
48 "errno = %d\n", -info.err);
49 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Jeff Dikeedea1382008-02-04 22:30:46 -080052 if (unlockpt(fd) < 0) {
53 err = -errno;
54 printk(UM_KERN_ERR "get_pty : Couldn't unlock pty - "
55 "errno = %d\n", errno);
56 goto out;
57 }
58 return fd;
59out:
60 close(fd);
61 return err;
62}