minix/minix/fs/ptyfs/node.c

85 lines
1.8 KiB
C
Raw Normal View History

Add PTYFS, Unix98 pseudo terminal support This patch adds support for Unix98 pseudo terminals, that is, posix_openpt(3), grantpt(3), unlockpt(3), /dev/ptmx, and /dev/pts/. The latter is implemented with a new pseudo file system, PTYFS. In effect, this patch adds secure support for unprivileged pseudo terminal allocation, allowing programs such as tmux(1) to be used by non-root users as well. Test77 has been extended with new tests, and no longer needs to run as root. The new functionality is optional. To revert to the old behavior, remove the "ptyfs" entry from /etc/fstab. Technical nodes: o The reason for not implementing the NetBSD /dev/ptm approach is that implementing the corresponding ioctl (TIOCPTMGET) would require adding a number of extremely hairy exceptions to VFS, including the PTY driver having to create new file descriptors for its own device nodes. o PTYFS is required for Unix98 PTYs in order to avoid that the PTY driver has to be aware of old-style PTY naming schemes and even has to call chmod(2) on a disk-backed file system. PTY cannot be its own PTYFS since a character driver may currently not also be a file system. However, PTYFS may be subsumed into a DEVFS in the future. o The Unix98 PTY behavior differs somewhat from NetBSD's, in that slave nodes are created on ptyfs only upon the first call to grantpt(3). This approach obviates the need to revoke access as part of the grantpt(3) call. o Shutting down PTY may leave slave nodes on PTYFS, but once PTY is restarted, these leftover slave nodes will be removed before they create a security risk. Unmounting PTYFS will make existing PTY slaves permanently unavailable, and absence of PTYFS will block allocation of new Unix98 PTYs until PTYFS is (re)mounted. Change-Id: I822b43ba32707c8815fd0f7d5bb7a438f51421c1
2015-06-22 19:14:34 +02:00
/* PTYFS slave node management */
/*
* While the interface of this module should be flexible enough to implement
* various memory management approaches, the current code simply relies on
* NR_PTYS being small enough to preallocate all data structures. In the
* future, NR_PTYS will no longer be a system-global definition, and future
* implementations of this module should not rely on NR_PTYS at all.
*/
#include <minix/drivers.h>
#include "node.h"
static bitchunk_t node_map[BITMAP_CHUNKS(NR_PTYS)];
static struct node_data node_data[NR_PTYS];
/*
* Initialize the node module.
*/
void
init_nodes(void)
{
memset(&node_map, 0, sizeof(node_map));
}
/*
* Allocate a node with a given node index number, and save node data for it.
* It is possible that the node is in use already; in that case, only update
* its associated data. Return OK on success, or an error code on failure.
*/
int
set_node(node_t index, struct node_data * data)
{
if (index >= NR_PTYS)
return ENOMEM;
SET_BIT(node_map, index);
node_data[index] = *data;
return OK;
}
/*
* Deallocate a node using its node index number. This function always
* succeeds, intentionally ignoring the case that the node was not allocated.
*/
void
clear_node(node_t index)
{
UNSET_BIT(node_map, index);
}
/*
* Return a pointer to the node data associated with the given node index
* number. If the node is not allocated, return NULL.
*/
struct node_data *
get_node(node_t index)
{
if (index >= NR_PTYS || !GET_BIT(node_map, index))
return NULL;
return &node_data[index];
}
/*
* Return the highest allocated node index number, plus one. This value is
* used to check given node indices and limit linear iterations.
*/
node_t
get_max_node(void)
{
/*
* NR_PTYS is low enough that we can always return it instead of
* tracking the actual value.
*/
return NR_PTYS;
}