File: processname.c

package info (click to toggle)
moosefs 4.58.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,644 kB
  • sloc: ansic: 129,204; python: 12,419; sh: 5,874; javascript: 2,273; makefile: 838
file content (111 lines) | stat: -rw-r--r-- 2,441 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (C) 2025 Jakub Kruszona-Zawadzki, Saglabs SA
 * 
 * This file is part of MooseFS.
 * 
 * MooseFS 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, version 2 (only).
 * 
 * MooseFS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseFS; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA
 * or visit http://www.gnu.org/licenses/gpl-2.0.html
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

static char *argv_start;
static uint32_t argv_leng;

void processname_init(int argc,char *argv[]) {
	extern char **environ;
	char **argp;
	char *lastpos;
	int i,j;

	argv_start = NULL;
	argv_leng = 0;

	if (argc==0 || argv[0]==NULL) { // ???
		return;
	}

	// copy environment
	argp = environ;
	for (i=0 ; argp[i]!=NULL ; i++) {}
	environ = malloc((i+1) * sizeof(char*));
	if (environ==NULL) {
		environ = argp;
		return;
	}
	for (i=0 ; argp[i]!=NULL ; i++) {
		environ[i] = strdup(argp[i]);
		if (environ[i]==NULL) { // strdup failed ?
			for (j=0 ; j<i ; j++) {
				free(environ[i]);
			}
			free(environ);
			environ = argp;
			return;
		}
	}
	environ[i] = NULL;

	lastpos = NULL;
	for (i=0 ; i<argc ; i++) {
		if (lastpos==NULL || lastpos+1==argv[i]) {
			lastpos = argv[i] + strlen(argv[i]);
		}
	}
	for (i=0 ; argp[i]!=NULL ; i++) {
		if (lastpos+1==argp[i]) {
			lastpos = argp[i] + strlen(argp[i]);
		}
	}

	argv_start = argv[0];
	argv_leng = (lastpos - argv_start) - 1;
}

void processname_set(char *name) {
#ifdef HAVE_SETPROCTITLE
	setproctitle("%s",name);
#else
	uint32_t l;
	if (argv_leng>0) {
		l = strlen(name);
		if (l>=argv_leng) {
			l = argv_leng-1;
		}
		if (l>0) {
			memcpy(argv_start,name,l);
		}
//		if (l<argv_leng) { // always true - so ignore this condition
		memset(argv_start+l,0,argv_leng-l);
//		}
	}
#endif
}

#if 0
int main(int argc,char *argv[]) {
	processname_init(argc,argv);
	processname_set("Process with very funny name shown in ps");

	sleep(10);
	return 0;
}
#endif