Skip to content

Commit

Permalink
Add additional port locking for root users
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Mar 7, 2019
1 parent 11896f4 commit 051ba9f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
35 changes: 33 additions & 2 deletions src/main/c/Posix/PosixHelperFunctions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* PosixHelperFunctions.c
*
* Created on: Mar 10, 2015
* Last Updated on: Dec 07, 2018
* Last Updated on: Mar 07, 2019
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
* Copyright (C) 2012-2019 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
Expand All @@ -24,6 +24,7 @@
*/

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
Expand Down Expand Up @@ -613,6 +614,36 @@ void searchForComPorts(charTupleVector* comPorts)
}
}

int flock(int fd, int op)
{
int rc = 0;

#if defined(F_SETLK) && defined(F_SETLKW)
struct flock fl = {0};
switch (op & (LOCK_EX|LOCK_SH|LOCK_UN))
{
case LOCK_EX:
fl.l_type = F_WRLCK;
break;
case LOCK_SH:
fl.l_type = F_RDLCK;
break;
case LOCK_UN:
fl.l_type = F_UNLCK;
break;
default:
errno = EINVAL;
return -1;
}
fl.l_whence = SEEK_SET;
rc = fcntl(fd, op & LOCK_NB ? F_SETLK : F_SETLKW, &fl);
if (rc && (errno == EAGAIN))
errno = EWOULDBLOCK;
#endif

return rc;
}

baud_rate getBaudRateCode(baud_rate baudRate)
{
// Translate a raw baud rate into a system-specified one
Expand Down
9 changes: 7 additions & 2 deletions src/main/c/Posix/PosixHelperFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* PosixHelperFunctions.h
*
* Created on: Mar 10, 2015
* Last Updated on: Nov 12, 2018
* Last Updated on: Mar 07, 2019
* Author: Will Hedgecock
*
* Copyright (C) 2012-2018 Fazecast, Inc.
* Copyright (C) 2012-2019 Fazecast, Inc.
*
* This file is part of jSerialComm.
*
Expand Down Expand Up @@ -53,8 +53,13 @@ void driverBasedSearchForComPorts(charTupleVector* comPorts);

// Solaris-specific functionality
#elif defined(__sun__)
#define LOCK_SH 1
#define LOCK_EX 2
#define LOCK_NB 4
#define LOCK_UN 8
typedef int baud_rate;
extern int ioctl(int __fd, int __request, ...);
int flock(int fd, int op);
void searchForComPorts(charTupleVector* comPorts);

// Apple-specific functionality
Expand Down

0 comments on commit 051ba9f

Please sign in to comment.