1/*
2 * Copyright (c) 2012, 2013, ETH Zurich.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, CAB F.78, Universitaetstrasse 6, CH-8092 Zurich,
8 * Attn: Systems Group.
9 */
10
11#ifndef POSIXCOMPAT_PTY_H
12#define POSIXCOMPAT_PTY_H
13
14#include <barrelfish/waitset.h>
15#include <collections/list.h>
16#include <term/server/server.h>
17
18#include <stdbool.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <stdint.h>
22#include <termios.h>
23
24/**
25 * Prefix used when creating a dev-file for the slave side of the pseudo-
26 * terminal.
27 */
28#define PTY_PTS_PATH_PREFIX "/dev/pts/"
29
30/**
31 * Prefix used when generating a system-wide unique number using octopus.
32 */
33#define PTY_PTS_OCTOPUS_PREFIX "ptypts"
34
35/**
36 * State of the pseudo-terminal.
37 */
38struct _pty {
39    /**
40     * Terminal server state of master side.
41     */
42    struct term_server *ts;
43
44    /**
45     * Interface reference for session interface.
46     */
47    iref_t iref;
48
49    /**
50     * Waitsets used for master side.
51     */
52    struct waitset session_ws;
53    struct waitset in_ws;
54    struct waitset out_ws;
55    struct waitset conf_ws;
56
57    /**
58     * Has at least one client connected?
59     */
60    bool connected;
61
62    /**
63     * Mutex used to lock master binding in case of concurrent reads and writes.
64     */
65    struct thread_mutex mmutex;
66
67    /**
68     * Unique number allocated to this pseudo-terminal master and slave pair.
69     */
70    uint32_t number;
71
72    /**
73     * Path of slave dev-file, e.g. /dev/pts/0.
74     */
75    char *ptsname;
76
77    /**
78     * Data received but not yet consumed by a ptm_read().
79     */
80    char *mreadbuf_start;
81    char *mreadbuf_current;
82    char *mreadbuf_end;
83    size_t mreadbuf_length;
84
85    /**
86     * Thread that dispatches the session waitset.
87     */
88    struct thread *session_thread;
89
90    /**
91     * Reference count for the number of file descriptors that refer to this
92     * pseudo terminal. If the reference count drops to 0, the state is freed.
93     */
94    int opencount;
95
96    /**
97     * Termios structure of slave.
98     */
99    struct termios termios;
100
101    /**
102     * Winsize structure of slave.
103     */
104    struct winsize winsize;
105
106    /**
107     * File status flags as set by fctnl's F_GETFL and F_SETFL.
108     */
109    int file_status_flags;
110};
111
112/**
113 * Wrapper functions for master side.
114 */
115int     ptm_close(int fd);
116ssize_t ptm_read(int fd, void *buf, size_t count);
117ssize_t ptm_write(int fd, const void *buf, size_t count);
118
119/**
120 * Wrapper functions for slave side.
121 */
122int     pts_close(int fd);
123int     pts_open(const char *path, int oflags);
124ssize_t pts_read(int fd, void *buf, size_t count);
125ssize_t pts_write(int fd, const void *buf, size_t count);
126
127#endif // POSIXCOMPAT_PTY_H
128