Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
unix: move uv_set_process_title() to proctitle.c
Browse files Browse the repository at this point in the history
Use hijacking argv array to change process' title. It seems to be working fine
on almost every platform (at least it should not break anything as it's used in
nginx in a similar way).
  • Loading branch information
indutny authored and bnoordhuis committed Jul 13, 2012
1 parent 3726dee commit dc97d44
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 147 deletions.
4 changes: 4 additions & 0 deletions config-unix.mk
Expand Up @@ -128,6 +128,10 @@ else
RUNNER_LINKFLAGS += -pthread
endif

ifneq (FreeBSD,$(uname_S))
OBJS += src/unix/proctitle.o
endif

RUNNER_LIBS=
RUNNER_SRC=test/runner-unix.c

Expand Down
27 changes: 0 additions & 27 deletions src/unix/darwin.c
Expand Up @@ -41,8 +41,6 @@
#include <sys/sysctl.h>
#include <unistd.h> /* sysconf */

static char *process_title;

#if TARGET_OS_IPHONE
/* see: http://developer.apple.com/library/mac/#qa/qa1398/_index.html */
uint64_t uv_hrtime() {
Expand Down Expand Up @@ -138,31 +136,6 @@ void uv_loadavg(double avg[3]) {
}


char** uv_setup_args(int argc, char** argv) {
process_title = argc ? strdup(argv[0]) : NULL;
return argv;
}


uv_err_t uv_set_process_title(const char* title) {
/* TODO implement me */
return uv__new_artificial_error(UV_ENOSYS);
}


uv_err_t uv_get_process_title(char* buffer, size_t size) {
if (process_title) {
strncpy(buffer, process_title, size);
} else {
if (size > 0) {
buffer[0] = '\0';
}
}

return uv_ok_;
}


uv_err_t uv_resident_set_memory(size_t* rss) {
struct task_basic_info t_info;
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
Expand Down
72 changes: 0 additions & 72 deletions src/unix/linux/linux-core.c
Expand Up @@ -58,11 +58,6 @@

static char buf[MAXPATHLEN + 1];

static struct {
char *str;
size_t len;
} process_title;


/*
* There's probably some way to get time from Linux than gettimeofday(). What
Expand Down Expand Up @@ -112,73 +107,6 @@ uint64_t uv_get_total_memory(void) {
}


char** uv_setup_args(int argc, char** argv) {
char **new_argv;
char **new_env;
size_t size;
int envc;
char *s;
int i;

for (envc = 0; environ[envc]; envc++);

s = envc ? environ[envc - 1] : argv[argc - 1];

process_title.str = argv[0];
process_title.len = s + strlen(s) + 1 - argv[0];

size = process_title.len;
size += (argc + 1) * sizeof(char **);
size += (envc + 1) * sizeof(char **);

if ((s = (char *) malloc(size)) == NULL) {
process_title.str = NULL;
process_title.len = 0;
return argv;
}

new_argv = (char **) s;
new_env = new_argv + argc + 1;
s = (char *) (new_env + envc + 1);
memcpy(s, process_title.str, process_title.len);

for (i = 0; i < argc; i++)
new_argv[i] = s + (argv[i] - argv[0]);
new_argv[argc] = NULL;

s += environ[0] - argv[0];

for (i = 0; i < envc; i++)
new_env[i] = s + (environ[i] - environ[0]);
new_env[envc] = NULL;

environ = new_env;
return new_argv;
}


uv_err_t uv_set_process_title(const char* title) {
/* No need to terminate, last char is always '\0'. */
if (process_title.len)
strncpy(process_title.str, title, process_title.len - 1);

return uv_ok_;
}


uv_err_t uv_get_process_title(char* buffer, size_t size) {
if (process_title.str) {
strncpy(buffer, process_title.str, size);
} else {
if (size > 0) {
buffer[0] = '\0';
}
}

return uv_ok_;
}


uv_err_t uv_resident_set_memory(size_t* rss) {
FILE* f;
int itmp;
Expand Down
30 changes: 0 additions & 30 deletions src/unix/openbsd.c
Expand Up @@ -40,9 +40,6 @@
#define NANOSEC ((uint64_t) 1e9)


static char *process_title;


uint64_t uv_hrtime(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
Expand Down Expand Up @@ -137,33 +134,6 @@ uint64_t uv_get_total_memory(void) {
}


char** uv_setup_args(int argc, char** argv) {
process_title = argc ? strdup(argv[0]) : NULL;
return argv;
}


uv_err_t uv_set_process_title(const char* title) {
if (process_title) free(process_title);
process_title = strdup(title);
setproctitle(title);
return uv_ok_;
}


uv_err_t uv_get_process_title(char* buffer, size_t size) {
if (process_title) {
strncpy(buffer, process_title, size);
} else {
if (size > 0) {
buffer[0] = '\0';
}
}

return uv_ok_;
}


uv_err_t uv_resident_set_memory(size_t* rss) {
kvm_t *kd = NULL;
struct kinfo_proc *kinfo = NULL;
Expand Down
102 changes: 102 additions & 0 deletions src/unix/proctitle.c
@@ -0,0 +1,102 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#include "uv.h"
#include "internal.h"

#include <stdlib.h>
#include <string.h>
#include <assert.h>

/* NOTE: FreeBSD is using it's own implementation of those functions */

static struct {
char *str;
size_t len;
} process_title;

extern char** environ;


char** uv_setup_args(int argc, char** argv) {
char **new_argv;
char **new_env;
size_t size;
int envc;
char *s;
int i;

for (envc = 0; environ[envc]; envc++);

s = envc ? environ[envc - 1] : argv[argc - 1];

process_title.str = argv[0];
process_title.len = s + strlen(s) + 1 - argv[0];

size = process_title.len;
size += (argc + 1) * sizeof(char **);
size += (envc + 1) * sizeof(char **);

if ((s = (char *) malloc(size)) == NULL) {
process_title.str = NULL;
process_title.len = 0;
return argv;
}

new_argv = (char **) s;
new_env = new_argv + argc + 1;
s = (char *) (new_env + envc + 1);
memcpy(s, process_title.str, process_title.len);

for (i = 0; i < argc; i++)
new_argv[i] = s + (argv[i] - argv[0]);
new_argv[argc] = NULL;

s += environ[0] - argv[0];

for (i = 0; i < envc; i++)
new_env[i] = s + (environ[i] - environ[0]);
new_env[envc] = NULL;

environ = new_env;
return new_argv;
}


uv_err_t uv_set_process_title(const char* title) {
/* No need to terminate, last char is always '\0'. */
if (process_title.len)
strncpy(process_title.str, title, process_title.len - 1);

return uv_ok_;
}


uv_err_t uv_get_process_title(char* buffer, size_t size) {
if (process_title.str) {
strncpy(buffer, process_title.str, size);
} else {
if (size > 0) {
buffer[0] = '\0';
}
}

return uv_ok_;
}
18 changes: 0 additions & 18 deletions src/unix/sunos.c
Expand Up @@ -238,24 +238,6 @@ void uv__fs_event_close(uv_fs_event_t* handle) {
#endif /* HAVE_PORTS_FS */


char** uv_setup_args(int argc, char** argv) {
return argv;
}


uv_err_t uv_set_process_title(const char* title) {
return uv_ok_;
}


uv_err_t uv_get_process_title(char* buffer, size_t size) {
if (size > 0) {
buffer[0] = '\0';
}
return uv_ok_;
}


uv_err_t uv_resident_set_memory(size_t* rss) {
psinfo_t psinfo;
uv_err_t err;
Expand Down
3 changes: 3 additions & 0 deletions uv.gyp
Expand Up @@ -287,6 +287,9 @@
[ 'OS=="mac" or OS=="freebsd" or OS=="openbsd" or OS=="netbsd"', {
'sources': [ 'src/unix/kqueue.c' ],
}],
[ 'OS!="win" and OS!="freebsd"', {
'sources': [ 'src/unix/proctitle.c' ],
}],
]
},

Expand Down

0 comments on commit dc97d44

Please sign in to comment.