diff options
Diffstat (limited to 'missing')
-rw-r--r-- | missing/dtoa.c | 11 | ||||
-rw-r--r-- | missing/explicit_bzero.c | 5 | ||||
-rw-r--r-- | missing/flock.c | 8 | ||||
-rw-r--r-- | missing/procstat_vm.c | 34 | ||||
-rw-r--r-- | missing/setproctitle.c | 68 |
5 files changed, 92 insertions, 34 deletions
diff --git a/missing/dtoa.c b/missing/dtoa.c index b7a8302875..bce2cb22a1 100644 --- a/missing/dtoa.c +++ b/missing/dtoa.c @@ -183,7 +183,10 @@ #undef Long #undef ULong +#include <assert.h> #include <limits.h> +#include <stddef.h> +#include <stdint.h> #if (INT_MAX >> 30) && !(INT_MAX >> 31) #define Long int @@ -195,7 +198,7 @@ #error No 32bit integer #endif -#if HAVE_LONG_LONG +#if defined(HAVE_LONG_LONG) && (HAVE_LONG_LONG) #define Llong LONG_LONG #else #define NO_LONG_LONG @@ -221,12 +224,12 @@ #ifdef MALLOC extern void *MALLOC(size_t); #else -#define MALLOC xmalloc +#define MALLOC malloc #endif #ifdef FREE extern void FREE(void*); #else -#define FREE xfree +#define FREE free #endif #ifndef NO_SANITIZE #define NO_SANITIZE(x, y) y @@ -502,7 +505,7 @@ extern double rnd_prod(double, double), rnd_quot(double, double); #endif #ifndef ATOMIC_PTR_CAS -#define ATOMIC_PTR_CAS(var, old, new) ((var) = (new), (old)) +#define ATOMIC_PTR_CAS(var, old, new) ((var) = (new), (void *)(old)) #endif #ifndef LIKELY #define LIKELY(x) (x) diff --git a/missing/explicit_bzero.c b/missing/explicit_bzero.c index 1220e5f9ad..59417e158e 100644 --- a/missing/explicit_bzero.c +++ b/missing/explicit_bzero.c @@ -1,12 +1,9 @@ #ifndef __STDC_WANT_LIB_EXT1__ -#define __STDC_WANT_LIB_EXT1__ 1 +#define __STDC_WANT_LIB_EXT1__ 1 /* for memset_s() */ #endif #include "ruby/missing.h" #include <string.h> -#ifdef HAVE_MEMSET_S -# include <string.h> -#endif #ifdef _WIN32 #include <windows.h> diff --git a/missing/flock.c b/missing/flock.c index 9daf1c38cc..0b76961762 100644 --- a/missing/flock.c +++ b/missing/flock.c @@ -5,9 +5,11 @@ #elif defined __wasi__ #include <errno.h> -int flock(int fd, int operation) { - errno = EINVAL; - return -1; +int +flock(int fd, int operation) +{ + errno = EINVAL; + return -1; } #elif defined HAVE_FCNTL && defined HAVE_FCNTL_H diff --git a/missing/procstat_vm.c b/missing/procstat_vm.c index 76fd8f61ba..155ee355d1 100644 --- a/missing/procstat_vm.c +++ b/missing/procstat_vm.c @@ -6,7 +6,7 @@ # define KVME_TYPE_MGTDEVICE 8 # endif void -procstat_vm(struct procstat *procstat, struct kinfo_proc *kipp) +procstat_vm(struct procstat *procstat, struct kinfo_proc *kipp, FILE *errout) { struct kinfo_vmentry *freep, *kve; int ptrwidth; @@ -17,7 +17,7 @@ procstat_vm(struct procstat *procstat, struct kinfo_proc *kipp) #else ptrwidth = 2*sizeof(void *) + 2; #endif - fprintf(stderr, "%*s %*s %3s %4s %4s %3s %3s %4s %-2s %-s\n", + fprintf(errout, "%*s %*s %3s %4s %4s %3s %3s %4s %-2s %-s\n", ptrwidth, "START", ptrwidth, "END", "PRT", "RES", "P""RES", "REF", "SHD", "FL", "TP", "PATH"); @@ -30,20 +30,20 @@ procstat_vm(struct procstat *procstat, struct kinfo_proc *kipp) return; for (i = 0; i < cnt; i++) { kve = &freep[i]; - fprintf(stderr, "%#*jx ", ptrwidth, (uintmax_t)kve->kve_start); - fprintf(stderr, "%#*jx ", ptrwidth, (uintmax_t)kve->kve_end); - fprintf(stderr, "%s", kve->kve_protection & KVME_PROT_READ ? "r" : "-"); - fprintf(stderr, "%s", kve->kve_protection & KVME_PROT_WRITE ? "w" : "-"); - fprintf(stderr, "%s ", kve->kve_protection & KVME_PROT_EXEC ? "x" : "-"); - fprintf(stderr, "%4d ", kve->kve_resident); - fprintf(stderr, "%4d ", kve->kve_private_resident); - fprintf(stderr, "%3d ", kve->kve_ref_count); - fprintf(stderr, "%3d ", kve->kve_shadow_count); - fprintf(stderr, "%-1s", kve->kve_flags & KVME_FLAG_COW ? "C" : "-"); - fprintf(stderr, "%-1s", kve->kve_flags & KVME_FLAG_NEEDS_COPY ? "N" : + fprintf(errout, "%#*jx ", ptrwidth, (uintmax_t)kve->kve_start); + fprintf(errout, "%#*jx ", ptrwidth, (uintmax_t)kve->kve_end); + fprintf(errout, "%s", kve->kve_protection & KVME_PROT_READ ? "r" : "-"); + fprintf(errout, "%s", kve->kve_protection & KVME_PROT_WRITE ? "w" : "-"); + fprintf(errout, "%s ", kve->kve_protection & KVME_PROT_EXEC ? "x" : "-"); + fprintf(errout, "%4d ", kve->kve_resident); + fprintf(errout, "%4d ", kve->kve_private_resident); + fprintf(errout, "%3d ", kve->kve_ref_count); + fprintf(errout, "%3d ", kve->kve_shadow_count); + fprintf(errout, "%-1s", kve->kve_flags & KVME_FLAG_COW ? "C" : "-"); + fprintf(errout, "%-1s", kve->kve_flags & KVME_FLAG_NEEDS_COPY ? "N" : "-"); - fprintf(stderr, "%-1s", kve->kve_flags & KVME_FLAG_SUPER ? "S" : "-"); - fprintf(stderr, "%-1s ", kve->kve_flags & KVME_FLAG_GROWS_UP ? "U" : + fprintf(errout, "%-1s", kve->kve_flags & KVME_FLAG_SUPER ? "S" : "-"); + fprintf(errout, "%-1s ", kve->kve_flags & KVME_FLAG_GROWS_UP ? "U" : kve->kve_flags & KVME_FLAG_GROWS_DOWN ? "D" : "-"); switch (kve->kve_type) { case KVME_TYPE_NONE: @@ -78,8 +78,8 @@ procstat_vm(struct procstat *procstat, struct kinfo_proc *kipp) str = "??"; break; } - fprintf(stderr, "%-2s ", str); - fprintf(stderr, "%-s\n", kve->kve_path); + fprintf(errout, "%-2s ", str); + fprintf(errout, "%-s\n", kve->kve_path); } free(freep); } diff --git a/missing/setproctitle.c b/missing/setproctitle.c index 811829c060..5b2dfa65ce 100644 --- a/missing/setproctitle.c +++ b/missing/setproctitle.c @@ -80,10 +80,43 @@ static char **argv1_addr = NULL; #endif /* HAVE_SETPROCTITLE */ +#if defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV +# define ALLOCATE_ENVIRON 1 +#else +# define ALLOCATE_ENVIRON 0 +#endif + +#if ALLOCATE_ENVIRON +/* system_environ is the value of environ before we allocate a custom buffer. + * + * We use this to restore environ in ruby_free_proctitle. + */ +static char **system_environ = NULL; +/* orig_environ is the buffer we allocate for environ. + * + * We use this to free this buffer in ruby_free_proctitle. When we add new + * environment variables using setenv, the system may change environ to a + * different buffer and will not free the original buffer, so we need to hold + * onto this so we can free it in ruby_free_proctitle. + * + * We must not free any of the contents because it may change if the system + * updates existing environment variables. + */ +static char **orig_environ = NULL; +/* alloc_environ is a copy of orig_environ. + * + * We use this to free all the original string copies that were in orig_environ. + * Since environ could be changed to point to strings allocated by the system + * if environment variables are updated, so we need this to point to the + * original strings. + */ +static char **alloc_environ = NULL; +#endif + void compat_init_setproctitle(int argc, char *argv[]) { -#if defined(SPT_TYPE) && SPT_TYPE == SPT_REUSEARGV +#if ALLOCATE_ENVIRON extern char **environ; char *lastargv = NULL; char *lastenvp = NULL; @@ -100,9 +133,13 @@ compat_init_setproctitle(int argc, char *argv[]) return; /* Fail if we can't allocate room for the new environment */ - for (i = 0; envp[i] != NULL; i++) - ; - if ((environ = calloc(i + 1, sizeof(*environ))) == NULL) { + for (i = 0; envp[i] != NULL; i++); + + system_environ = environ; + + alloc_environ = xcalloc(i + 1, sizeof(*environ)); + orig_environ = environ = xcalloc(i + 1, sizeof(*environ)); + if (environ == NULL) { environ = envp; /* put it back */ return; } @@ -129,12 +166,31 @@ compat_init_setproctitle(int argc, char *argv[]) argv_env_len = lastenvp - argv[0]; for (i = 0; envp[i] != NULL; i++) - environ[i] = ruby_strdup(envp[i]); - environ[i] = NULL; + alloc_environ[i] = environ[i] = ruby_strdup(envp[i]); + alloc_environ[i] = environ[i] = NULL; #endif /* SPT_REUSEARGV */ } +void +ruby_free_proctitle(void) +{ +#if ALLOCATE_ENVIRON + extern char **environ; + + if (!orig_environ) return; /* environ is allocated by OS */ + + for (int i = 0; alloc_environ[i] != NULL; i++) { + xfree(alloc_environ[i]); + } + xfree(alloc_environ); + xfree(orig_environ); + + environ = system_environ; +#endif +} + #ifndef HAVE_SETPROCTITLE + void setproctitle(const char *fmt, ...) { |