GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
+the Free Software Foundation, either version 3 of the License, or (at
+your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
#include <config.h>
-/* If HYBRID_GET_CURRENT_DIR_NAME is defined in conf_post.h, then we
- need the following before including unistd.h, in order to pick up
- the right prototype for gget_current_dir_name. */
-#ifdef HYBRID_GET_CURRENT_DIR_NAME
-#undef get_current_dir_name
-#define get_current_dir_name gget_current_dir_name
-#endif
-
#include <execinfo.h>
#include "sysstdio.h"
#ifdef HAVE_PWD_H
#include <utimens.h>
#include "lisp.h"
+#include "sheap.h"
#include "sysselect.h"
#include "blockinput.h"
#include "process.h"
#include "cm.h"
+#include "gnutls.h"
+/* MS-Windows loads GnuTLS at run time, if available; we don't want to
+ do that during startup just to call gnutls_rnd. */
+#if 0x020c00 <= GNUTLS_VERSION_NUMBER && !defined WINDOWSNT
+# include <gnutls/crypto.h>
+#else
+# define emacs_gnutls_global_init() Qnil
+# define gnutls_rnd(level, data, len) (-1)
+#endif
+
#ifdef WINDOWSNT
#include <direct.h>
/* In process.h which conflicts with the local copy. */
1800, 2400, 4800, 9600, 19200, 38400
};
-#if !defined HAVE_GET_CURRENT_DIR_NAME || defined BROKEN_GET_CURRENT_DIR_NAME \
- || (defined HYBRID_GET_CURRENT_DIR_NAME)
-/* Return the current working directory. Returns NULL on errors.
- Any other returned value must be freed with free. This is used
- only when get_current_dir_name is not defined on the system. */
+/* If FD is not already open, arrange for it to be open with FLAGS. */
+static void
+force_open (int fd, int flags)
+{
+ if (dup2 (fd, fd) < 0 && errno == EBADF)
+ {
+ int n = open (NULL_DEVICE, flags);
+ if (n < 0 || (fd != n && (dup2 (n, fd) < 0 || emacs_close (n) != 0)))
+ {
+ emacs_perror (NULL_DEVICE);
+ exit (EXIT_FAILURE);
+ }
+ }
+}
+
+/* Make sure stdin, stdout, and stderr are open to something, so that
+ their file descriptors are not hijacked by later system calls. */
+void
+init_standard_fds (void)
+{
+ /* Open stdin for *writing*, and stdout and stderr for *reading*.
+ That way, any attempt to do normal I/O will result in an error,
+ just as if the files were closed, and the file descriptors will
+ not be reused by later opens. */
+ force_open (STDIN_FILENO, O_WRONLY);
+ force_open (STDOUT_FILENO, O_RDONLY);
+ force_open (STDERR_FILENO, O_RDONLY);
+}
+
+/* Return the current working directory. The result should be freed
+ with 'free'. Return NULL on errors. */
char *
-get_current_dir_name (void)
+emacs_get_current_dir_name (void)
{
+# if HAVE_GET_CURRENT_DIR_NAME && !BROKEN_GET_CURRENT_DIR_NAME
+# ifdef HYBRID_MALLOC
+ bool use_libc = bss_sbrk_did_unexec;
+# else
+ bool use_libc = true;
+# endif
+ if (use_libc)
+ return get_current_dir_name ();
+# endif
+
char *buf;
char *pwd = getenv ("PWD");
struct stat dotstat, pwdstat;
}
return buf;
}
-#endif
\f
/* Discard pending input on all input descriptors. */
void
init_system_name (void)
{
+ if (!build_details)
+ {
+ /* Set system-name to nil so that the build is deterministic. */
+ Vsystem_name = Qnil;
+ return;
+ }
char *hostname_alloc = NULL;
char *hostname;
#ifndef HAVE_GETHOSTNAME
static bool
stack_overflow (siginfo_t *siginfo)
{
+ if (!attempt_stack_overflow_recovery)
+ return false;
+
/* In theory, a more-accurate heuristic can be obtained by using
GNU/Linux pthread_getattr_np along with POSIX pthread_attr_getstack
and pthread_attr_getguardsize to find the location and size of the
init_random (void)
{
random_seed v;
- bool success = false;
-#ifndef WINDOWSNT
- int fd = emacs_open ("/dev/urandom", O_RDONLY | O_BINARY, 0);
- if (fd >= 0)
+ if (! (EQ (emacs_gnutls_global_init (), Qt)
+ && gnutls_rnd (GNUTLS_RND_NONCE, &v, sizeof v) == 0))
{
- success = emacs_read (fd, &v, sizeof v) == sizeof v;
- emacs_close (fd);
- }
+ bool success = false;
+#ifndef WINDOWSNT
+ int fd = emacs_open ("/dev/urandom", O_RDONLY, 0);
+ if (0 <= fd)
+ {
+ success = emacs_read (fd, &v, sizeof v) == sizeof v;
+ emacs_close (fd);
+ }
#else
- success = w32_init_random (&v, sizeof v) == 0;
+ success = w32_init_random (&v, sizeof v) == 0;
#endif
- if (! success)
- {
- /* Fall back to current time value + PID. */
- struct timespec t = current_timespec ();
- v = getpid () ^ t.tv_sec ^ t.tv_nsec;
+ if (! success)
+ {
+ /* Fall back to current time value + PID. */
+ struct timespec t = current_timespec ();
+ v = getpid () ^ t.tv_sec ^ t.tv_nsec;
+ }
}
set_random_seed (v);
}
xfree (b);
}
- if (nbytes > INT_MAX)
+ if (INT_MAX < nbytes)
{
#ifdef EOVERFLOW
errno = EOVERFLOW;
{
emacs_write (STDERR_FILENO, "\nBacktrace:\n", 12);
backtrace_symbols_fd (buffer, npointers, STDERR_FILENO);
- if (npointers > bounded_limit)
+ if (bounded_limit < npointers)
emacs_write (STDERR_FILENO, "...\n", 4);
}
}
oflags |= O_CLOEXEC;
while ((fd = open (file, oflags, mode)) < 0 && errno == EINTR)
QUIT;
- if (! O_CLOEXEC && fd >= 0)
+ if (! O_CLOEXEC && 0 <= fd)
fcntl (fd, F_SETFD, FD_CLOEXEC);
return fd;
}
switch (*m++)
{
case '+': omode = O_RDWR; break;
- case 'b': bflag = O_BINARY; break;
case 't': bflag = O_TEXT; break;
default: /* Ignore. */ break;
}
unsigned long long frac = tval % hz;
int ns;
- if (s > TYPE_MAXIMUM (time_t))
+ if (TYPE_MAXIMUM (time_t) < s)
time_overflow ();
- if (ULLONG_MAX / TIMESPEC_RESOLUTION >= LONG_MAX - 1
+ if (LONG_MAX - 1 <= ULLONG_MAX / TIMESPEC_RESOLUTION
|| frac <= ULLONG_MAX / TIMESPEC_RESOLUTION)
ns = frac * TIMESPEC_RESOLUTION / hz;
else
struct timespec tnow, tstart, tboot, telapsed, us_time;
double pcpu, pmem;
Lisp_Object attrs = Qnil;
- Lisp_Object cmd_str, decoded_cmd;
+ Lisp_Object decoded_cmd;
ptrdiff_t count;
CHECK_NUMBER_OR_FLOAT (pid);
record_unwind_protect_int (close_file_unwind, fd);
nread = emacs_read (fd, procbuf, sizeof procbuf - 1);
}
- if (nread > 0)
+ if (0 < nread)
{
procbuf[nread] = '\0';
p = procbuf;
else
q = NULL;
/* Command name is encoded in locale-coding-system; decode it. */
- cmd_str = make_unibyte_string (cmd, cmdsize);
+ AUTO_STRING_WITH_LEN (cmd_str, cmd, cmdsize);
decoded_cmd = code_convert_string_norecord (cmd_str,
Vlocale_coding_system, 0);
attrs = Fcons (Fcons (Qcomm, decoded_cmd), attrs);
if (nread)
{
/* We don't want trailing null characters. */
- for (p = cmdline + nread; p > cmdline && !p[-1]; p--)
+ for (p = cmdline + nread; cmdline < p && !p[-1]; p--)
continue;
/* Escape-quote whitespace and backslashes. */
q = cmdline + cmdline_size;
- while (p > cmdline)
+ while (cmdline < p)
{
char c = *--p;
*--q = c ? c : ' ';
sprintf (cmdline, "[%.*s]", cmdsize, cmd);
}
/* Command line is encoded in locale-coding-system; decode it. */
- cmd_str = make_unibyte_string (q, nread);
+ AUTO_STRING_WITH_LEN (cmd_str, q, nread);
decoded_cmd = code_convert_string_norecord (cmd_str,
Vlocale_coding_system, 0);
unbind_to (count, Qnil);
make_float (100.0 / 0x8000 * pinfo.pr_pctmem)),
attrs);
- decoded_cmd = (code_convert_string_norecord
- (build_unibyte_string (pinfo.pr_fname),
- Vlocale_coding_system, 0));
+ AUTO_STRING (fname, pinfo.pr_fname);
+ decoded_cmd = code_convert_string_norecord (fname,
+ Vlocale_coding_system, 0);
attrs = Fcons (Fcons (Qcomm, decoded_cmd), attrs);
- decoded_cmd = (code_convert_string_norecord
- (build_unibyte_string (pinfo.pr_psargs),
- Vlocale_coding_system, 0));
+ AUTO_STRING (psargs, pinfo.pr_psargs);
+ decoded_cmd = code_convert_string_norecord (psargs,
+ Vlocale_coding_system, 0);
attrs = Fcons (Fcons (Qargs, decoded_cmd), attrs);
}
unbind_to (count, Qnil);
if (gr)
attrs = Fcons (Fcons (Qgroup, build_string (gr->gr_name)), attrs);
- decoded_comm = (code_convert_string_norecord
- (build_unibyte_string (proc.ki_comm),
- Vlocale_coding_system, 0));
+ AUTO_STRING (comm, proc.ki_comm);
+ decoded_comm = code_convert_string_norecord (comm, Vlocale_coding_system, 0);
attrs = Fcons (Fcons (Qcomm, decoded_comm), attrs);
{
args[i] = ' ';
}
- decoded_comm =
- (code_convert_string_norecord
- (build_unibyte_string (args),
- Vlocale_coding_system, 0));
+ AUTO_STRING (comm, args);
+ decoded_comm = code_convert_string_norecord (comm,
+ Vlocale_coding_system, 0);
attrs = Fcons (Fcons (Qargs, decoded_comm), attrs);
}
return attrs;
}
+#elif defined DARWIN_OS
+
+static struct timespec
+timeval_to_timespec (struct timeval t)
+{
+ return make_timespec (t.tv_sec, t.tv_usec * 1000);
+}
+
+static Lisp_Object
+make_lisp_timeval (struct timeval t)
+{
+ return make_lisp_time (timeval_to_timespec (t));
+}
+
+Lisp_Object
+system_process_attributes (Lisp_Object pid)
+{
+ int proc_id;
+ int pagesize = getpagesize ();
+ unsigned long npages;
+ int fscale;
+ struct passwd *pw;
+ struct group *gr;
+ char *ttyname;
+ size_t len;
+ char args[MAXPATHLEN];
+ struct timeval starttime;
+ struct timespec t, now;
+ struct rusage *rusage;
+ dev_t tdev;
+ uid_t uid;
+ gid_t gid;
+
+ int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID};
+ struct kinfo_proc proc;
+ size_t proclen = sizeof proc;
+
+ Lisp_Object attrs = Qnil;
+ Lisp_Object decoded_comm;
+
+ CHECK_NUMBER_OR_FLOAT (pid);
+ CONS_TO_INTEGER (pid, int, proc_id);
+ mib[3] = proc_id;
+
+ if (sysctl (mib, 4, &proc, &proclen, NULL, 0) != 0)
+ return attrs;
+
+ uid = proc.kp_eproc.e_ucred.cr_uid;
+ attrs = Fcons (Fcons (Qeuid, make_fixnum_or_float (uid)), attrs);
+
+ block_input ();
+ pw = getpwuid (uid);
+ unblock_input ();
+ if (pw)
+ attrs = Fcons (Fcons (Quser, build_string (pw->pw_name)), attrs);
+
+ gid = proc.kp_eproc.e_pcred.p_svgid;
+ attrs = Fcons (Fcons (Qegid, make_fixnum_or_float (gid)), attrs);
+
+ block_input ();
+ gr = getgrgid (gid);
+ unblock_input ();
+ if (gr)
+ attrs = Fcons (Fcons (Qgroup, build_string (gr->gr_name)), attrs);
+
+ decoded_comm = (code_convert_string_norecord
+ (build_unibyte_string (proc.kp_proc.p_comm),
+ Vlocale_coding_system, 0));
+
+ attrs = Fcons (Fcons (Qcomm, decoded_comm), attrs);
+ {
+ char state[2] = {'\0', '\0'};
+ switch (proc.kp_proc.p_stat)
+ {
+ case SRUN:
+ state[0] = 'R';
+ break;
+
+ case SSLEEP:
+ state[0] = 'S';
+ break;
+
+ case SZOMB:
+ state[0] = 'Z';
+ break;
+
+ case SSTOP:
+ state[0] = 'T';
+ break;
+
+ case SIDL:
+ state[0] = 'I';
+ break;
+ }
+ attrs = Fcons (Fcons (Qstate, build_string (state)), attrs);
+ }
+
+ attrs = Fcons (Fcons (Qppid, make_fixnum_or_float (proc.kp_eproc.e_ppid)),
+ attrs);
+ attrs = Fcons (Fcons (Qpgrp, make_fixnum_or_float (proc.kp_eproc.e_pgid)),
+ attrs);
+
+ tdev = proc.kp_eproc.e_tdev;
+ block_input ();
+ ttyname = tdev == NODEV ? NULL : devname (tdev, S_IFCHR);
+ unblock_input ();
+ if (ttyname)
+ attrs = Fcons (Fcons (Qtty, build_string (ttyname)), attrs);
+
+ attrs = Fcons (Fcons (Qtpgid, make_fixnum_or_float (proc.kp_eproc.e_tpgid)),
+ attrs);
+
+ rusage = proc.kp_proc.p_ru;
+ if (rusage)
+ {
+ attrs = Fcons (Fcons (Qminflt, make_fixnum_or_float (rusage->ru_minflt)),
+ attrs);
+ attrs = Fcons (Fcons (Qmajflt, make_fixnum_or_float (rusage->ru_majflt)),
+ attrs);
+
+ attrs = Fcons (Fcons (Qutime, make_lisp_timeval (rusage->ru_utime)),
+ attrs);
+ attrs = Fcons (Fcons (Qstime, make_lisp_timeval (rusage->ru_stime)),
+ attrs);
+ t = timespec_add (timeval_to_timespec (rusage->ru_utime),
+ timeval_to_timespec (rusage->ru_stime));
+ attrs = Fcons (Fcons (Qtime, make_lisp_time (t)), attrs);
+ }
+
+ starttime = proc.kp_proc.p_starttime;
+ attrs = Fcons (Fcons (Qnice, make_number (proc.kp_proc.p_nice)), attrs);
+ attrs = Fcons (Fcons (Qstart, make_lisp_timeval (starttime)), attrs);
+
+ now = current_timespec ();
+ t = timespec_sub (now, timeval_to_timespec (starttime));
+ attrs = Fcons (Fcons (Qetime, make_lisp_time (t)), attrs);
+
+ return attrs;
+}
+
/* The WINDOWSNT implementation is in w32.c.
The MSDOS implementation is in dosfns.c. */
#elif !defined (WINDOWSNT) && !defined (MSDOS)