2013-07-05 Paul Eggert <eggert@cs.ucla.edu>
+ Use emacs_open more consistently when opening files.
+ This handles EINTR more consistently now, and makes it easier
+ to introduce other uniform changes to file descriptor handling.
+ * src/systdio.h: New file.
+ * src/buffer.c (mmap_init):
+ * cygw32.c (chdir_to_default_directory):
+ * dispnew.c (Fopen_termscript):
+ * emacs.c (Fdaemon_initialized):
+ * fileio.c (Fdo_auto_save):
+ * image.c (slurp_file, png_load_body, jpeg_load_body):
+ * keyboard.c (Fopen_dribble_file):
+ * lread.c (Fload):
+ * print.c (Fredirect_debugging_output):
+ * sysdep.c (get_up_time, procfs_ttyname, procfs_get_total_memory):
+ * termcap.c (tgetent):
+ * unexaix.c, unexcoff.c (unexec, adjust_lnnoptrs):
+ * unexcw.c, unexelf.c, unexhp9k800.c, unexmacosx.c (unexec):
+ * w32term.c (w32_initialize) [CYGWIN]:
+ * xfaces.c (Fx_load_color_file):
+ Use emacs_open instead of plain open, and emacs_fopen instead of
+ plain fopen.
+ * dispnew.c, fileio.c, image.c, keyboard.c, lread.c, print.c, sysdep.c:
+ * xfaces.c: Include sysstdio.h rather than stdio.h, for emacs_fopen.
+ * callproc.c (default_output_mode): New constant.
+ (Fcall_process): Use it to call emacs_open instead of plain creat.
+ * dispnew.c (Fopen_termscript): Fix minor race in opening termscript.
+ * sysdep.c (emacs_open): Add commentary and don't call file name "path".
+ (emacs_fopen): New function.
+ * unexaix.c, unexcoff.c, unexelf.c, unexhp9k800.c, unexmacosx.c:
+ Include <lisp.h>, for emacs_open.
+ * unexelf.c (fatal): Remove decl; not needed with <lisp.h> included.
+
Remove duplicate #include directives.
* alloc.c [GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES]:
* xfaces.c:
if (mmap_fd <= 0)
{
/* No anonymous mmap -- we need the file descriptor. */
- mmap_fd = open ("/dev/zero", O_RDONLY);
+ mmap_fd = emacs_open ("/dev/zero", O_RDONLY, 0);
if (mmap_fd == -1)
fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
}
return Qnil;
}
+#ifdef DOS_NT
+static mode_t const default_output_mode = S_IREAD | S_IWRITE;
+#else
+static mode_t const default_output_mode = 0666;
+#endif
+
DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
doc: /* Call PROGRAM synchronously in separate process.
The remaining arguments are optional.
if (STRINGP (output_file))
{
-#ifdef DOS_NT
fd_output = emacs_open (SSDATA (output_file),
- O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
- S_IREAD | S_IWRITE);
-#else /* not DOS_NT */
- fd_output = creat (SSDATA (output_file), 0666);
-#endif /* not DOS_NT */
+ O_WRONLY | O_CREAT | O_TRUNC | O_TEXT,
+ default_output_mode);
if (fd_output < 0)
{
output_file = DECODE_FILE (output_file);
strcat (tempfile, "/");
strcat (tempfile, "detmp.XXX");
mktemp (tempfile);
- outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
+ outfilefd = emacs_open (tempfile, O_WRONLY | O_CREAT | O_TRUNC,
+ S_IREAD | S_IWRITE);
if (outfilefd < 0) {
emacs_close (filefd);
report_file_error ("Opening process output file",
if (NILP (error_file))
fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0);
else if (STRINGP (error_file))
- {
-#ifdef DOS_NT
- fd_error = emacs_open (SSDATA (error_file),
- O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
- S_IREAD | S_IWRITE);
-#else /* not DOS_NT */
- fd_error = creat (SSDATA (error_file), 0666);
-#endif /* not DOS_NT */
- }
+ fd_error = emacs_open (SSDATA (error_file),
+ O_WRONLY | O_CREAT | O_TRUNC | O_TEXT,
+ default_output_mode);
if (fd_error < 0)
{
chdir_to_default_directory ()
{
Lisp_Object new_cwd;
- int old_cwd_fd = open (".", O_RDONLY | O_DIRECTORY);
+ int old_cwd_fd = emacs_open (".", O_RDONLY | O_DIRECTORY, 0);
if (old_cwd_fd == -1)
error ("could not open current directory: %s", strerror (errno));
#define DISPEXTERN_INLINE EXTERN_INLINE
-#include <stdio.h>
+#include "sysstdio.h"
#include <unistd.h>
#include "lisp.h"
tty = CURTTY ();
if (tty->termscript != 0)
- {
- block_input ();
- fclose (tty->termscript);
- unblock_input ();
- }
- tty->termscript = 0;
+ {
+ block_input ();
+ fclose (tty->termscript);
+ tty->termscript = 0;
+ unblock_input ();
+ }
if (! NILP (file))
{
file = Fexpand_file_name (file, Qnil);
- tty->termscript = fopen (SSDATA (file), "w");
+ tty->termscript = emacs_fopen (SSDATA (file), "w");
if (tty->termscript == 0)
report_file_error ("Opening termscript", Fcons (file, Qnil));
}
error ("This function can only be called after loading the init files");
/* Get rid of stdin, stdout and stderr. */
- nfd = open ("/dev/null", O_RDWR);
+ nfd = emacs_open ("/dev/null", O_RDWR, 0);
err |= nfd < 0;
err |= dup2 (nfd, 0) < 0;
err |= dup2 (nfd, 1) < 0;
#include <config.h>
#include <limits.h>
#include <fcntl.h>
-#include <stdio.h>
+#include "sysstdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
UNGCPRO;
}
- stream = fopen (SSDATA (listfile), "w");
+ stream = emacs_fopen (SSDATA (listfile), "w");
}
record_unwind_protect (do_auto_save_unwind,
along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
-#include <stdio.h>
+#include "sysstdio.h"
#include <unistd.h>
#ifdef HAVE_PNG
static unsigned char *
slurp_file (char *file, ptrdiff_t *size)
{
- FILE *fp = fopen (file, "rb");
+ FILE *fp = emacs_fopen (file, "rb");
unsigned char *buf = NULL;
struct stat st;
}
/* Open the image file. */
- fp = fopen (SSDATA (file), "rb");
+ fp = emacs_fopen (SSDATA (file), "rb");
if (!fp)
{
image_error ("Cannot open image file `%s'", file, Qnil);
return 0;
}
- fp = fopen (SSDATA (file), "rb");
+ fp = emacs_fopen (SSDATA (file), "rb");
if (fp == NULL)
{
image_error ("Cannot open `%s'", file, Qnil);
#define BLOCKINPUT_INLINE EXTERN_INLINE
#define KEYBOARD_INLINE EXTERN_INLINE
-#include <stdio.h>
+#include "sysstdio.h"
#include "lisp.h"
#include "termchar.h"
if (!NILP (file))
{
file = Fexpand_file_name (file, Qnil);
- dribble = fopen (SSDATA (file), "w");
+ dribble = emacs_fopen (SSDATA (file), "w");
if (dribble == 0)
report_file_error ("Opening dribble", Fcons (file, Qnil));
}
#include <config.h>
-#include <stdio.h>
+#include "sysstdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
if (fd >= 0)
{
emacs_close (fd);
- stream = fopen (SSDATA (efound), fmode);
+ stream = emacs_fopen (SSDATA (efound), fmode);
}
else
stream = NULL;
#include <config.h>
-#include <stdio.h>
+#include "sysstdio.h"
#include "lisp.h"
#include "character.h"
{
file = Fexpand_file_name (file, Qnil);
initial_stderr_stream = stderr;
- stderr = fopen (SSDATA (file), NILP (append) ? "w" : "a");
+ stderr = emacs_fopen (SSDATA (file), NILP (append) ? "w" : "a");
if (stderr == NULL)
{
stderr = initial_stderr_stream;
#define SYSTIME_INLINE EXTERN_INLINE
#include <execinfo.h>
-#include <stdio.h>
+#include "sysstdio.h"
#ifdef HAVE_PWD_H
#include <pwd.h>
#include <grp.h>
}
#endif
+/* Open FILE for Emacs use, using open flags OFLAG and mode MODE.
+ Do not fail merely because the open was interrupted by a signal.
+ Allow the user to quit. */
+
int
-emacs_open (const char *path, int oflag, int mode)
+emacs_open (const char *file, int oflags, int mode)
{
- register int rtnval;
+ int fd;
+ while ((fd = open (file, oflags, mode)) < 0 && errno == EINTR)
+ QUIT;
+ return fd;
+}
- while ((rtnval = open (path, oflag, mode)) == -1
- && (errno == EINTR))
+/* Open FILE as a stream for Emacs use, with mode MODE.
+ Act like emacs_open with respect to threads, signals, and quits. */
+
+FILE *
+emacs_fopen (char const *file, char const *mode)
+{
+ FILE *fp;
+ while (! (fp = fopen (file, mode)) && errno == EINTR)
QUIT;
- return (rtnval);
+ return fp;
}
int
EMACS_TIME up = make_emacs_time (0, 0);
block_input ();
- fup = fopen ("/proc/uptime", "r");
+ fup = emacs_fopen ("/proc/uptime", "r");
if (fup)
{
char name[PATH_MAX];
block_input ();
- fdev = fopen ("/proc/tty/drivers", "r");
+ fdev = emacs_fopen ("/proc/tty/drivers", "r");
if (fdev)
{
unsigned long retval = 2 * 1024 * 1024; /* default: 2GB */
block_input ();
- fmem = fopen ("/proc/meminfo", "r");
+ fmem = emacs_fopen ("/proc/meminfo", "r");
if (fmem)
{
--- /dev/null
+#include <stdio.h>
+extern FILE *emacs_fopen (char const *, char const *);
const char *name;
int fd;
name = (const char *) ttyname (0);
- fd = open (name, O_WRONLY);
+ fd = emacs_open (name, O_WRONLY, 0);
SOME_FUNCTION (x, y, fd);
close (fd);
last_mouse_x = x;
/* Here we know we must search a file and termcap_name has its name. */
-#ifdef MSDOS
- fd = open (termcap_name, O_RDONLY|O_TEXT, 0);
-#else
- fd = open (termcap_name, O_RDONLY, 0);
-#endif
+ fd = emacs_open (termcap_name, O_RDONLY | O_TEXT, 0);
if (fd < 0)
return -1;
#include <config.h>
#include "unexec.h"
+#include "lisp.h"
#define PERROR(file) report_error (file, new)
#include <a.out.h>
{
int new = -1, a_out = -1;
- if (a_name && (a_out = open (a_name, O_RDONLY)) < 0)
+ if (a_name && (a_out = emacs_open (a_name, O_RDONLY, 0)) < 0)
{
PERROR (a_name);
}
- if ((new = creat (new_name, 0666)) < 0)
+ if ((new = emacs_open (new_name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
{
PERROR (new_name);
}
if (!lnnoptr || !f_hdr.f_symptr)
return 0;
- if ((new = open (new_name, O_RDWR)) < 0)
+ if ((new = emacs_open (new_name, O_RDWR, 0)) < 0)
{
PERROR (new_name);
return -1;
#include <config.h>
#include "unexec.h"
+#include "lisp.h"
#define PERROR(file) report_error (file, new)
#ifdef MSDOS
if ((new = writedesc) < 0)
#else
- if ((new = open (new_name, O_RDWR)) < 0)
+ if ((new = emacs_open (new_name, O_RDWR, 0)) < 0)
#endif
{
PERROR (new_name);
{
int new = -1, a_out = -1;
- if (a_name && (a_out = open (a_name, O_RDONLY)) < 0)
+ if (a_name && (a_out = emacs_open (a_name, O_RDONLY, 0)) < 0)
{
PERROR (a_name);
}
- if ((new = creat (new_name, 0666)) < 0)
+ if ((new = emacs_open (new_name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
{
PERROR (new_name);
}
#include <config.h>
#include "unexec.h"
+#include "lisp.h"
-#include <lisp.h>
#include <stdio.h>
#include <fcntl.h>
#include <a.out.h>
infile = add_exe_suffix_if_necessary (infile, infile_buffer);
outfile = add_exe_suffix_if_necessary (outfile, outfile_buffer);
- fd_in = open (infile, O_RDONLY | O_BINARY);
+ fd_in = emacs_open (infile, O_RDONLY | O_BINARY, 0);
assert (fd_in >= 0);
- fd_out = open (outfile, O_RDWR | O_TRUNC | O_CREAT | O_BINARY, 0755);
+ fd_out = emacs_open (outfile, O_RDWR | O_TRUNC | O_CREAT | O_BINARY, 0755);
assert (fd_out >= 0);
for (;;)
{
Instead we read the whole file, modify it, and write it out. */
#include <config.h>
-#include <unexec.h>
-
-extern _Noreturn void fatal (const char *, ...) ATTRIBUTE_FORMAT_PRINTF (1, 2);
+#include "unexec.h"
+#include "lisp.h"
#include <errno.h>
#include <fcntl.h>
/* Open the old file, allocate a buffer of the right size, and read
in the file contents. */
- old_file = open (old_name, O_RDONLY);
+ old_file = emacs_open (old_name, O_RDONLY, 0);
if (old_file < 0)
fatal ("Can't open %s for reading: %s", old_name, strerror (errno));
fatal ("Can't fstat (%s): %s", old_name, strerror (errno));
#if MAP_ANON == 0
- mmap_fd = open ("/dev/zero", O_RDONLY);
+ mmap_fd = emacs_open ("/dev/zero", O_RDONLY, 0);
if (mmap_fd < 0)
fatal ("Can't open /dev/zero for reading: %s", strerror (errno));
#endif
the image of the new file. Set pointers to various interesting
objects. */
- new_file = open (new_name, O_RDWR | O_CREAT, 0666);
+ new_file = emacs_open (new_name, O_RDWR | O_CREAT, 0666);
if (new_file < 0)
fatal ("Can't creat (%s): %s", new_name, strerror (errno));
\f
#include <config.h>
#include "unexec.h"
+#include "lisp.h"
#include <stdio.h>
#include <fcntl.h>
intact. NOT implemented. */
/* Open the input and output a.out files */
- old = open (old_name, O_RDONLY);
+ old = emacs_open (old_name, O_RDONLY, 0);
if (old < 0)
{ perror (old_name); exit (1); }
- new = open (new_name, O_CREAT|O_RDWR|O_TRUNC, 0777);
+ new = emacs_open (new_name, O_CREAT | O_RDWR | O_TRUNC, 0777);
if (new < 0)
{ perror (new_name); exit (1); }
#undef free
#include "unexec.h"
+#include "lisp.h"
#include <stdio.h>
#include <fcntl.h>
unexec_error ("Unexec from a dumped executable is not supported.");
pagesize = getpagesize ();
- infd = open (infile, O_RDONLY, 0);
+ infd = emacs_open (infile, O_RDONLY, 0);
if (infd < 0)
{
unexec_error ("cannot open input file `%s'", infile);
}
- outfd = open (outfile, O_WRONLY | O_TRUNC | O_CREAT, 0755);
+ outfd = emacs_open (outfile, O_WRONLY | O_TRUNC | O_CREAT, 0755);
if (outfd < 0)
{
close (infd);
}
#ifdef CYGWIN
- if ((w32_message_fd = open ("/dev/windows", O_RDWR | O_CLOEXEC)) == -1)
+ if ((w32_message_fd = emacs_open ("/dev/windows", O_RDWR, 0)) == -1)
fatal ("opening /dev/windows: %s", strerror (errno));
#endif /* CYGWIN */
used to fill in unspecified attributes of the default face. */
#include <config.h>
-#include <stdio.h>
+#include "sysstdio.h"
#include <sys/types.h>
#include <sys/stat.h>
CHECK_STRING (filename);
abspath = Fexpand_file_name (filename, Qnil);
- fp = fopen (SSDATA (abspath), "rt");
+ fp = emacs_fopen (SSDATA (abspath), "rt");
if (fp)
{
char buf[512];