2016-07-15 04:20:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/serial/ptmx.c
|
|
|
|
*
|
2020-04-01 18:13:52 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-07-15 04:20:48 +02:00
|
|
|
*
|
2020-04-01 18:13:52 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-07-15 04:20:48 +02:00
|
|
|
*
|
2020-04-01 18:13:52 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-07-15 04:20:48 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdbool.h>
|
2016-07-15 14:48:10 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2016-07-15 04:20:48 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <poll.h>
|
2016-07-15 14:48:10 +02:00
|
|
|
#include <assert.h>
|
2016-07-15 04:20:48 +02:00
|
|
|
#include <errno.h>
|
2016-07-15 14:48:10 +02:00
|
|
|
|
2016-07-15 04:20:48 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2016-07-15 15:18:12 +02:00
|
|
|
#include <nuttx/serial/pty.h>
|
|
|
|
|
|
|
|
#include "pty.h"
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_PTY_MAXPTY
|
|
|
|
# define CONFIG_PTY_MAXPTY 32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_PTY_MAXPTY > 256
|
|
|
|
# define CONFIG_PTY_MAXPTY 256
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PTY_MAX ((CONFIG_PTY_MAXPTY + 31) & ~31)
|
|
|
|
#define INDEX_MAX (PTY_MAX >> 5)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* PTMX device state */
|
|
|
|
|
|
|
|
struct ptmx_dev_s
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t px_lock; /* Supports mutual exclusion */
|
2022-09-06 08:18:45 +02:00
|
|
|
uint8_t px_next; /* Next minor number to allocate */
|
2016-07-15 04:20:48 +02:00
|
|
|
uint32_t px_alloctab[INDEX_MAX]; /* Set of allocated PTYs */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ptmx_open(FAR struct file *filep);
|
|
|
|
static ssize_t ptmx_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t ptmx_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_ptmx_fops =
|
|
|
|
{
|
|
|
|
ptmx_open, /* open */
|
|
|
|
NULL, /* close */
|
|
|
|
ptmx_read, /* read */
|
|
|
|
ptmx_write, /* write */
|
|
|
|
};
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
static struct ptmx_dev_s g_ptmx =
|
|
|
|
{
|
|
|
|
NXMUTEX_INITIALIZER,
|
|
|
|
};
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ptmx_minor_allocate
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a new unique PTY minor number.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2022-09-06 08:18:45 +02:00
|
|
|
* Caller hold the px_lock
|
2016-07-15 04:20:48 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-15 14:48:10 +02:00
|
|
|
static int ptmx_minor_allocate(void)
|
2016-07-15 04:20:48 +02:00
|
|
|
{
|
|
|
|
uint8_t startaddr = g_ptmx.px_next;
|
|
|
|
uint8_t minor;
|
|
|
|
int index;
|
|
|
|
int bitno;
|
|
|
|
|
|
|
|
/* Loop until we find a valid device address */
|
|
|
|
|
|
|
|
for (; ; )
|
|
|
|
{
|
|
|
|
/* Try the next device address */
|
|
|
|
|
|
|
|
minor = g_ptmx.px_next;
|
|
|
|
if (g_ptmx.px_next >= PTY_MAX)
|
|
|
|
{
|
|
|
|
g_ptmx.px_next = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_ptmx.px_next++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is this address already allocated? */
|
|
|
|
|
|
|
|
index = minor >> 5;
|
|
|
|
bitno = minor & 31;
|
|
|
|
if ((g_ptmx.px_alloctab[index] & (1 << bitno)) == 0)
|
|
|
|
{
|
|
|
|
/* No... allocate it now */
|
|
|
|
|
|
|
|
g_ptmx.px_alloctab[index] |= (1 << bitno);
|
|
|
|
return (int)minor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This address has already been allocated. The following logic will
|
|
|
|
* prevent (unexpected) infinite loops.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (startaddr == minor)
|
|
|
|
{
|
|
|
|
/* We are back where we started... the are no free device address */
|
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ptmx_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ptmx_open(FAR struct file *filep)
|
|
|
|
{
|
2021-01-04 06:46:35 +01:00
|
|
|
struct file temp;
|
drivers/ptmx: Fix a potential buffer overflow
Fix warning:
```
serial/ptmx.c:205:34: warning: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 8 [-Wformat-truncation=]
205 | snprintf(devname, 16, "/dev/pty%d", minor);
|
serial/ptmx.c:205:25: note: directive argument in the range [0, 2147483647]
205 | snprintf(devname, 16, "/dev/pty%d", minor);
| ^~~~~~~~~~~~
serial/ptmx.c:205:3: note: ‘snprintf’ output between 10 and 19 bytes into a destination of size 16
205 | snprintf(devname, 16, "/dev/pty%d", minor);
```
Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
2023-06-09 15:10:44 +02:00
|
|
|
char devname[32];
|
2016-07-15 04:20:48 +02:00
|
|
|
int minor;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get exclusive access */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&g_ptmx.px_lock);
|
2020-04-01 18:13:52 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
/* Allocate a PTY minor */
|
|
|
|
|
|
|
|
minor = ptmx_minor_allocate();
|
|
|
|
if (minor < 0)
|
|
|
|
{
|
|
|
|
ret = minor;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
|
|
|
|
2016-07-15 22:09:02 +02:00
|
|
|
/* Create the master slave pair. This should create:
|
|
|
|
*
|
|
|
|
* Slave device: /dev/pts/N
|
|
|
|
* Master device: /dev/ptyN
|
|
|
|
*
|
|
|
|
* Where N=minor
|
|
|
|
*/
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
ret = pty_register2(minor, true);
|
2016-07-15 04:20:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_minor;
|
|
|
|
}
|
|
|
|
|
2016-07-15 22:09:02 +02:00
|
|
|
/* Open the master device: /dev/ptyN, where N=minor */
|
2016-07-15 04:20:48 +02:00
|
|
|
|
drivers/ptmx: Fix a potential buffer overflow
Fix warning:
```
serial/ptmx.c:205:34: warning: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 8 [-Wformat-truncation=]
205 | snprintf(devname, 16, "/dev/pty%d", minor);
|
serial/ptmx.c:205:25: note: directive argument in the range [0, 2147483647]
205 | snprintf(devname, 16, "/dev/pty%d", minor);
| ^~~~~~~~~~~~
serial/ptmx.c:205:3: note: ‘snprintf’ output between 10 and 19 bytes into a destination of size 16
205 | snprintf(devname, 16, "/dev/pty%d", minor);
```
Signed-off-by: Huang Qi <huangqi3@xiaomi.com>
2023-06-09 15:10:44 +02:00
|
|
|
snprintf(devname, sizeof(devname), "/dev/pty%d", minor);
|
2021-01-04 06:46:35 +01:00
|
|
|
memcpy(&temp, filep, sizeof(temp));
|
2023-09-13 15:08:13 +02:00
|
|
|
ret = file_open(filep, devname, O_RDWR | O_CLOEXEC);
|
2022-02-06 18:31:01 +01:00
|
|
|
DEBUGASSERT(ret >= 0); /* file_open() should never fail */
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2021-01-04 06:46:35 +01:00
|
|
|
/* Close the multiplexor device: /dev/ptmx */
|
|
|
|
|
|
|
|
ret = file_close(&temp);
|
2022-02-06 18:31:01 +01:00
|
|
|
DEBUGASSERT(ret >= 0); /* file_close() should never fail */
|
2021-01-04 06:46:35 +01:00
|
|
|
|
|
|
|
/* Now unlink the master. This will remove it from the VFS namespace,
|
2017-05-11 21:35:56 +02:00
|
|
|
* the driver will still persist because of the open count on the
|
2016-07-15 04:20:48 +02:00
|
|
|
* driver.
|
|
|
|
*/
|
|
|
|
|
2022-02-06 18:31:01 +01:00
|
|
|
ret = unregister_driver(devname);
|
2022-02-10 07:38:37 +01:00
|
|
|
DEBUGASSERT(ret >= 0 || ret == -EBUSY); /* unregister_driver() should never fail */
|
2016-07-15 04:20:48 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_ptmx.px_lock);
|
2021-01-04 06:46:35 +01:00
|
|
|
return OK;
|
2016-07-15 04:20:48 +02:00
|
|
|
|
|
|
|
errout_with_minor:
|
|
|
|
ptmx_minor_free(minor);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
nxmutex_unlock(&g_ptmx.px_lock);
|
2016-07-15 04:20:48 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ptmx_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
static ssize_t ptmx_read(FAR struct file *filep,
|
|
|
|
FAR char *buffer, size_t len)
|
2016-07-15 04:20:48 +02:00
|
|
|
{
|
|
|
|
return 0; /* Return EOF */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ptmx_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-01 18:13:52 +02:00
|
|
|
static ssize_t ptmx_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer, size_t len)
|
2016-07-15 04:20:48 +02:00
|
|
|
{
|
|
|
|
return len; /* Say that everything was written */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ptmx_register
|
|
|
|
*
|
2016-07-15 15:18:12 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
2016-07-15 04:20:48 +02:00
|
|
|
* Description:
|
2016-07-15 19:15:33 +02:00
|
|
|
* Register the master pseudo-terminal multiplexor device at /dev/ptmx
|
2016-07-15 15:18:12 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* any failure.
|
2016-07-15 04:20:48 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-07-15 15:18:12 +02:00
|
|
|
int ptmx_register(void)
|
2016-07-15 04:20:48 +02:00
|
|
|
{
|
|
|
|
/* Register the PTMX driver */
|
|
|
|
|
2016-07-15 15:18:12 +02:00
|
|
|
return register_driver("/dev/ptmx", &g_ptmx_fops, 0666, NULL);
|
2016-07-15 04:20:48 +02:00
|
|
|
}
|
2016-07-15 15:22:36 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ptmx_minor_free
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* De-allocate a PTY minor number.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2022-09-06 08:18:45 +02:00
|
|
|
* Caller hold the px_lock
|
2016-07-15 15:22:36 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void ptmx_minor_free(uint8_t minor)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int bitno;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&g_ptmx.px_lock);
|
2022-02-06 10:35:13 +01:00
|
|
|
|
2016-07-15 15:22:36 +02:00
|
|
|
/* Free the address by clearing the associated bit in the px_alloctab[]; */
|
|
|
|
|
|
|
|
index = minor >> 5;
|
|
|
|
bitno = minor & 31;
|
|
|
|
|
2022-02-06 10:38:49 +01:00
|
|
|
DEBUGASSERT((g_ptmx.px_alloctab[index] & (1 << bitno)) != 0);
|
2016-07-15 15:22:36 +02:00
|
|
|
g_ptmx.px_alloctab[index] &= ~(1 << bitno);
|
|
|
|
|
|
|
|
/* Reset the next pointer if the one just released has a lower value */
|
|
|
|
|
|
|
|
if (minor < g_ptmx.px_next)
|
|
|
|
{
|
|
|
|
g_ptmx.px_next = minor;
|
|
|
|
}
|
2022-02-06 10:35:13 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_ptmx.px_lock);
|
2016-07-15 15:22:36 +02:00
|
|
|
}
|