Gentoo Archives: gentoo-commits

From: "Mike Frysinger (vapier)" <vapier@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] gentoo-projects commit in pax-utils: pspax.c
Date: Tue, 30 Dec 2008 13:50:09
Message-Id: E1LHeyz-0002gV-1g@stork.gentoo.org
1 vapier 08/12/30 13:50:05
2
3 Modified: pspax.c
4 Log:
5 cleanup pspax code a bit
6
7 Revision Changes Path
8 1.43 pax-utils/pspax.c
9
10 file : http://sources.gentoo.org/viewcvs.py/gentoo-projects/pax-utils/pspax.c?rev=1.43&view=markup
11 plain: http://sources.gentoo.org/viewcvs.py/gentoo-projects/pax-utils/pspax.c?rev=1.43&content-type=text/plain
12 diff : http://sources.gentoo.org/viewcvs.py/gentoo-projects/pax-utils/pspax.c?r1=1.42&r2=1.43
13
14 Index: pspax.c
15 ===================================================================
16 RCS file: /var/cvsroot/gentoo-projects/pax-utils/pspax.c,v
17 retrieving revision 1.42
18 retrieving revision 1.43
19 diff -u -r1.42 -r1.43
20 --- pspax.c 30 Dec 2008 13:13:15 -0000 1.42
21 +++ pspax.c 30 Dec 2008 13:50:04 -0000 1.43
22 @@ -12,7 +12,7 @@
23 * cc -o pspax pspax.c -DWANT_SYSCAP -lcap
24 */
25
26 -static const char *rcsid = "$Id: pspax.c,v 1.42 2008/12/30 13:13:15 vapier Exp $";
27 +static const char *rcsid = "$Id: pspax.c,v 1.43 2008/12/30 13:50:04 vapier Exp $";
28 const char * const argv0 = "pspax";
29
30 #include "paxinc.h"
31 @@ -41,35 +41,38 @@
32 static uid_t show_uid = -1;
33 static gid_t show_gid = -1;
34
35 +static FILE *proc_fopen(pid_t pid, const char *file)
36 +{
37 + char path[__PAX_UTILS_PATH_MAX];
38 + snprintf(path, sizeof(path), PROC_DIR "/%u/%s", pid, file);
39 + path[sizeof(path) - 1] = '\0';
40 + return fopen(path, "r");
41 +}
42 +
43 static char *get_proc_name(pid_t pid)
44 {
45 FILE *fp;
46 - static char str[__PAX_UTILS_PATH_MAX];
47 - memset(&str, 0, sizeof(str));
48 + static char str[BUFSIZ];
49
50 - snprintf(str, sizeof(str), PROC_DIR "/%u/stat", pid);
51 - if ((fp = fopen(str, "r")) == NULL)
52 + if ((fp = proc_fopen(pid, "stat")) == NULL)
53 return NULL;
54
55 - memset(&str, 0, sizeof(str));
56 -
57 fscanf(fp, "%*d %s.16", str);
58 if (*str) {
59 str[strlen(str) - 1] = '\0';
60 str[16] = 0;
61 }
62 fclose(fp);
63 +
64 return (str+1);
65 }
66
67 static int get_proc_maps(pid_t pid)
68 {
69 - static char str[__PAX_UTILS_PATH_MAX];
70 FILE *fp;
71 + static char str[BUFSIZ];
72
73 - snprintf(str, sizeof(str), PROC_DIR "/%u/maps", pid);
74 -
75 - if ((fp = fopen(str, "r")) == NULL)
76 + if ((fp = proc_fopen(pid, "maps")) == NULL)
77 return -1;
78
79 while (fgets(str, sizeof(str), fp)) {
80 @@ -96,17 +99,16 @@
81 }
82 }
83 fclose(fp);
84 +
85 return 0;
86 }
87
88 static int print_executable_mappings(pid_t pid)
89 {
90 - static char str[__PAX_UTILS_PATH_MAX];
91 FILE *fp;
92 + static char str[BUFSIZ];
93
94 - snprintf(str, sizeof(str), PROC_DIR "/%u/maps", pid);
95 -
96 - if ((fp = fopen(str, "r")) == NULL)
97 + if ((fp = proc_fopen(pid, "maps")) == NULL)
98 return -1;
99
100 while (fgets(str, sizeof(str), fp)) {
101 @@ -131,6 +133,7 @@
102 }
103 }
104 fclose(fp);
105 +
106 return 0;
107 }
108
109 @@ -147,13 +150,14 @@
110 {
111 struct stat st;
112 struct passwd *pwd;
113 - static char str[__PAX_UTILS_PATH_MAX];
114 + char path[__PAX_UTILS_PATH_MAX];
115
116 - snprintf(str, sizeof(str), PROC_DIR "/%u/stat", pid);
117 + snprintf(path, sizeof(path), PROC_DIR "/%u/stat", pid);
118
119 - if (stat(str, &st) != -1)
120 + if (stat(path, &st) != -1)
121 if ((pwd = getpwuid(st.st_uid)) != NULL)
122 return pwd;
123 +
124 return NULL;
125 }
126
127 @@ -161,10 +165,9 @@
128 {
129 FILE *fp;
130 size_t len;
131 - static char str[__PAX_UTILS_PATH_MAX];
132 + static char str[BUFSIZ];
133
134 - snprintf(str, sizeof(str), PROC_DIR "/%u/status", pid);
135 - if ((fp = fopen(str, "r")) == NULL)
136 + if ((fp = proc_fopen(pid, "status")) == NULL)
137 return NULL;
138
139 len = strlen(name);
140 @@ -178,6 +181,7 @@
141 }
142 }
143 fclose(fp);
144 +
145 return NULL;
146 }
147
148 @@ -185,18 +189,16 @@
149 {
150 FILE *fp;
151 char *p;
152 - char str[32];
153 static char buf[BUFSIZ];
154
155 - memset(buf, 0, sizeof(buf));
156 -
157 - snprintf(str, sizeof(str), PROC_DIR "/%u/attr/current", pid);
158 - if ((fp = fopen(str, "r")) == NULL)
159 + if ((fp = proc_fopen(pid, "attr/current")) == NULL)
160 return NULL;
161 +
162 if (fgets(buf, sizeof(buf), fp) != NULL)
163 if ((p = strchr(buf, '\n')) != NULL)
164 *p = 0;
165 fclose(fp);
166 +
167 return buf;
168 }
169
170 @@ -204,31 +206,29 @@
171 {
172 FILE *fp;
173 char *p;
174 - char str[32];
175 static char buf[BUFSIZ];
176
177 - memset(buf, 0, sizeof(buf));
178 -
179 - snprintf(str, sizeof(str), PROC_DIR "/%u/ipaddr", pid);
180 - if ((fp = fopen(str, "r")) == NULL)
181 + if ((fp = proc_fopen(pid, "ipaddr")) == NULL)
182 return NULL;
183 +
184 if (fgets(buf, sizeof(buf), fp) != NULL)
185 if ((p = strchr(buf, '\n')) != NULL)
186 *p = 0;
187 fclose(fp);
188 +
189 return buf;
190 }
191
192 static const char *get_proc_type(pid_t pid)
193 {
194 char fname[32];
195 - elfobj *elf = NULL;
196 - char *ret = NULL;
197 + elfobj *elf;
198 + const char *ret;
199
200 snprintf(fname, sizeof(fname), PROC_DIR "/%u/exe", pid);
201 if ((elf = readelf(fname)) == NULL)
202 - return ret;
203 - ret = (char *)get_elfetype(elf);
204 + return NULL;
205 + ret = get_elfetype(elf);
206 unreadelf(elf);
207 return ret;
208 }
209 @@ -273,13 +273,13 @@
210 static const char *get_proc_phdr(pid_t pid)
211 {
212 char fname[32];
213 - elfobj *elf = NULL;
214 - char *ret = NULL;
215 + elfobj *elf;
216 + const char *ret;
217
218 snprintf(fname, sizeof(fname), PROC_DIR "/%u/exe", pid);
219 if ((elf = readelf(fname)) == NULL)
220 - return ret;
221 - ret = (char *) scanelf_file_phdr(elf);
222 + return NULL;
223 + ret = scanelf_file_phdr(elf);
224 unreadelf(elf);
225 return ret;
226 }