Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storing regex matching environment variables in the existing cmdine variable #221

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion atop.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@
#include "json.h"
#include "gpucom.h"

#define allflags "ab:cde:fghijklmnopqrstuvwxyz1ABCDEFGHIJ:KL:MNOP:QRSTUVWXYZ"
#define allflags "ab:cde:fghijklmnopqrstuvwxyz:1ABCDEFGHIJ:KL:MNOP:QRSTUVWXYZ"
#define MAXFL 64 /* maximum number of command-line flags */

/*
Expand All @@ -324,6 +324,9 @@ char calcpss = 0; /* boolean: read/calculate process PSS */
char getwchan = 0; /* boolean: obtain wchan string */
char rmspaces = 0; /* boolean: remove spaces from command */
/* name in case of parseable output */
char prependenv = 0; /* boolean: prepend selected */
/* environment variables to cmdline */
regex_t envregex;

unsigned short hertz;
unsigned int pidwidth;
Expand Down Expand Up @@ -619,6 +622,14 @@ main(int argc, char *argv[])
rmspaces = 1;
break;

case 'z': /* prepend regex matching environment variables */
if (regcomp(&envregex, optarg, REG_NOSUB|REG_EXTENDED)) {
printf("Invalid environment regular expression!");
prusage(argv[0]);
}
prependenv = 1;
break;

default: /* gather other flags */
flaglist[i++] = c;
}
Expand Down Expand Up @@ -1102,6 +1113,8 @@ prusage(char *myname)
MRMSPACES);
printf("\t -L alternate line length (default 80) in case of "
"non-screen output\n");
printf("\t -z prepend regex matching environment variables to "
"processes command line\n");

if (vis.show_usage)
(*vis.show_usage)();
Expand Down
5 changes: 4 additions & 1 deletion man/atop.1
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,10 @@ the total number of pages of the process/thread list are shown.
.B COMMAND-LINE
The full command line of the process (including arguments). If the length of
the command line exceeds the length of the screen line, the arrow
keys -> and <- can be used for horizontal scroll.
keys -> and <- can be used for horizontal scroll. The '-z <regex>' command
line option can be used to prepend matching environment variables to the
displayed command line. POSIX Extended Regular Expression syntax are used
(see regex(3)).
.br
Behind the verb `COMMAND-LINE' in the header line, the current page number
and the total number of pages of the process/thread list are shown.
Expand Down
37 changes: 34 additions & 3 deletions photoproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <regex.h>

#include "atop.h"
#include "photoproc.h"
Expand Down Expand Up @@ -73,6 +74,9 @@ void fillcgroupv2(struct cgroupv2vals *, char *, char *, int);
int readcgroupv2(char *, char *, char *, int, long []);
static void wipecgroupv2(void);

extern char prependenv;
extern regex_t envregex;

unsigned long
photoproc(struct tstat *tasklist, int maxtask)
{
Expand Down Expand Up @@ -711,21 +715,48 @@ procio(struct tstat *curtask)
static void
proccmd(struct tstat *curtask)
{
FILE *fp;
FILE *fp, *fpe;
register int i, nr;

memset(curtask->gen.cmdline, 0, CMDLEN+1);

ssize_t env_len = 0;

if ( prependenv && (fpe = fopen("environ", "r")) != NULL) {
register char *p = curtask->gen.cmdline;
/* we let getdelim handle the allocation */
char *line = NULL;
ssize_t nread;
size_t len = 0;
while ((nread = getdelim(&line, &len, '\0', fpe)) != -1) {
if (nread > 0 && !regexec(&envregex, line, 0, NULL, 0)) {
env_len += nread;

if (env_len >= CMDLEN) {
env_len -= nread;
break;
}

strcpy(p, line);
p += nread;
}
}
/* line has been (re)allocated within the first call to getlim */
/* even if the call fails, see getline(3) */
free(line);
fclose(fpe);
}

if ( (fp = fopen("cmdline", "r")) != NULL)
{
register char *p = curtask->gen.cmdline;

nr = fread(p, 1, CMDLEN, fp);
nr = fread(p+env_len, 1, CMDLEN-env_len, fp);
fclose(fp);

if (nr >= 0) /* anything read ? */
{
for (i=0; i < nr-1; i++, p++)
for (i=0; i < nr+env_len-1; i++, p++)
{
switch (*p)
{
Expand Down