acassis commented on code in PR #14823: URL: https://github.com/apache/nuttx/pull/14823#discussion_r1854456540
########## tools/cmux.c: ########## @@ -0,0 +1,434 @@ +/**************************************************************************** + * tools/cmux.c + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <termios.h> +#include <unistd.h> + +#include <sys/ioctl.h> +#include <sys/stat.h> +#include <sys/sysmacros.h> + +#include <linux/gsmmux.h> +#include <linux/tty.h> + +/* Maximum transfert unit (MTU), value in bytes */ + +#define MTU 127 +#define MRU 127 + +/* Name of the virtual TTYs to create */ + +#define DEVICE_NAME "/dev/ttyGSM" + +/* Name of the driver, used to get the major number */ + +#define DRIVER_NAME "gsmtty" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: get_major + * + * Description: + * Gets the major number of the driver device. + * Returns the major number on success, -1 on failure. + * + ****************************************************************************/ + +static int get_major(const char *driver) +{ + char device[NAME_MAX]; + char *line = NULL; + int major = -1; + size_t len = 0; + ssize_t read; + FILE *fp; + + /* Open /proc/devices file */ + + if ((fp = fopen("/proc/devices", "r")) == NULL) + { + perror("Cannot open /proc/devices\n"); + return -1; + } + + while ((major == -1) && (read = getline(&line, &len, fp)) != -1) + { + if (strstr(line, driver) != NULL) + { + if (sscanf(line, "%d %s\n", &major, device) != 2) + { + major = -1; + } + } + + /* Free the line before getting a new one */ + + free(line); + line = NULL; + } + + fclose(fp); + return major; +} + +/**************************************************************************** + * Name: make_nodes + * + * Description: + * Creates the virtual TTY nodes. + * Returns the number of nodes created. + * + ****************************************************************************/ + +int make_nodes(int major, const char *basename, int number) +{ + char devname[PATH_MAX]; + int created = 0; + mode_t oldmask; + dev_t device; + int minor; + + /* Set a new mask to get 666 mode and stores the old one */ + + oldmask = umask(0); + + for (minor = 1; minor <= number; minor++) + { + /* Append the minor number to the base name */ + + sprintf(devname, "%s%d", basename, minor); + + /* Store a device info with major and minor */ + + device = makedev(major, minor); + + /* Create the actual character node */ + + if (mknod(devname, S_IFCHR | 0666, device) != 0) Review Comment: @xiaoxiang781216 I think mknod will require root access, doesn't it? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org