Discussion:
getprogname() for IBM z/OS
Daniel Richard G.
2016-10-12 08:51:37 UTC
Permalink
Please double-check the use of strdup(), to avoid memory leakage.

2016-10-12 Daniel Richard G. <***@iSKUNK.ORG>

getprogname: port to IBM z/OS
* lib/getprogname.c (getprogname) [__MVS__]: Use getpid,
w_getpsent and strdup to obtain the program name string.


--Daniel
--
Daniel Richard G. || ***@iSKUNK.ORG
My ASCII-art .sig got a bad case of Times New Roman.
Pádraig Brady
2016-10-12 10:04:55 UTC
Permalink
+#elif __MVS__
+ /* https://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.bpxbd00/rtwgetp.htm */
+ char *p = "?";
+ pid_t pid = getpid ();
+ int token;
+ W_PSPROC buf;
+ memset (&buf, 0, sizeof(buf));
+ buf.ps_cmdptr = (char *) malloc (buf.ps_cmdlen = PS_CMDBLEN_LONG);
+ buf.ps_conttyptr = (char *) malloc (buf.ps_conttylen = PS_CONTTYBLEN);
+ buf.ps_pathptr = (char *) malloc (buf.ps_pathlen = PS_PATHBLEN);
+ if (buf.ps_cmdptr && buf.ps_conttyptr && buf.ps_pathptr)
+ {
+ for (token = 0; token >= 0;
+ token = w_getpsent (token, &buf, sizeof(buf)))
+ {
+ if (token > 0 && buf.ps_pid == pid)
+ {
+ p = strdup (last_component (buf.ps_pathptr));
You only want to strdup once.
So you could use a static to track that as is done in the AIX case.
+ break;
+ }
+ }
+ }
+ if (buf.ps_cmdptr) free (buf.ps_cmdptr);
+ if (buf.ps_conttyptr) free (buf.ps_conttyptr);
+ if (buf.ps_pathptr) free (buf.ps_pathptr);
+ return p;
You can call free(NULL), so the last 3 ifs are redundant.

thanks!
Pádraig
Daniel Richard G.
2016-10-13 01:43:19 UTC
Permalink
Post by Pádraig Brady
You only want to strdup once.
So you could use a static to track that as is done in the AIX case.
Ah, I see, the program name never changes.
Post by Pádraig Brady
You can call free(NULL), so the last 3 ifs are redundant.
Revised patch is tested and attached.


--Daniel
--
Daniel Richard G. || ***@iSKUNK.ORG
My ASCII-art .sig got a bad case of Times New Roman.
Pádraig Brady
2016-10-13 08:50:37 UTC
Permalink
Post by Daniel Richard G.
Post by Pádraig Brady
You only want to strdup once.
So you could use a static to track that as is done in the AIX case.
Ah, I see, the program name never changes.
Post by Pádraig Brady
You can call free(NULL), so the last 3 ifs are redundant.
Revised patch is tested and attached.
Pushed at:
http://git.svh.gnu.org/gitweb/?p=gnulib.git;a=commit;h=d75cbb3

thanks,
Pádraig
Jim Meyering
2016-10-13 20:10:07 UTC
Permalink
Post by Pádraig Brady
Post by Daniel Richard G.
Post by Pádraig Brady
You only want to strdup once.
So you could use a static to track that as is done in the AIX case.
Ah, I see, the program name never changes.
Post by Pádraig Brady
You can call free(NULL), so the last 3 ifs are redundant.
Revised patch is tested and attached.
http://git.svh.gnu.org/gitweb/?p=gnulib.git;a=commit;h=d75cbb3
Thanks.
This will be pulled into grep soon.
Jim Meyering
2016-10-14 21:14:24 UTC
Permalink
Post by Jim Meyering
Post by Pádraig Brady
Post by Daniel Richard G.
Post by Pádraig Brady
You only want to strdup once.
So you could use a static to track that as is done in the AIX case.
Ah, I see, the program name never changes.
Post by Pádraig Brady
You can call free(NULL), so the last 3 ifs are redundant.
Revised patch is tested and attached.
http://git.svh.gnu.org/gitweb/?p=gnulib.git;a=commit;h=d75cbb3
Thanks.
This will be pulled into grep soon.
I've pushed this additional fix:

Loading...