From 68aea40e09df6409198bcf423cd4acb9fffd0a88 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 17 Mar 2001 14:42:16 +0000 Subject: [PATCH] Define setproctitle() and use it for gatherer thread status reporting. --- lib/Makefile | 2 +- lib/lib.h | 5 ++++ lib/proctitle.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 lib/proctitle.c diff --git a/lib/Makefile b/lib/Makefile index 66a5a72c..c76105d6 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -7,7 +7,7 @@ SHLIB_OBJS=alloc.o alloc_str.o ctmatch.o db.o fastbuf.o fb-file.o fb-mem.o lists log.o log2.o md5.o md5hex.o mmap.o pagecache.o patimatch.o patmatch.o pool.o \ prime.o random.o realloc.o regex.o temp.o timer.o url.o wildmatch.o \ wordsplit.o str_ctype.o str_upper.o bucket.o conf.o object.o sorter.o \ - finger.o + finger.o proctitle.o obj/lib/libsh.a: $(addprefix obj/lib/,$(SHLIB_OBJS)) diff --git a/lib/lib.h b/lib/lib.h index eb079dcd..05bde59e 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -184,4 +184,9 @@ uns random_max(uns); void *mmap_file(byte *name, unsigned *len, int writeable); void munmap_file(void *start, unsigned len); +/* proctitle.c */ + +void setproctitle_init(int argc, char **argv); +void setproctitle(char *msg, ...) __attribute__((format(printf,1,2))); + #endif diff --git a/lib/proctitle.c b/lib/proctitle.c new file mode 100644 index 00000000..e5f9bd4f --- /dev/null +++ b/lib/proctitle.c @@ -0,0 +1,69 @@ +/* + * Sherlock Library -- Setting of Process Title + * + * (c) 2001 Martin Mares + */ + +#include "lib/lib.h" + +#include +#include +#include +#include + +static char *spt_start, *spt_end; + +void +setproctitle_init(int argc, char **argv) +{ +#ifdef linux +#if 0 /* FIXME: This doesn't work. Why? */ + uns i, len; + char **env, *t; + + /* Create a backup copy of environment */ + len = 0; + for (i=0; __environ[i]; i++) + len += strlen(__environ[i]) + 1; + env = xmalloc(sizeof(char *)*(i+1)); + t = xmalloc(len); + spt_end = __environ[0]; + for (i=0; __environ[i]; i++) + { + env[i] = t; + len = strlen(__environ[i]) + 1; + memcpy(t, __environ[i], len); + t += len; + spt_end = MAX(spt_end, __environ[i] + len); + } + env[i] = NULL; + __environ[0] = NULL; + spt_start = (byte *)(__environ+1); + __environ = env; + argv[0] = spt_start; +#else + spt_start = argv[0]; + spt_end = argv[argc-1] + strlen(argv[argc-1]) - 1; +#endif +#endif +} + +void +setproctitle(char *msg, ...) +{ + va_list args; + byte buf[256]; + int n; + + va_start(args, msg); + if (spt_end > spt_start) + { + n = vsnprintf(buf, sizeof(buf), msg, args); + if (n >= (int) sizeof(buf) || n < 0) + sprintf(buf, ""); + n = spt_end - spt_start; + strncpy(spt_start, buf, n); + spt_start[n] = 0; + } + va_end(args); +} -- 2.39.2