Skip to content

Enable compilation on Solaris #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions blink/flock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef BLINK_FLOCK_H_
#define BLINK_FLOCK_H_

#if defined(sun) || defined(__sun)

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>

#define LOCK_SH 1 // shared lock
#define LOCK_EX 2 // exclusive lock
#define LOCK_UN 8 // unlock
#define LOCK_NB 4 // non-blocking

static int flock(int fd, int operation) {
struct flock fl = {0};

fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0; // Lock the whole file

switch (operation & (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;
}

return fcntl(fd, (operation & LOCK_NB) ? F_SETLK : F_SETLKW, &fl);
}

#endif

#endif /* BLINK_FLOCK_H_ */
81 changes: 53 additions & 28 deletions blink/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -4422,8 +4422,25 @@ static i32 Select(struct Machine *m, //
int fildes, rc;
i32 setsize;
u64 oldmask_guest = 0;
fd_set readfds, writefds, exceptfds, readyreadfds, readywritefds,
readyexceptfds;
fd_set *readfds, *writefds, *exceptfds, *readyreadfds, *readywritefds,
*readyexceptfds;
#if defined(sun) || defined(__sun)
readfds = (fd_set*)malloc(sizeof(fd_set));
writefds = (fd_set*)malloc(sizeof(fd_set));
exceptfds = (fd_set*)malloc(sizeof(fd_set));
readyreadfds = (fd_set*)malloc(sizeof(fd_set));
readywritefds = (fd_set*)malloc(sizeof(fd_set));
readyexceptfds = (fd_set*)malloc(sizeof(fd_set));
#else
fd_set _readfds, _writefds, _exceptfds, _readyreadfds, _readywritefds,
_readyexceptfds;
readfds = &_readfds;
writefds = &_writefds;
exceptfds = &_exceptfds;
readyreadfds = &_readyreadfds;
readywritefds = &_readywritefds;
readyexceptfds = &_readyexceptfds;
#endif
struct pollfd hfds[1];
struct timespec now, wait, remain, deadline = {0};
struct Fd *fd;
Expand All @@ -4437,29 +4454,29 @@ static i32 Select(struct Machine *m, //
return einval();
}
if (readfds_addr) {
if (LoadFdSet(m, nfds, &readfds, readfds_addr) == -1) {
if (LoadFdSet(m, nfds, readfds, readfds_addr) == -1) {
return -1;
}
} else {
FD_ZERO(&readfds);
FD_ZERO(readfds);
}
if (writefds_addr) {
if (LoadFdSet(m, nfds, &writefds, writefds_addr) == -1) {
if (LoadFdSet(m, nfds, writefds, writefds_addr) == -1) {
return -1;
}
} else {
FD_ZERO(&writefds);
FD_ZERO(writefds);
}
if (exceptfds_addr) {
if (LoadFdSet(m, nfds, &exceptfds, exceptfds_addr) == -1) {
if (LoadFdSet(m, nfds, exceptfds, exceptfds_addr) == -1) {
return -1;
}
} else {
FD_ZERO(&exceptfds);
FD_ZERO(exceptfds);
}
FD_ZERO(&readyreadfds);
FD_ZERO(&readywritefds);
FD_ZERO(&readyexceptfds);
FD_ZERO(readyreadfds);
FD_ZERO(readywritefds);
FD_ZERO(readyexceptfds);
if (sigmaskp_guest) {
oldmask_guest = m->sigmask;
m->sigmask = *sigmaskp_guest;
Expand All @@ -4472,8 +4489,8 @@ static i32 Select(struct Machine *m, //
}
rc = 0;
for (fildes = 0; fildes < nfds; ++fildes) {
if (!FD_ISSET(fildes, &readfds) && !FD_ISSET(fildes, &writefds) &&
!FD_ISSET(fildes, &exceptfds)) {
if (!FD_ISSET(fildes, readfds) && !FD_ISSET(fildes, writefds) &&
!FD_ISSET(fildes, exceptfds)) {
continue;
}
TryAgain:
Expand All @@ -4491,27 +4508,27 @@ static i32 Select(struct Machine *m, //
UNLOCK(&m->system->fds.lock);
if (fd) {
hfds[0].fd = fildes;
hfds[0].events = ((FD_ISSET(fildes, &readfds) ? POLLIN : 0) |
(FD_ISSET(fildes, &writefds) ? POLLOUT : 0) |
(FD_ISSET(fildes, &exceptfds) ? POLLPRI : 0));
hfds[0].events = ((FD_ISSET(fildes, readfds) ? POLLIN : 0) |
(FD_ISSET(fildes, writefds) ? POLLOUT : 0) |
(FD_ISSET(fildes, exceptfds) ? POLLPRI : 0));
switch (poll_impl(hfds, 1, 0)) {
case 0:
break;
case 1:
if (FD_ISSET(fildes, &readfds) && (hfds[0].revents & POLLIN)) {
if (FD_ISSET(fildes, readfds) && (hfds[0].revents & POLLIN)) {
++rc;
FD_SET(fildes, &readyreadfds);
FD_CLR(fildes, &readfds);
FD_SET(fildes, readyreadfds);
FD_CLR(fildes, readfds);
}
if (FD_ISSET(fildes, &writefds) && (hfds[0].revents & POLLOUT)) {
if (FD_ISSET(fildes, writefds) && (hfds[0].revents & POLLOUT)) {
++rc;
FD_SET(fildes, &readywritefds);
FD_CLR(fildes, &writefds);
FD_SET(fildes, readywritefds);
FD_CLR(fildes, writefds);
}
if (FD_ISSET(fildes, &exceptfds) && (hfds[0].revents & POLLPRI)) {
if (FD_ISSET(fildes, exceptfds) && (hfds[0].revents & POLLPRI)) {
++rc;
FD_SET(fildes, &readyexceptfds);
FD_CLR(fildes, &exceptfds);
FD_SET(fildes, readyexceptfds);
FD_CLR(fildes, exceptfds);
}
break;
case -1:
Expand Down Expand Up @@ -4547,11 +4564,11 @@ static i32 Select(struct Machine *m, //
}
if (rc != -1) {
if ((readfds_addr &&
SaveFdSet(m, nfds, &readyreadfds, readfds_addr) == -1) ||
SaveFdSet(m, nfds, readyreadfds, readfds_addr) == -1) ||
(writefds_addr &&
SaveFdSet(m, nfds, &readywritefds, writefds_addr) == -1) ||
SaveFdSet(m, nfds, readywritefds, writefds_addr) == -1) ||
(exceptfds_addr &&
SaveFdSet(m, nfds, &readyexceptfds, exceptfds_addr) == -1)) {
SaveFdSet(m, nfds, readyexceptfds, exceptfds_addr) == -1)) {
return -1;
}
}
Expand All @@ -4564,6 +4581,14 @@ static i32 Select(struct Machine *m, //
*timeoutp = GetZeroTime();
}
}
#endif
#if defined(sun) || defined(__sun)
free(readfds);
free(writefds);
free(exceptfds);
free(readyreadfds);
free(readywritefds);
free(readyexceptfds);
#endif
return rc;
}
Expand Down
1 change: 1 addition & 0 deletions blink/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "blink/builtin.h"
#include "blink/fds.h"
#include "blink/flock.h"
#include "blink/machine.h"
#include "blink/ndelay.h"
#include "blink/types.h"
Expand Down
23 changes: 16 additions & 7 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,22 @@ hassstr() {
fi
}

replace() {
exec 7<>"${LOCK}"
"${FLOCK}" -x 7
sed "s@$1@$2@" <"${CONFIG}" >"${CONFIG}.tmp" &&
mv -f "${CONFIG}.tmp" "${CONFIG}"
exec 7<&-
}
if [ "${HOST_SYSTEM}" = "SunOS" ]; then
replace() {
"${FLOCK}" -x "${LOCK}.1"
sed "s@$1@$2@" <"${CONFIG}" >"${CONFIG}.tmp" &&
mv -f "${CONFIG}.tmp" "${CONFIG}"
"${FLOCK}" -u "${LOCK}.1"
}
else
replace() {
exec 7<>"${LOCK}"
"${FLOCK}" -x 7
sed "s@$1@$2@" <"${CONFIG}" >"${CONFIG}.tmp" &&
mv -f "${CONFIG}.tmp" "${CONFIG}"
exec 7<&-
}
fi

comment() {
hassstr "$1"
Expand Down
29 changes: 29 additions & 0 deletions tool/flock.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,38 @@
#include <string.h>
#include <sys/file.h>

#if defined(sun) || defined(__sun)
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#endif

int main(int argc, char *argv[]) {
if (argc != 3) return 1;

#if defined(sun) || defined(__sun)
if (!strcmp(argv[1], "-x")) {
int fd;
do {
fd = open(argv[2], O_CREAT | O_RDWR | O_EXCL, 0666);
if (fd < 0) {
if (errno != EEXIST) {
return 3;
}
sleep(1);
}
} while (fd < 0);
} else if (!strcmp(argv[1], "-u")) {
if (unlink(argv[2]) < 0) {
return 3;
}
} else {
return 2;
}
#else
if (strcmp(argv[1], "-x")) return 2;
if (flock(atoi(argv[2]), LOCK_EX)) return 3;
#endif

return 0;
}