Skip to content

Commit 3cccf0a

Browse files
committed
Don’t assume openat
Use openat only on platforms with O_PATH. This ports to OS X 10.9 and earlier. Problem reported by Keith David Bershatsky in: https://lists.gnu.org/r/emacs-devel/2022-04/msg00805.html * lib-src/emacsclient.c (local_sockname): Use open, not openat. * src/sysdep.c (sys_openat): New static function, which uses openat only if O_PATH is defined. (emacs_openat): Use it instead of openat. (emacs_openat_noquit): Remove. (emacs_open_noquit): Reimplement as per the old emacs_openat_noquit, but use plain 'open'.
1 parent 4641bc1 commit 3cccf0a

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

lib-src/emacsclient.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,8 +1412,7 @@ local_sockname (int s, char sockname[socknamesize], int tmpdirlen,
14121412
char *emacsdirend = sockname + tmpdirlen + suffixlen -
14131413
strlen(server_name) - 1;
14141414
*emacsdirend = '\0';
1415-
int dir = openat (AT_FDCWD, sockname,
1416-
O_PATH | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
1415+
int dir = open (sockname, O_PATH | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
14171416
*emacsdirend = '/';
14181417
if (dir < 0)
14191418
return errno;

src/sysdep.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,6 +2302,20 @@ emacs_fstatat (int dirfd, char const *filename, void *st, int flags)
23022302
return r;
23032303
}
23042304

2305+
static int
2306+
sys_openat (int dirfd, char const *file, int oflags, int mode)
2307+
{
2308+
#ifdef O_PATH
2309+
return openat (dirfd, file, oflags, mode);
2310+
#else
2311+
/* On platforms without O_PATH, emacs_openat's callers arrange for
2312+
DIRFD to be AT_FDCWD, so it should be safe to just call 'open'.
2313+
This ports to old platforms like OS X 10.9 that lack openat. */
2314+
eassert (dirfd == AT_FDCWD);
2315+
return open (file, oflags, mode);
2316+
#endif
2317+
}
2318+
23052319
/* Assuming the directory DIRFD, open FILE for Emacs use,
23062320
using open flags OFLAGS and mode MODE.
23072321
Use binary I/O on systems that care about text vs binary I/O.
@@ -2317,7 +2331,7 @@ emacs_openat (int dirfd, char const *file, int oflags, int mode)
23172331
if (! (oflags & O_TEXT))
23182332
oflags |= O_BINARY;
23192333
oflags |= O_CLOEXEC;
2320-
while ((fd = openat (dirfd, file, oflags, mode)) < 0 && errno == EINTR)
2334+
while ((fd = sys_openat (dirfd, file, oflags, mode)) < 0 && errno == EINTR)
23212335
maybe_quit ();
23222336
return fd;
23232337
}
@@ -2330,26 +2344,19 @@ emacs_open (char const *file, int oflags, int mode)
23302344

23312345
/* Same as above, but doesn't allow the user to quit. */
23322346

2333-
static int
2334-
emacs_openat_noquit (int dirfd, const char *file, int oflags,
2335-
int mode)
2347+
int
2348+
emacs_open_noquit (char const *file, int oflags, int mode)
23362349
{
23372350
int fd;
23382351
if (! (oflags & O_TEXT))
23392352
oflags |= O_BINARY;
23402353
oflags |= O_CLOEXEC;
23412354
do
2342-
fd = openat (dirfd, file, oflags, mode);
2355+
fd = open (file, oflags, mode);
23432356
while (fd < 0 && errno == EINTR);
23442357
return fd;
23452358
}
23462359

2347-
int
2348-
emacs_open_noquit (char const *file, int oflags, int mode)
2349-
{
2350-
return emacs_openat_noquit (AT_FDCWD, file, oflags, mode);
2351-
}
2352-
23532360
/* Open FILE as a stream for Emacs use, with mode MODE.
23542361
Act like emacs_open with respect to threads, signals, and quits. */
23552362

0 commit comments

Comments
 (0)