Browse Source

change group to dialout

Signed-off-by: leyyer <15135994+leyyer@user.noreply.gitee.com>
master
leyyer 4 days ago
parent
commit
17485e0a7d
  1. 4
      Makefile
  2. 2
      contrib/serialmux.service
  3. 58
      src/main.c

4
Makefile

@ -10,8 +10,8 @@ CFLAGS += -Ilibev -DNDEBUG -Iduktape
LDFLAGS += -static -lutil -lm -Llibs -lev -Wl,-s
OBJS := src/main.o src/dictionary.o src/iniparser.o \
duktape/duktape.o
OBJS := src/main.o src/dictionary.o src/iniparser.o
#OBJS += duktape/duktape.o
serialmux: $(OBJS)
$(LD) -o $@ $^ $(LDFLAGS)

2
contrib/serialmux.service

@ -1,8 +1,6 @@
[Unit]
Description=Serial Mux Server
ConditionPathExists=/etc/serialmux.ini
Before=sysinit.target
After=systemd-sysusers.service
[Service]
Type=simple

58
src/main.c

@ -13,10 +13,10 @@
#include <errno.h>
#include <signal.h>
#include <pty.h>
#include <grp.h>
#include <ev.h>
#include "iniparser.h"
#include "ev.h"
#include "duktape.h"
#define MAJOR_VERSION 1
#define MINOR_VERSION 1
@ -34,7 +34,6 @@ struct tty_mux_session {
int connected;
int mfd;
int sfd;
int index;
@ -160,10 +159,10 @@ static void __reading_pty(EV_P_ ev_io *base, int events)
struct tty_mux_session *session_alloc(int index, const char *name_prefix)
{
char name[64];
const char *name;
struct tty_mux_session *mux;
int flags;
struct termios opts;
char cmd[512];
mux = calloc(1, sizeof *mux);
if (mux == NULL) {
@ -171,25 +170,51 @@ struct tty_mux_session *session_alloc(int index, const char *name_prefix)
return NULL;
}
if (openpty(&mux->mfd, &mux->sfd, name, NULL, NULL) == -1) {
fprintf(stderr, "Can't alloc mux %d\n", index);
mux->mfd = posix_openpt(O_RDWR | O_NOCTTY | O_NONBLOCK);
if (mux->mfd < 0) {
fprintf(stderr, "Can't openpyt\r\n");
free(mux);
return NULL;
}
if (grantpt(mux->mfd) < 0) {
close(mux->mfd);
fprintf(stderr, "Can't granpty\r\n");
free(mux);
return NULL;
}
if (unlockpt(mux->mfd) < 0) {
close(mux->mfd);
fprintf(stderr, "Can't unlockpt\r\n");
free(mux);
return NULL;
}
name = ptsname(mux->mfd);
if (name == NULL) {
close(mux->mfd);
fprintf(stderr, "Can't ptsname\r\n");
free(mux);
return NULL;
}
flags = fcntl(mux->mfd, F_GETFL);
flags |= O_NONBLOCK;
fcntl(mux->mfd, F_SETFL, flags);
snprintf(cmd, sizeof cmd, "chmod 0666 %s", name);
system(cmd);
printf("ptsname: %s\r\n", name);
if (tcgetattr(mux->mfd, &opts) == 0) {
cfmakeraw(&opts);
tcsetattr(mux->mfd,TCSANOW,&opts);
}
snprintf(mux->name, sizeof mux->name, "%s%d", name_prefix, index);
unlink(mux->name);
symlink(name, mux->name);
if (symlink(name, mux->name) < 0) {
printf("can't symlink %d\n", errno);
}
mux->connected = 0;
mux->index = index;
@ -202,7 +227,6 @@ struct tty_mux_session *session_alloc(int index, const char *name_prefix)
void session_close(struct tty_mux_session *ses)
{
close(ses->mfd);
close(ses->sfd);
unlink(ses->name);
free(ses);
}
@ -349,10 +373,19 @@ int main(int argc, char *argv[])
int brd, databits, stopbits, parity;
struct ev_loop *loop;
ev_io uart_io;
struct group *gr;
signal(SIGPIPE, SIG_IGN);
gr = getgrnam("dialout");
if (gr) {
if (setregid(gr->gr_gid, gr->gr_gid)) {
printf("setregid: %d (%s)\n", errno, strerror(errno));
}
}
printf("serialmux %d.%d build: %s %s\n", MAJOR_VERSION, MINOR_VERSION, __DATE__, __TIME__);
sfp = fopen(CONFIG_FILE, "rb");
if (sfp == NULL) {
fprintf(stderr, "Can't open %s to read\n", CONFIG_FILE);
@ -367,8 +400,7 @@ int main(int argc, char *argv[])
return -2;
}
serdev = iniparser_getstring(ini, "serial:device", "/dev/ttyS1");
serdev = iniparser_getstring(ini, "serial:device", "/dev/ttyAMA0");
brd = iniparser_getint(ini, "serial:baudrate", 115200);
databits = iniparser_getint(ini, "serial:databits", 8);
stopbits = iniparser_getint(ini, "serial:stopbits", 1);

Loading…
Cancel
Save