Skip to content

Commit 3e2a2d7

Browse files
fcntl(..., F_SETFL, ...) return value only signals error. (#141)
The return value of `fcntl` when passed `F_SETFL` isn't guaranteed to return the flags passed in as its return value. It will be `-1` in the event of an error, but no other value is to be relied upon. Fortunately, we weren't relying upon the value apart from occasionally checking for an error. This is as it is specified in https://pubs.opengroup.org/onlinepubs/7908799/xsh/fcntl.html Closes Interlisp/medley#87.
1 parent 01926fc commit 3e2a2d7

File tree

8 files changed

+21
-25
lines changed

8 files changed

+21
-25
lines changed

src/Cldeetr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ int main(int argc, char *argv[])
142142
strcpy(Ename, if_data.ifc_req[0].ifr_name);
143143

144144
flags = fcntl(ether_fd, F_GETFL, 0);
145-
flags = fcntl(ether_fd, F_SETFL, flags | FASYNC | FNDELAY);
145+
fcntl(ether_fd, F_SETFL, flags | FASYNC | FNDELAY);
146146

147147
#ifdef DEBUG
148148
printf("init_ether: **** Ethernet starts ****\n");

src/chardev.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ LispPTR CHAR_openfile(LispPTR *args)
7878
#ifndef DOS
7979
register int fd; /* return value of open system call. */
8080
register int flags; /* open system call's argument */
81-
register int rval;
8281
struct stat statbuf;
8382
char pathname[MAXPATHLEN];
8483

@@ -104,9 +103,7 @@ LispPTR CHAR_openfile(LispPTR *args)
104103
}
105104
/* Prevent I/O requests from blocking -- make them error */
106105
/* if no char is available, or there's no room in pipe. */
107-
rval = fcntl(fd, F_GETFL, 0);
108-
rval |= FNDELAY;
109-
rval = fcntl(fd, F_SETFL, rval);
106+
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | FNDELAY);
110107

111108
return (GetSmallp(fd));
112109
#endif /* DOS */

src/ether.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ void init_ether() {
823823
}
824824

825825
flags = fcntl(ether_fd, F_GETFL, 0);
826-
flags = fcntl(ether_fd, F_SETFL, flags | O_NDELAY);
826+
fcntl(ether_fd, F_SETFL, flags | O_NDELAY);
827827

828828
} else {
829829
I_Give_Up:

src/ldeether.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ int main(int argc, char *argv[]) {
130130
}
131131

132132
flags = fcntl(ether_fd, F_GETFL, 0);
133-
flags = fcntl(ether_fd, F_SETFL, flags | O_NDELAY);
133+
fcntl(ether_fd, F_SETFL, flags | O_NDELAY);
134134

135135
#else
136136
/* N O T D L P I C O D E */
@@ -211,7 +211,7 @@ int main(int argc, char *argv[]) {
211211
strcpy(Ename, if_data.ifc_req[0].ifr_name);
212212

213213
flags = fcntl(ether_fd, F_GETFL, 0);
214-
flags = fcntl(ether_fd, F_SETFL, flags | FASYNC | FNDELAY);
214+
fcntl(ether_fd, F_SETFL, flags | FASYNC | FNDELAY);
215215

216216
#endif /* USE_DLPI */
217217
#ifdef DEBUG

src/oldeether.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ int main(int argc, char *argv[])
140140
strcpy(Ename, if_data.ifc_req[0].ifr_name);
141141

142142
flags = fcntl(ether_fd, F_GETFL, 0);
143-
flags = fcntl(ether_fd, F_SETFL, flags | FASYNC | FNDELAY);
143+
fcntl(ether_fd, F_SETFL, flags | FASYNC | FNDELAY);
144144

145145
#ifdef DEBUG
146146
printf("init_ether: **** Ethernet starts ****\n");

src/osmsg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void mess_init() {
149149
#ifdef LOGINT
150150
LogFileFd = cons_pty; /* was kept as an fd_set, but doesn't need to be */
151151
flags = fcntl(cons_pty, F_GETFL, 0);
152-
flags = fcntl(cons_pty, F_SETFL, (flags | FASYNC | FNDELAY));
152+
fcntl(cons_pty, F_SETFL, (flags | FASYNC | FNDELAY));
153153
if (fcntl(cons_pty, F_SETOWN, getpid()) == -1) {
154154
#ifdef DEBUG
155155
perror("fcntl F_SETOWN of log PTY");

src/unixcomm.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ int FindAvailablePty(char *Master, char *Slave) {
338338
flags = fcntl(res, F_GETFL, 0);
339339
flags |= FNDELAY;
340340

341-
flags = fcntl(res, F_SETFL, flags);
341+
fcntl(res, F_SETFL, flags);
342342
return (res);
343343
}
344344
#ifndef FULLSLAVENAME
@@ -452,8 +452,7 @@ LispPTR Unix_handlecomm(LispPTR *args) {
452452
}
453453
res = fcntl(PipeFD, F_GETFL, 0);
454454
res |= FNDELAY;
455-
res = fcntl(PipeFD, F_SETFL, res);
456-
if (res < 0) {
455+
if (fcntl(PipeFD, F_SETFL, res) == -1) {
457456
perror("setting up fifo to nodelay");
458457
return (NIL);
459458
}
@@ -615,7 +614,7 @@ LispPTR Unix_handlecomm(LispPTR *args) {
615614
case 11: /* Fork PTY process */
616615
{
617616
char MasterFD[20], SlavePTY[32];
618-
int Master, res, slot;
617+
int Master, flags, slot;
619618
unsigned short len;
620619

621620
Master = FindAvailablePty(MasterFD, SlavePTY);
@@ -651,9 +650,9 @@ LispPTR Unix_handlecomm(LispPTR *args) {
651650
DBPRINT(("Pipe/fork result = %d.\n", d[3]));
652651
if (d[3] == 1) {
653652
/* Set up the IO not to block */
654-
res = fcntl(Master, F_GETFL, 0);
655-
res |= FNDELAY;
656-
res = fcntl(Master, F_SETFL, res);
653+
flags = fcntl(Master, F_GETFL, 0);
654+
flags |= FNDELAY;
655+
fcntl(Master, F_SETFL, flags);
657656

658657
UJ[slot].type = UJSHELL; /* so we can find them */
659658
UJ[slot].PID = (d[1] << 8) | d[2];
@@ -833,7 +832,7 @@ LispPTR Unix_handlecomm(LispPTR *args) {
833832
case 12: /* create Unix socket */
834833

835834
{
836-
int res, sockFD;
835+
int flags, sockFD;
837836
struct sockaddr_un sock;
838837

839838
/* First open the socket */
@@ -862,9 +861,9 @@ LispPTR Unix_handlecomm(LispPTR *args) {
862861
DBPRINT(("Socket %d bound to name %s.\n", sockFD, shcom));
863862
if (listen(sockFD, 1) < 0) perror("Listen");
864863
/* Set up the IO not to block */
865-
res = fcntl(sockFD, F_GETFL, 0);
866-
res |= FNDELAY;
867-
res = fcntl(sockFD, F_SETFL, res);
864+
flags = fcntl(sockFD, F_GETFL, 0);
865+
flags |= FNDELAY;
866+
fcntl(sockFD, F_SETFL, flags);
868867

869868
/* things seem sane, fill out the rest of the UJ slot and return */
870869
UJ[sockFD].status = -1;

src/unixfork.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ of the packet received except:
225225
int fork_Unix() {
226226
int LispToUnix[2], /* Incoming pipe from LISP */
227227
UnixToLisp[2], /* Outgoing pipe to LISP */
228-
UnixPID, LispPipeIn, LispPipeOut, res, slot;
228+
UnixPID, LispPipeIn, LispPipeOut, flags, slot;
229229
pid_t pid;
230230

231231
char IOBuf[4];
@@ -292,9 +292,9 @@ int fork_Unix() {
292292
close(LispToUnix[1]);
293293
close(UnixToLisp[0]);
294294

295-
res = fcntl(LispPipeIn, F_GETFL, 0);
296-
res &= (65535 - FNDELAY);
297-
res = fcntl(LispPipeIn, F_SETFL, res);
295+
flags = fcntl(LispPipeIn, F_GETFL, 0);
296+
flags &= (65535 - FNDELAY);
297+
fcntl(LispPipeIn, F_SETFL, flags);
298298

299299
while (1) {
300300
ssize_t len;

0 commit comments

Comments
 (0)