1 /* Copyright (C) 1998-99 Martin Baulig
2    This file is part of LibGTop 1.0.
3 
4    Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
5 
6    LibGTop is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License,
9    or (at your option) any later version.
10 
11    LibGTop is distributed in the hope that it will be useful, but WITHOUT
12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14    for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with LibGTop; see the file COPYING. If not, write to the
18    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19    Boston, MA 02110-1301, USA.
20 */
21 
22 #include <config.h>
23 #include <procinfo.h>
24 
25 #include <glibtop.h>
26 #include <glibtop/error.h>
27 #include <glibtop/procargs.h>
28 
29 #include "utils.h"
30 
31 static const unsigned long _glibtop_sysdeps_proc_args =
32 	(1 << GLIBTOP_PROC_ARGS_SIZE);
33 
34 /* Init function. */
35 
36 void
_glibtop_init_proc_args_s(glibtop * server)37 _glibtop_init_proc_args_s (glibtop *server)
38 {
39 	server->sysdeps.proc_args = _glibtop_sysdeps_proc_args;
40 }
41 
42 /* Provides detailed information about a process. */
43 
_glibtop_remove_non_ascii(char * string)44 static void _glibtop_remove_non_ascii(char* string)
45 {
46 	for (;*string != 0;string++)
47 	{
48 		if (!isascii(*string))
49 			*string = '?';
50 	}
51 }
52 
53 char *
glibtop_get_proc_args_s(glibtop * server,glibtop_proc_args * buf,pid_t pid,unsigned max_len)54 glibtop_get_proc_args_s (glibtop *server, glibtop_proc_args *buf,
55 			 pid_t pid, unsigned max_len)
56 {
57 	struct procsinfo *pinfo;
58 	char *args_buffer;
59 	char* args = NULL;
60 	int size;
61 	int result;
62 	int len;
63 
64 	glibtop_init_s (&server, (1L << GLIBTOP_SYSDEPS_PROC_ARGS), 0);
65 
66 	memset (buf, 0, sizeof (glibtop_proc_args));
67 
68 	pinfo = _glibtop_get_procinfo(server, pid);
69 	if (pinfo == NULL) return NULL;
70 
71 	size = max_len != 0 ? max_len : 4096;
72 	args_buffer = g_malloc (size);
73 
74 	result = getargs(pinfo, sizeof(struct procsinfo), args_buffer, size);
75 	if (result == -1)
76 	{
77 		glibtop_error_io_r (server, "Cannot malloc getargs");
78 
79 		g_free(args_buffer);
80 
81 		return NULL;
82 	}
83 
84 	/* look if empty string */
85 
86 	if (args_buffer[0] == 0)
87 	{
88 		g_free(args_buffer);
89 
90 		return NULL;
91 	}
92 
93 	/* compute length of args and realloc */
94 
95 	len = 0;
96 	while ((args_buffer[len] != 0) && (len < size))
97 	{
98 		_glibtop_remove_non_ascii(args_buffer + len);
99 
100 		len += strlen(args_buffer + len) + 1;
101 	}
102 
103 	args = g_malloc (len);
104 
105 	memcpy(args, args_buffer, len);
106 	g_free(args_buffer);
107 
108 	buf->size = len - 1;
109 
110 	buf->flags = _glibtop_sysdeps_proc_args;
111 
112 	return args;
113 }
114