Skip to content

Commit 1c3d6b4

Browse files
committed
Drop more static variables for thread safety
1 parent f39b6ef commit 1c3d6b4

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

lib/dialects/linux/dproc.c

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -208,22 +208,21 @@ static MALLOC_S alloc_cbf(struct lsof_context *ctx, /* context */
208208
*/
209209

210210
void gather_proc_info(struct lsof_context *ctx) {
211-
char *cmd, *tcmd;
212-
char cmdbuf[MAXPATHLEN];
211+
char *cmd = NULL, *tcmd;
213212
struct dirent *dp;
214213
unsigned char ht, pidts;
215214
int n, nl, pgid, pid, ppid, prv, rv, tid, tpgid, tppid, tx;
216-
static char *path = (char *)NULL;
217-
static int pathl = 0;
218-
static char *pidpath = (char *)NULL;
219-
static MALLOC_S pidpathl = 0;
220-
static MALLOC_S pidx = 0;
221-
static DIR *ps = (DIR *)NULL;
215+
char *path = (char *)NULL;
216+
int pathl = 0;
217+
char *pidpath = (char *)NULL;
218+
MALLOC_S pidpathl = 0;
219+
MALLOC_S pidx = 0;
220+
DIR *ps = (DIR *)NULL;
222221
struct stat sb;
223-
static char *taskpath = (char *)NULL;
224-
static int taskpathl = 0;
225-
static char *tidpath = (char *)NULL;
226-
static int tidpathl = 0;
222+
char *taskpath = (char *)NULL;
223+
int taskpathl = 0;
224+
char *tidpath = (char *)NULL;
225+
int tidpathl = 0;
227226
DIR *ts;
228227
UID_ARG uid;
229228

@@ -333,6 +332,9 @@ void gather_proc_info(struct lsof_context *ctx) {
333332
* Get the PID's command name.
334333
*/
335334
(void)make_proc_path(ctx, pidpath, n, &path, &pathl, "stat");
335+
336+
/* Free previous cmd buffer */
337+
CLEAN(cmd);
336338
if ((prv = read_id_stat(ctx, path, pid, &cmd, &ppid, &pgid)) < 0)
337339
cmd = NULL; /* NULL means failure to get command name */
338340

@@ -346,16 +348,6 @@ void gather_proc_info(struct lsof_context *ctx) {
346348
* options work properly.
347349
*/
348350
else if (!IgnTasks && (Selflags & SELTASK)) {
349-
/*
350-
* Copy cmd before next call to read_id_stat due to static
351-
* variables
352-
*/
353-
if (cmd) {
354-
strncpy(cmdbuf, cmd, sizeof(cmdbuf) - 1);
355-
cmdbuf[sizeof(cmdbuf) - 1] = '\0';
356-
cmd = cmdbuf;
357-
}
358-
359351
(void)make_proc_path(ctx, pidpath, n, &taskpath, &taskpathl,
360352
"task");
361353
tx = n + 4;
@@ -435,6 +427,17 @@ void gather_proc_info(struct lsof_context *ctx) {
435427
}
436428
}
437429
}
430+
431+
/* cleanup buffers */
432+
CLEAN(path);
433+
CLEAN(pidpath);
434+
CLEAN(taskpath);
435+
CLEAN(tidpath);
436+
CLEAN(cmd);
437+
if (ps) {
438+
closedir(ps);
439+
ps = NULL;
440+
}
438441
}
439442

440443
/*
@@ -1412,9 +1415,9 @@ process_proc_map(struct lsof_context *ctx, /* context */
14121415
dev_t dev;
14131416
INODETYPE inode;
14141417
};
1415-
static struct saved_map *sm = (struct saved_map *)NULL;
1418+
struct saved_map *sm = (struct saved_map *)NULL;
14161419
efsys_list_t *rep;
1417-
static int sma = 0;
1420+
int sma = 0;
14181421
int diff_mntns = 0;
14191422
/*
14201423
* Open the /proc/<pid>/maps file, assign a page size buffer to its stream,
@@ -1644,6 +1647,7 @@ process_proc_map(struct lsof_context *ctx, /* context */
16441647
link_lfile(ctx);
16451648
}
16461649
(void)fclose(ms);
1650+
CLEAN(sm);
16471651
}
16481652

16491653
/*
@@ -1664,8 +1668,8 @@ static int read_id_stat(struct lsof_context *ctx, /* context */
16641668
{
16651669
char buf[MAXPATHLEN], *cp, *cp1, **fp;
16661670
int ch, cx, es, pc;
1667-
static char *cbf = (char *)NULL;
1668-
static MALLOC_S cbfa = 0;
1671+
char *cbf = (char *)NULL;
1672+
MALLOC_S cbfa = 0;
16691673
FILE *fs;
16701674
/*
16711675
* Open the stat file path, assign a page size buffer to its stream,
@@ -1677,6 +1681,7 @@ static int read_id_stat(struct lsof_context *ctx, /* context */
16771681

16781682
read_id_stat_exit:
16791683

1684+
CLEAN(cbf);
16801685
(void)fclose(fs);
16811686
return (-1);
16821687
}
@@ -1704,13 +1709,6 @@ static int read_id_stat(struct lsof_context *ctx, /* context */
17041709
cp++;
17051710
pc = 1; /* start the parenthesis balance count at 1 */
17061711

1707-
/* empty process name to avoid leaking previous process name,
1708-
* see issue #246
1709-
*/
1710-
if (cbf) {
1711-
cbf[0] = '\0';
1712-
}
1713-
17141712
/*
17151713
* Enter the command characters safely. Supply them from the initial read
17161714
* of the stat file line, a '\n' if the initial read didn't yield a ')'
@@ -1747,6 +1745,7 @@ static int read_id_stat(struct lsof_context *ctx, /* context */
17471745
es = 1; /* Switch to fgetc() when a '\0' appears. */
17481746
}
17491747
*cmd = cbf;
1748+
cbf = NULL;
17501749
/*
17511750
* Read the remainder of the stat line if it was necessary to read command
17521751
* characters individually from the stat file.

0 commit comments

Comments
 (0)