Skip to content

Commit f39b6ef

Browse files
committed
Drop self-maintained vbuf/vsz in open_proc_stream for thread safety
1 parent d9f4bef commit f39b6ef

File tree

7 files changed

+29
-98
lines changed

7 files changed

+29
-98
lines changed

lib/dialects/linux/dlsof.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,6 @@ void lsof_dialect_destroy(struct lsof_context *ctx) {
8686
Fpa = 0;
8787
}
8888

89-
/* Free lock stream buffer */
90-
if (Vbuf) {
91-
CLEAN(Vbuf);
92-
Vsz = 0;
93-
}
94-
9589
/* Free /proc/FD/fd buffer */
9690
if (Dpath) {
9791
CLEAN(Dpath);

lib/dialects/linux/dlsof.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,6 @@ struct lsof_context_dialect {
238238
/* dnode.c - field parser state */
239239
char **field_ptrs;
240240
int field_ptrs_alloc;
241-
/* dnode.c - lock stream buffer */
242-
char *lock_stream_buf;
243-
size_t lock_stream_buf_sz;
244241
/* dproc.c - /proc/FD/fd buffer */
245242
char *fd_path_buf;
246243
int fd_path_buf_sz;
@@ -277,10 +274,6 @@ void lsof_dialect_destroy(struct lsof_context *ctx);
277274
# define Fp (ctx->dialect.field_ptrs)
278275
# define Fpa (ctx->dialect.field_ptrs_alloc)
279276

280-
/* Lock stream buffer macros */
281-
# define Vbuf (ctx->dialect.lock_stream_buf)
282-
# define Vsz (ctx->dialect.lock_stream_buf_sz)
283-
284277
/* /proc/FD/fd path buffer macros */
285278
# define Dpath (ctx->dialect.fd_path_buf)
286279
# define Dpathl (ctx->dialect.fd_path_buf_sz)

lib/dialects/linux/dmnt.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,6 @@ getmntdev(struct lsof_context *ctx, /* context */
158158
static int err = 0;
159159
int h;
160160
mntsup_t *mp, *mpn;
161-
static char *vbuf = (char *)NULL;
162-
static size_t vsz = (size_t)0;
163161

164162
if (err)
165163
return (0);
@@ -185,7 +183,7 @@ getmntdev(struct lsof_context *ctx, /* context */
185183
err = 1;
186184
return (0);
187185
}
188-
if (!(fs = open_proc_stream(ctx, MntSupP, "r", &vbuf, &vsz, 0))) {
186+
if (!(fs = open_proc_stream(ctx, MntSupP, "r", 0))) {
189187

190188
/*
191189
* The mount supplement file can't be opened for reading.
@@ -390,16 +388,14 @@ struct mounts *readmnt(struct lsof_context *ctx) {
390388
int nfs;
391389
int mqueue;
392390
struct stat sb;
393-
static char *vbuf = (char *)NULL;
394-
static size_t vsz = (size_t)0;
395391

396392
if (Lmi || Lmist)
397393
return (Lmi);
398394
/*
399395
* Open access to /proc/mounts, assigning a page size buffer to its stream.
400396
*/
401397
(void)snpf(buf, sizeof(buf), "%s/mounts", PROCFS);
402-
ms = open_proc_stream(ctx, buf, "r", &vbuf, &vsz, 1);
398+
ms = open_proc_stream(ctx, buf, "r", 1);
403399
/*
404400
* Read mount table entries.
405401
*/

lib/dialects/linux/dnode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ void get_locks(struct lsof_context *ctx, /* context */
560560
* Open the /proc lock file, assign a page size buffer to its stream,
561561
* and read it.
562562
*/
563-
if (!(ls = open_proc_stream(ctx, p, "r", &Vbuf, &Vsz, 0)))
563+
if (!(ls = open_proc_stream(ctx, p, "r", 0)))
564564
return;
565565
while (fgets(buf, sizeof(buf), ls)) {
566566
if (get_fields(ctx, buf, ":", &fp, (int *)NULL, 0) < 10)

lib/dialects/linux/dproc.c

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -819,20 +819,15 @@ static int nm2id(char *nm, /* pointer to name */
819819

820820
FILE *open_proc_stream(struct lsof_context *ctx, /* context */
821821
char *p, /* pointer to path to open */
822-
char *m, /* pointer to mode -- e.g., "r" */
823-
char **buf, /* pointer tp setvbuf() address
824-
* (NULL if none) */
825-
size_t *sz, /* setvbuf() size (0 if none or if
826-
* getpagesize() desired */
827-
int act) /* fopen() failure action:
828-
* 0 : return (FILE *)NULL
829-
* <>0 : fprintf() an error message
830-
* and Error()
831-
*/
822+
char *m, /* pointer to mode -- e.g., "r" */
823+
int act) /* fopen() failure action:
824+
* 0 : return (FILE *)NULL
825+
* <>0 : fprintf() an error message
826+
* and Error()
827+
*/
832828
{
833-
FILE *fs; /* opened stream */
834-
static size_t psz = (size_t)0; /* page size */
835-
size_t tsz; /* temporary size */
829+
FILE *fs; /* opened stream */
830+
size_t psz = (size_t)0; /* page size */
836831
/*
837832
* Open the stream.
838833
*/
@@ -843,37 +838,16 @@ FILE *open_proc_stream(struct lsof_context *ctx, /* context */
843838
strerror(errno));
844839
Error(ctx);
845840
}
846-
/*
847-
* Return the stream if no buffer change is required.
848-
*/
849-
if (!buf)
850-
return (fs);
851841
/*
852842
* Determine the buffer size required.
853843
*/
854-
if (!(tsz = *sz)) {
855-
if (!psz)
856-
psz = getpagesize();
857-
tsz = psz;
858-
}
859-
/*
860-
* Allocate a buffer for the stream, as required.
861-
*/
862-
if (!*buf) {
863-
if (!(*buf = (char *)malloc((MALLOC_S)tsz))) {
864-
(void)fprintf(stderr,
865-
"%s: can't allocate %d bytes for %s stream buffer\n",
866-
Pn, (int)tsz, p);
867-
Error(ctx);
868-
}
869-
*sz = tsz;
870-
}
844+
psz = getpagesize();
871845
/*
872-
* Assign the buffer to the stream.
846+
* Setup stream buffering.
873847
*/
874-
if (setvbuf(fs, *buf, _IOFBF, tsz)) {
848+
if (setvbuf(fs, NULL, _IOFBF, psz)) {
875849
(void)fprintf(stderr, "%s: setvbuf(%s)=%d failure: %s\n", Pn, p,
876-
(int)tsz, strerror(errno));
850+
(int)psz, strerror(errno));
877851
Error(ctx);
878852
}
879853
return (fs);
@@ -1441,14 +1415,12 @@ process_proc_map(struct lsof_context *ctx, /* context */
14411415
static struct saved_map *sm = (struct saved_map *)NULL;
14421416
efsys_list_t *rep;
14431417
static int sma = 0;
1444-
static char *vbuf = (char *)NULL;
1445-
static size_t vsz = (size_t)0;
14461418
int diff_mntns = 0;
14471419
/*
14481420
* Open the /proc/<pid>/maps file, assign a page size buffer to its stream,
14491421
* and read it/
14501422
*/
1451-
if (!(ms = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
1423+
if (!(ms = open_proc_stream(ctx, p, "r", 0)))
14521424
return;
14531425

14541426
/* target process in a different mount namespace from lsof process. */
@@ -1695,13 +1667,11 @@ static int read_id_stat(struct lsof_context *ctx, /* context */
16951667
static char *cbf = (char *)NULL;
16961668
static MALLOC_S cbfa = 0;
16971669
FILE *fs;
1698-
static char *vbuf = (char *)NULL;
1699-
static size_t vsz = (size_t)0;
17001670
/*
17011671
* Open the stat file path, assign a page size buffer to its stream,
17021672
* and read the file's first line.
17031673
*/
1704-
if (!(fs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
1674+
if (!(fs = open_proc_stream(ctx, p, "r", 0)))
17051675
return (-1);
17061676
if (!(cp = fgets(buf, sizeof(buf), fs))) {
17071677

lib/dialects/linux/dproto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ extern int is_file_named(struct lsof_context *ctx, int ty, char *p,
6060
extern int make_proc_path(struct lsof_context *ctx, char *pp, int lp, char **np,
6161
int *npl, char *sf);
6262
extern FILE *open_proc_stream(struct lsof_context *ctx, char *p, char *mode,
63-
char **buf, size_t *sz, int act);
63+
int act);
6464
extern void process_proc_node(struct lsof_context *ctx, char *p, char *pbr,
6565
struct stat *s, int ss, struct stat *l, int ls);
6666
extern void process_proc_sock(struct lsof_context *ctx, char *p, char *pbr,

lib/dialects/linux/dsock.c

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -702,8 +702,6 @@ static void get_ax25(struct lsof_context *ctx,
702702
unsigned long rq, sq, state;
703703
MALLOC_S len;
704704
unsigned char rqs, sqs;
705-
static char *vbuf = (char *)NULL;
706-
static size_t vsz = (size_t)0;
707705
/*
708706
* Do second time cleanup or first time setup.
709707
*/
@@ -734,7 +732,7 @@ static void get_ax25(struct lsof_context *ctx,
734732
* Open the /proc/net/ax25 file, assign a page size buffer to the stream,
735733
* and read it. Store AX25 socket info in the AX25sin[] hash buckets.
736734
*/
737-
if (!(as = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
735+
if (!(as = open_proc_stream(ctx, p, "r", 0)))
738736
return;
739737
while (fgets(buf, sizeof(buf) - 1, as)) {
740738
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < 24)
@@ -1544,8 +1542,6 @@ static void get_icmp(struct lsof_context *ctx, /* context */
15441542
INODETYPE inode;
15451543
struct icmpin *np, *icmpp;
15461544
MALLOC_S lal, ral;
1547-
static char *vbuf = (char *)NULL;
1548-
static size_t vsz = (size_t)0;
15491545
FILE *xs;
15501546
/*
15511547
* Do second time cleanup or first time setup.
@@ -1571,7 +1567,7 @@ static void get_icmp(struct lsof_context *ctx, /* context */
15711567
* Open the /proc/net/icmp file, assign a page size buffer to its stream,
15721568
* and read the file. Store icmp info in the Icmpin[] hash buckets.
15731569
*/
1574-
if (!(xs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
1570+
if (!(xs = open_proc_stream(ctx, p, "r", 0)))
15751571
return;
15761572
while (fgets(buf, sizeof(buf) - 1, xs)) {
15771573
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < 11)
@@ -1673,8 +1669,6 @@ static void get_ipx(struct lsof_context *ctx, /* context */
16731669
unsigned long rxq, state, txq;
16741670
struct ipxsin *ip, *np;
16751671
MALLOC_S len;
1676-
static char *vbuf = (char *)NULL;
1677-
static size_t vsz = (size_t)0;
16781672
FILE *xs;
16791673
/*
16801674
* Do second time cleanup or first time setup.
@@ -1704,7 +1698,7 @@ static void get_ipx(struct lsof_context *ctx, /* context */
17041698
* Open the /proc/net/ipx file, assign a page size buffer to the stream,
17051699
* and read it. Store IPX socket info in the Ipxsin[] hash buckets.
17061700
*/
1707-
if (!(xs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
1701+
if (!(xs = open_proc_stream(ctx, p, "r", 0)))
17081702
return;
17091703
while (fgets(buf, sizeof(buf) - 1, xs)) {
17101704
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < 7)
@@ -1819,8 +1813,6 @@ static void get_netlink(struct lsof_context *ctx, /* context */
18191813
int h, pr;
18201814
INODETYPE inode;
18211815
struct nlksin *np, *lp;
1822-
static char *vbuf = (char *)NULL;
1823-
static size_t vsz = (size_t)0;
18241816
FILE *xs;
18251817
/*
18261818
* Do second time cleanup or first time setup.
@@ -1846,7 +1838,7 @@ static void get_netlink(struct lsof_context *ctx, /* context */
18461838
* Open the /proc/net/netlink file, assign a page size buffer to its stream,
18471839
* and read the file. Store Netlink info in the Nlksin[] hash buckets.
18481840
*/
1849-
if (!(xs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
1841+
if (!(xs = open_proc_stream(ctx, p, "r", 0)))
18501842
return;
18511843
while (fgets(buf, sizeof(buf) - 1, xs)) {
18521844
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < 10)
@@ -1913,8 +1905,6 @@ static void get_pack(struct lsof_context *ctx, /* context */
19131905
INODETYPE inode;
19141906
struct packin *np, *pp;
19151907
unsigned long pr;
1916-
static char *vbuf = (char *)NULL;
1917-
static size_t vsz = (size_t)0;
19181908
FILE *xs;
19191909
/*
19201910
* Do second time cleanup or first time setup.
@@ -1940,7 +1930,7 @@ static void get_pack(struct lsof_context *ctx, /* context */
19401930
* Open the /proc/net/packet file, assign a page size buffer to its stream,
19411931
* and read the file. Store packet info in the Packin[] hash buckets.
19421932
*/
1943-
if (!(xs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
1933+
if (!(xs = open_proc_stream(ctx, p, "r", 0)))
19441934
return;
19451935
while (fgets(buf, sizeof(buf) - 1, xs)) {
19461936
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < 9)
@@ -2012,8 +2002,6 @@ static void get_raw(struct lsof_context *ctx, /* context */
20122002
int nf = 12;
20132003
struct rawsin *np, *rp;
20142004
MALLOC_S lal, ral, spl;
2015-
static char *vbuf = (char *)NULL;
2016-
static size_t vsz = (size_t)0;
20172005
FILE *xs;
20182006
/*
20192007
* Do second time cleanup or first time setup.
@@ -2043,7 +2031,7 @@ static void get_raw(struct lsof_context *ctx, /* context */
20432031
* Open the /proc/net/raw file, assign a page size buffer to its stream,
20442032
* and read the file. Store raw socket info in the Rawsin[] hash buckets.
20452033
*/
2046-
if (!(xs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
2034+
if (!(xs = open_proc_stream(ctx, p, "r", 0)))
20472035
return;
20482036
while (fgets(buf, sizeof(buf) - 1, xs)) {
20492037
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < nf)
@@ -2150,8 +2138,6 @@ static void get_sctp(struct lsof_context *ctx) {
21502138
MALLOC_S len, plen;
21512139
struct sctpsin *sp, *np;
21522140
FILE *ss;
2153-
static char *vbuf = (char *)NULL;
2154-
static size_t vsz = (size_t)0;
21552141
/*
21562142
* Do second time cleanup or first time setup.
21572143
*/
@@ -2189,7 +2175,7 @@ static void get_sctp(struct lsof_context *ctx) {
21892175
* and read them. Store SCTP socket info in the SCTPsin[] hash buckets.
21902176
*/
21912177
for (i = 0; i < NSCTPPATHS; i++) {
2192-
if (!(ss = open_proc_stream(ctx, SCTPPath[i], "r", &vbuf, &vsz, 0)))
2178+
if (!(ss = open_proc_stream(ctx, SCTPPath[i], "r", 0)))
21932179
continue;
21942180
fl = 1;
21952181
while (fgets(buf, sizeof(buf) - 1, ss)) {
@@ -2516,8 +2502,6 @@ static void get_tcpudp(struct lsof_context *ctx, /* context */
25162502
int h, nf;
25172503
INODETYPE inode;
25182504
struct tcp_udp *np, *tp;
2519-
static char *vbuf = (char *)NULL;
2520-
static size_t vsz = (size_t)0;
25212505

25222506
#if defined(HASEPTOPTS)
25232507
pxinfo_t *pp, *pnp;
@@ -2598,7 +2582,7 @@ static void get_tcpudp(struct lsof_context *ctx, /* context */
25982582
* Open the /proc/net file, assign a page size buffer to the stream, and
25992583
* read it.
26002584
*/
2601-
if (!(fs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
2585+
if (!(fs = open_proc_stream(ctx, p, "r", 0)))
26022586
return;
26032587
nf = 12;
26042588
while (fgets(buf, sizeof(buf) - 1, fs)) {
@@ -2719,8 +2703,6 @@ static void get_raw6(struct lsof_context *ctx, /* context */
27192703
int nf = 12;
27202704
struct rawsin *np, *rp;
27212705
MALLOC_S lal, ral, spl;
2722-
static char *vbuf = (char *)NULL;
2723-
static size_t vsz = (size_t)0;
27242706
FILE *xs;
27252707
/*
27262708
* Do second time cleanup or first time setup.
@@ -2752,7 +2734,7 @@ static void get_raw6(struct lsof_context *ctx, /* context */
27522734
* Open the /proc/net/raw6 file, assign a page size buffer to the stream,
27532735
* and read it. Store raw6 socket info in the Rawsin6[] hash buckets.
27542736
*/
2755-
if (!(xs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
2737+
if (!(xs = open_proc_stream(ctx, p, "r", 0)))
27562738
return;
27572739
while (fgets(buf, sizeof(buf) - 1, xs)) {
27582740
if (get_fields(ctx, buf, (char *)NULL, &fp, (int *)NULL, 0) < nf)
@@ -2865,8 +2847,6 @@ static void get_tcpudp6(struct lsof_context *ctx, /* context */
28652847
int h, i, nf;
28662848
INODETYPE inode;
28672849
struct tcp_udp6 *np6, *tp6;
2868-
static char *vbuf = (char *)NULL;
2869-
static size_t vsz = (size_t)0;
28702850

28712851
# if defined(HASEPTOPTS)
28722852
pxinfo_t *pp, *pnp;
@@ -2957,7 +2937,7 @@ static void get_tcpudp6(struct lsof_context *ctx, /* context */
29572937
* Open the /proc/net file, assign a page size buffer to the stream,
29582938
* and read it.
29592939
*/
2960-
if (!(fs = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
2940+
if (!(fs = open_proc_stream(ctx, p, "r", 0)))
29612941
return;
29622942
nf = 12;
29632943
while (fgets(buf, sizeof(buf) - 1, fs)) {
@@ -3076,8 +3056,6 @@ static void get_unix(struct lsof_context *ctx, /* context */
30763056
uxsin_t *np, *up;
30773057
FILE *us;
30783058
uint32_t ty;
3079-
static char *vbuf = (char *)NULL;
3080-
static size_t vsz = (size_t)0;
30813059

30823060
#if defined(HASEPTOPTS) && defined(HASUXSOCKEPT)
30833061
pxinfo_t *pp, *pnp;
@@ -3119,7 +3097,7 @@ static void get_unix(struct lsof_context *ctx, /* context */
31193097
* Open the /proc/net/unix file, assign a page size buffer to the stream,
31203098
* read the file's contents, and add them to the Uxsin hash buckets.
31213099
*/
3122-
if (!(us = open_proc_stream(ctx, p, "r", &vbuf, &vsz, 0)))
3100+
if (!(us = open_proc_stream(ctx, p, "r", 0)))
31233101
return;
31243102
while (fgets(buf, sizeof(buf) - 1, us)) {
31253103
if ((nf = get_fields(ctx, buf, ":", &fp, (int *)NULL, 0)) < 7)

0 commit comments

Comments
 (0)