Go to:
Gentoo Home
Documentation
Forums
Lists
Bugs
Planet
Store
Wiki
Get Gentoo!
Gentoo's Bugzilla – Attachment 373816 Details for
Bug 506146
media-radio/ax25-tools enhancements
Home
|
New
–
[Ex]
|
Browse
|
Search
|
Privacy Policy
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
ax25-tools-kissnetd-pty.diff: Add symlinking support to kissnetd
ax25-tools-kissnetd-pty.diff (text/plain), 13.48 KB, created by
Stuart Longland
on 2014-03-29 12:28:18 UTC
(
hide
)
Description:
ax25-tools-kissnetd-pty.diff: Add symlinking support to kissnetd
Filename:
MIME Type:
Creator:
Stuart Longland
Created:
2014-03-29 12:28:18 UTC
Size:
13.48 KB
patch
obsolete
>--- kiss/kissnetd.c.orig 2014-01-11 11:33:59.332000000 +1000 >+++ kiss/kissnetd.c 2014-01-11 12:47:50.938000000 +1000 >@@ -8,12 +8,13 @@ > * > * F1OAT 960804 - Frederic RIBLE > */ >- >+ > #include <stdio.h> > #define __USE_XOPEN > #include <stdlib.h> > #include <sys/time.h> > #include <sys/types.h> >+#include <sys/stat.h> > #include <string.h> > #include <unistd.h> > #include <assert.h> >@@ -23,11 +24,17 @@ > #include <syslog.h> > #include <time.h> > #include <limits.h> >+#include <signal.h> >+ >+#include <pwd.h> >+#include <grp.h> > > static char *Version = "1.5"; > static int VerboseMode = 0; > static int MaxFrameSize = 512; > >+#define PTMX_TTY "/dev/ptmx" >+ > #define REOPEN_TIMEOUT 30 /* try tio reopen every 10 s */ > > struct PortDescriptor { >@@ -39,20 +46,34 @@ > char namepts[PATH_MAX]; /* name of the unix98 pts slaves, which > * the client has to use */ > int is_active; >+ int is_unix98; >+ int is_symlinked; >+ >+ int do_chown; >+ uid_t pty_uid; >+ gid_t pty_gid; >+ >+ mode_t pty_mode; > }; > > static struct PortDescriptor *PortList[FD_SETSIZE]; > > static int NbPort = 0; > >+static int SetPtyOwner = 0; >+static uid_t PtyOwnerUid = 0; >+static gid_t PtyOwnerGid = 0; >+ >+static mode_t PtyMode = 0; >+ > static void Usage(void) > { >- fprintf(stderr, "\nUsage : kissnetd [-v] [-f size] [-p num | /dev/pty?? [/dev/pty??]* ]\n"); >+ fprintf(stderr, "\nUsage : kissnetd [-v] [-f size] [-p num | /dev/pty?? [/dev/pty??]* | [owner=USER:GROUP] [mode=MODE] pty:/dev/symlink1 [pty:/dev/symlink2]* ]\n"); > fprintf(stderr, " -v : Verbose mode, trace on stdout\n"); > fprintf(stderr, " -f size : Set max frame size to size bytes (default 512)\n"); > fprintf(stderr, " -p num : Number of /dev/ptmx-master-devices has to open\n"); > exit(1); >-} >+} > > static void Banner(int Small) > { >@@ -64,7 +85,7 @@ > printf("* Network broadcast between kiss ports *\n"); > printf("* ATEPRA FPAC/Linux Project *\n"); > printf("****************************************\n"); >- printf("* kissnetd Version %-4s *\n", Version); >+ printf("* kissnetd Version %-4s *\n", Version); > printf("* by Frederic RIBLE F1OAT *\n"); > printf("****************************************\n"); > } >@@ -73,30 +94,41 @@ > static void NewPort(char *Name) > { > struct PortDescriptor *MyPort; >- >+ > if (VerboseMode) { > printf("Opening port %s\n", Name); > } >- >+ > if (NbPort == FD_SETSIZE) { > fprintf(stderr, "Cannot handle %s : too many ports\n", Name); > exit(1); > } >- >+ > MyPort = calloc(sizeof(struct PortDescriptor), 1); > if (MyPort) MyPort->FrameBuffer = calloc(sizeof (unsigned char), MaxFrameSize); > if (!MyPort || !MyPort->FrameBuffer) { > perror("cannot allocate port descriptor"); > exit(1); > } >- >- strncpy(MyPort->Name, Name, PATH_MAX-1); >+ >+ MyPort->is_symlinked = (strstr(Name, "pty:") == Name); >+ MyPort->is_unix98 = MyPort->is_symlinked >+ || (!strcmp(PTMX_TTY, Name)); >+ >+ strncpy(MyPort->Name, (MyPort->is_symlinked >+ ? index(Name, ':') + 1 >+ : Name), PATH_MAX-1); >+ > MyPort->Name[PATH_MAX-1] = '\0'; > MyPort->Fd = -1; > MyPort->FrameBuffer[0] = 0xC0; > MyPort->BufferIndex = 1; > MyPort->namepts [0] = '\0'; > MyPort->is_active = 0; >+ MyPort->do_chown = SetPtyOwner; >+ MyPort->pty_uid = PtyOwnerUid; >+ MyPort->pty_gid = PtyOwnerGid; >+ MyPort->pty_mode= PtyMode; > PortList[NbPort++] = MyPort; > } > >@@ -104,17 +136,20 @@ > { > char MyString[80]; > PortList[PortNumber]->TimeLastOpen = time(NULL); >- >+ > if (VerboseMode) { > printf("Reopening port %d\n", PortNumber); > } >- >+ > if (PortList[PortNumber]->namepts[0] == '\0') { >- >+ > syslog(LOG_WARNING, "kissnetd : Opening port %s\n", PortList[PortNumber]->Name); >- PortList[PortNumber]->Fd = open(PortList[PortNumber]->Name, O_RDWR | O_NONBLOCK); >+ PortList[PortNumber]->Fd = open( >+ (PortList[PortNumber]->is_symlinked >+ ? PTMX_TTY >+ : PortList[PortNumber]->Name), O_RDWR | O_NONBLOCK); > if (PortList[PortNumber]->Fd < 0) { >- syslog(LOG_WARNING, "kissnetd : Error opening port %s : %s\n", >+ syslog(LOG_WARNING, "kissnetd : Error opening port %s : %s\n", > PortList[PortNumber]->Name, strerror(errno)); > if (VerboseMode) { > sprintf(MyString, "cannot reopen %s", PortList[PortNumber]->Name); >@@ -123,12 +158,12 @@ > return; > } > PortList[PortNumber]->is_active = 1; >- if (!strcmp(PortList[PortNumber]->Name, "/dev/ptmx")) { >+ if (PortList[PortNumber]->is_unix98) { > char *npts; > /* get name of pts-device */ > if ((npts = ptsname(PortList[PortNumber]->Fd)) == NULL) { > sprintf(MyString, "Cannot get name of pts-device.\n"); >- syslog(LOG_WARNING, "kissnetd : Cannot get name of pts-device\n"); >+ syslog(LOG_WARNING, "kissnetd : Cannot get name of pts-device\n"); > exit(1); > } > strncpy(PortList[PortNumber]->namepts, npts, PATH_MAX-1); >@@ -140,11 +175,39 @@ > syslog(LOG_WARNING, "kissnetd : Cannot unlock pts-device %s\n", PortList[PortNumber]->namepts); > exit(1); > } >- syslog(LOG_WARNING, "kissnetd : Using /dev/ptmx with slave pty %s\n", PortList[PortNumber]->namepts); >+ >+ if (PortList[PortNumber]->do_chown) { >+ if (chown(PortList[PortNumber]->namepts, >+ PortList[PortNumber]->pty_uid, >+ PortList[PortNumber]->pty_gid)) { >+ syslog(LOG_CRIT, "Cannot chown %s\n", >+ PortList[PortNumber]->namepts); >+ exit(1); >+ } >+ } >+ if (PortList[PortNumber]->pty_mode) { >+ if (chmod(PortList[PortNumber]->namepts, >+ PortList[PortNumber]->pty_mode)) { >+ syslog(LOG_CRIT, "Cannot chmod %s\n", >+ PortList[PortNumber]->namepts); >+ exit(1); >+ } >+ } >+ >+ if (PortList[PortNumber]->is_symlinked) { >+ /* Create symbolic link */ >+ if (symlink(PortList[PortNumber]->namepts, PortList[PortNumber]->Name)) { >+ syslog(LOG_CRIT, "Cannot symlink %s to %s\n", PortList[PortNumber]->namepts, >+ PortList[PortNumber]->Name); >+ exit(1); >+ } >+ } else { >+ syslog(LOG_WARNING, "kissnetd : Using /dev/ptmx with slave pty %s\n", PortList[PortNumber]->namepts); >+ } > } > } else { > if (PortList[PortNumber]->Fd == -1) { >- syslog(LOG_WARNING, "kissnetd : Cannot reopen port ptmx (slave %s) : not supported by ptmx-device\n", >+ syslog(LOG_WARNING, "kissnetd : Cannot reopen port ptmx (slave %s) : not supported by ptmx-device\n", > PortList[PortNumber]->namepts); > if (VerboseMode) { > sprintf(MyString, "cannot reopen ptmx (slave %s).", PortList[PortNumber]->namepts); >@@ -152,7 +215,7 @@ > } > return; > } >- syslog(LOG_WARNING, "kissnetd : Trying to poll port ptmx (slave %s).\n", >+ syslog(LOG_WARNING, "kissnetd : Trying to poll port ptmx (slave %s).\n", > PortList[PortNumber]->namepts); > PortList[PortNumber]->is_active = 1; > } >@@ -163,12 +226,12 @@ > int i; > static int wrote_info = 0; > time_t CurrentTime = time(NULL); >- >+ > for (i=0; i<NbPort; i++) { > if (PortList[i]->Fd >= 0 && PortList[i]->is_active == 1) continue; > if ( (CurrentTime - PortList[i]->TimeLastOpen) > REOPEN_TIMEOUT ) ReopenPort(i); > } >- >+ > if (!wrote_info) { > for (i=0; i<NbPort; i++) { > if (PortList[i]->namepts[0] != '\0') { >@@ -197,37 +260,37 @@ > { > int i; > int rc; >- >+ > /* Broadcast only info frames */ >- >+ > if (PortList[InputPort]->FrameBuffer[1] != 0x00 && \ > PortList[InputPort]->FrameBuffer[1] != 0x20 && \ > PortList[InputPort]->FrameBuffer[1] != 0x80) > return; >- >+ > for (i=0; i<NbPort; i++) { > int offset = 0; > if (i == InputPort) continue; > if (PortList[i]->Fd < 0 || PortList[i]->is_active == 0) continue; > again: >- rc = write(PortList[i]->Fd, >- PortList[InputPort]->FrameBuffer+offset, >+ rc = write(PortList[i]->Fd, >+ PortList[InputPort]->FrameBuffer+offset, > PortList[InputPort]->BufferIndex-offset); > if (rc < 0) { > if (errno == EAGAIN) { > if (PortList[i]->namepts[0] == '\0') >- syslog(LOG_WARNING, "kissnetd : write buffer full on port %s. dropping frame. %s", >+ syslog(LOG_WARNING, "kissnetd : write buffer full on port %s. dropping frame. %s", > PortList[i]->Name, strerror(errno)); > else >- syslog(LOG_WARNING, "kissnetd : write buffer full on ptmx port %s. dropping frame. %s", >+ syslog(LOG_WARNING, "kissnetd : write buffer full on ptmx port %s. dropping frame. %s", > PortList[i]->namepts, strerror(errno)); > continue; > } > if (PortList[i]->namepts[0] == '\0') >- syslog(LOG_WARNING, "kissnetd : Error writing to port %s : %s\n", >+ syslog(LOG_WARNING, "kissnetd : Error writing to port %s : %s\n", > PortList[i]->Name, strerror(errno)); > else >- syslog(LOG_WARNING, "kissnetd : Error writing to port ptmx (slave %s) : %s\n", >+ syslog(LOG_WARNING, "kissnetd : Error writing to port ptmx (slave %s) : %s\n", > PortList[i]->namepts, strerror(errno)); > if (VerboseMode) perror("write"); > PortList[i]->is_active = 0; >@@ -241,7 +304,7 @@ > printf("Sending %d bytes on port %d : rc=%d\n", > PortList[InputPort]->BufferIndex, > i, rc); >- } >+ } > if (rc < PortList[InputPort]->BufferIndex-offset) { > offset += rc; > goto again; >@@ -255,7 +318,7 @@ > int Length; > int i; > struct PortDescriptor *MyPort = PortList[PortNumber]; >- >+ > Length = read(MyPort->Fd, MyBuffer, sizeof(MyBuffer)); > if (VerboseMode) { > printf("Read port %d : rc=%d\n", PortNumber, Length); >@@ -265,10 +328,10 @@ > if (errno == EAGAIN) > return; > if (MyPort->namepts[0] == '\0') >- syslog(LOG_WARNING, "kissnetd : Error reading from port %s : %s\n", >+ syslog(LOG_WARNING, "kissnetd : Error reading from port %s : %s\n", > PortList[PortNumber]->Name, strerror(errno)); > else >- syslog(LOG_WARNING, "kissnetd : Error reading from port ptmx (slave %s) : %s\n", >+ syslog(LOG_WARNING, "kissnetd : Error reading from port ptmx (slave %s) : %s\n", > PortList[PortNumber]->namepts, strerror(errno)); > if (VerboseMode) perror("read"); > MyPort->is_active = 0; >@@ -285,11 +348,11 @@ > MyPort->BufferIndex = 1; > } > } >- else { >+ else { > MyPort->FrameBuffer[MyPort->BufferIndex++] = MyBuffer[i]; > if (MyBuffer[i] == 0xC0) { > Broadcast(PortNumber); >- MyPort->BufferIndex = 1; >+ MyPort->BufferIndex = 1; > } > } > } >@@ -300,19 +363,19 @@ > static fd_set MyFdSet; > int i, rc; > struct timeval Timeout; >- >+ > Timeout.tv_sec = 1; > Timeout.tv_usec = 0; >- >+ > FD_ZERO(&MyFdSet); > for (i=0; i<NbPort; i++) { > if (PortList[i]->Fd >= 0 && PortList[i]->is_active) FD_SET(PortList[i]->Fd, &MyFdSet); > } > rc = select(FD_SETSIZE, &MyFdSet, NULL, NULL, &Timeout); >- >+ > if (VerboseMode) printf("select : rc=%d\n", rc); > if (!rc ) TickReopen(); >- >+ > if (rc > 0) { > for (i=0; i<NbPort && rc; i++) { > if (PortList[i]->Fd < 0) continue; >@@ -321,7 +384,64 @@ > rc--; > } > } >- } >+ } >+} >+ >+/* >+ * Parse the owner string; given as a user:group pair. Result is stored >+ * in PtyOwnerUid and PtyOwnerGid; and overrides previously set defaults. >+ */ >+static int ParseOwner(char* owner) >+{ >+ char* colon; >+ char* pty_owner_group; >+ >+ if (!strcmp(owner, "default")) { >+ SetPtyOwner = 0; >+ return 0; >+ } >+ >+ colon = index(owner, ':'); >+ if (!colon) { >+ fprintf(stderr, "Owner must be given in the form user:group\n"); >+ return 1; >+ } >+ *colon = 0; >+ pty_owner_group = colon + 1; >+ >+ if (owner) { >+ /* Look up the user */ >+ struct passwd user; >+ struct passwd* user_ptr = NULL; >+ struct group grp; >+ struct group* grp_ptr = NULL; >+ char buffer[512]; >+ int err = getpwnam_r(owner, &user, >+ buffer, sizeof(buffer)-1, &user_ptr); >+ if (err) { >+ fprintf(stderr, >+ "Failed to look up user: %s (%d)\n", >+ strerror(err), err); >+ return 1; >+ } >+ >+ err = getgrnam_r(pty_owner_group, &grp, >+ buffer, sizeof(buffer)-1, &grp_ptr); >+ if (err) { >+ fprintf(stderr, >+ "Failed to look up group: %s (%d)\n", >+ strerror(err), err); >+ return 1; >+ } >+ >+ if (user_ptr && grp_ptr) { >+ PtyOwnerUid = user_ptr->pw_uid; >+ PtyOwnerGid = grp_ptr->gr_gid; >+ SetPtyOwner = 1; >+ return 0; >+ } >+ } >+ return 1; > } > > static void ProcessArgv(int argc, char *argv[]) >@@ -329,7 +449,7 @@ > int opt; > int i=0; > int ptmxdevices = 0; >- >+ > while ((opt = getopt(argc, argv, "vf:p:")) != -1) { > switch (opt) { > case 'v': >@@ -345,7 +465,16 @@ > exit(1); > } > for (i=0; i < ptmxdevices; i++) >- NewPort("/dev/ptmx"); >+ NewPort(PTMX_TTY); >+ break; >+ case 'o': >+ /* Change the owner of the ptys */ >+ if (ParseOwner(optarg)) >+ exit(1); >+ break; >+ case 'm': >+ /* Change the mode of the pty */ >+ PtyMode = (mode_t)strtoul(optarg, NULL, 8); > break; > default: > fprintf(stderr, "Invalid option %s\n", argv[optind]); >@@ -353,9 +482,19 @@ > exit(1); > } > } >- >- while (optind < argc) >- NewPort(argv[optind++]); >+ >+ while (optind < argc) { >+ char* arg = argv[optind++]; >+ if (strstr(arg, "owner=") == arg) { >+ if (ParseOwner(index(arg,'=')+1)) >+ exit(1); >+ } else if (strstr(arg, "mode=") == arg) { >+ PtyMode = (mode_t)strtoul(index(arg,'=')+1, >+ NULL, 8); >+ } else { >+ NewPort(arg); >+ } >+ } > > if (NbPort < 2) { > fprintf(stderr, "This multiplexer needs at least two pty's\n"); >@@ -363,8 +502,25 @@ > } > } > >+void cleanup(void) >+{ >+ /* Clean up symbolic links */ >+ int i; >+ for (i=0; i<NbPort; i++) { >+ if (PortList[i]->is_symlinked) { >+ if (unlink(PortList[i]->Name)) >+ syslog(LOG_WARNING, "Failed to unlink %s", >+ PortList[i]->Name); >+ } >+ } >+} >+void cleanup_sig(int signal) >+{ >+ cleanup(); >+ exit(0); >+} > >-int main(int argc, char *argv[]) >+int main(int argc, char *argv[]) > { > if (argc < 2) { > Banner(0); >@@ -373,8 +529,13 @@ > else { > Banner(1); > } >- >+ >+ atexit(cleanup); >+ signal(SIGTERM, cleanup_sig); >+ signal(SIGINT, cleanup_sig); >+ signal(SIGQUIT, cleanup_sig); >+ > ProcessArgv(argc, argv); > while (1) ProcessPortList(); >- return 0; >+ return 0; > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 506146
:
373814
| 373816 |
373818
|
373820