LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2020, 05:12 AM   #16
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,818

Rep: Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870

see man proc:
Code:
 /proc/[pid]/environ
              This file contains the initial environment that was set when
              the currently executing program was started via execve(2).
You will not be able to see it anywhere, but inside the process. So you need to work inside the given shell. One way to modify prompt do the diff, another one could be to give a signal from outside and a signal handler will do the diff.
But anyway it is still not a good approach.
 
Old 05-28-2020, 05:18 AM   #17
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,927

Rep: Reputation: 674Reputation: 674Reputation: 674Reputation: 674Reputation: 674Reputation: 674
Quote:
Originally Posted by Skaperen View Post
got a solution. save the shell PID in a file [echo $$ >shell.pid]. cron a script that gets the PID from that file [if it does not exist, just quietly exit]. then it grabs the environment variables and saves them [tr '\0' '\012' </proc/${pid}/environ|grep =|sort >/home/phil/$(date +%Y_%m%d_%H%M)] in a unique file. then i can scan them later. the next version would look for those names and if it sees them, send me a message.
You are using the contents of /proc/<pid>/environ to determine the ENVIRONMENT variables. However, a quick websearch brings up a few results - e.g., this one and this one, too - that state that this will only display the initial ENVIRONMENT when the process was started. I could not find any official documentation that support that statements, but they sound plausible.

How about using a subshell as a background process?
E.g.,
Code:
fun() { (while (( 1 ));do sleep 5; echo "Do what needs to be done (printenv, maybe?) here.";done)& }
Save it to a file, adjust the timer as you see fit and source it where you need it.

EDIT:
While I was posting pan64 also posted with reference to official documentation. However, here is what I get from man 5 proc (Slackware64 14.2, kernel 4.4.217):
Code:
       /proc/[pid]/environ
              This file contains the environment for the process.  The entries
              are separated by null bytes ('\0'), and there may be a null byte
              at  the  end.   Thus, to print out the environment of process 1,
              you would do:

                  $ strings /proc/1/environ

Last edited by crts; 05-28-2020 at 11:44 PM.
 
1 members found this post helpful.
Old 05-28-2020, 05:21 AM   #18
GazL
LQ Guru
 
Registered: May 2008
Distribution: CRUX
Posts: 5,508
Blog Entries: 14

Rep: Reputation: 3343Reputation: 3343Reputation: 3343Reputation: 3343Reputation: 3343Reputation: 3343Reputation: 3343Reputation: 3343Reputation: 3343Reputation: 3343Reputation: 3343
just add the suspect variable to your PS1 string. You'll soon notice when it shows up.
 
Old 05-28-2020, 07:14 AM   #19
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,358

Rep: Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452
Quote:
Originally Posted by rnturn View Post
You could kick the script off, passing your PID to it, and run it in the background, via "at", via nohup (maybe) and grab the environment out of /proc/<PID>/environ. However, I tried that and saw no differences after making a change to the environment (see other reply: #14).
Yes, the environment is loaded with defaults when a process starts, but after that every process has an environment that is (or can be) unique to that process. If you want to monitor the environment for a process the monitoring (or exporting to a file) must be done FROM or UNDER that process.

This might be better solved if the OP published here the environment variables that appear or change that are of concern, so we can see if any of us have run into them before.
 
1 members found this post helpful.
Old 05-28-2020, 09:23 PM   #20
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386
Posts: 2,106

Original Poster
Blog Entries: 20

Rep: Reputation: 150Reputation: 150
you guys are right. i made a change and it didn't show up in /proc. that means the kernel must be storing it or there is some residual copy in the process. either way, that seems to be a waste. but at least the latter can be swapped out.
 
Old 05-28-2020, 09:26 PM   #21
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386
Posts: 2,106

Original Poster
Blog Entries: 20

Rep: Reputation: 150Reputation: 150
i will not be publishing the names or values as they resemble some private information.
 
Old 05-28-2020, 09:35 PM   #22
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386
Posts: 2,106

Original Poster
Blog Entries: 20

Rep: Reputation: 150Reputation: 150
even the names are not the same each time. it would be like gibberish except that it looks like it was taken from some private data. if it were written to files, i can understand some goofy or buggy apps doing it. that it sets environment variables in the shell just makes no sense. ordinary processes can't do that. i do have a small few shell functions that do (so i know how to set them inside the shell). but i haven't been using them.

i'm going to try the subshell suggestion.
 
Old 05-29-2020, 12:50 AM   #23
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,818

Rep: Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870
subshell won't work either. It is a different shell with its own environment. The environment is copied from the parent at the beginning, but will not (cannot) follow the changes, because it is another shell.
It is quite simple: all of the processes have their own environment, which is inherited at the start (but that can be also influenced). There is no any way to manipulate (even read) the environment of another process. That would be a serious security issue.
Just imagine if there was a possibility to access to those private information....
 
1 members found this post helpful.
Old 05-29-2020, 03:28 AM   #24
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,927

Rep: Reputation: 674Reputation: 674Reputation: 674Reputation: 674Reputation: 674Reputation: 674
Quote:
Originally Posted by pan64 View Post
subshell won't work either.
You are right, of course, although we could utilize traps, as has been suggested by ntubski. That is a bit tricky, however, if the script contains sleep instructions. Since OP mentioned that it is a long running script there are probably sleep instructions.
This is problematic:
Code:
trap doit SIGUSR2

doit() {
    echo "USER: $USER"
}

fun() {
(
    while ((1));do
        kill -SIGUSR2 $$
        sleep 1
    done
) &
}

fun

sleep 10

USER=VOID

sleep 10
The signal from the subshell will not be processed as long as the shell is "sleeping". A websearch may yield results like:
Code:
sleep 10 & wait $!
However, the wait will be interrupted by the signal and the script continues, which means the shell did not sleep for 10s, which may be a problem. A possible compromise to keep sleep timers accurate and process a signal in a timely fashion might be the following:
Code:
trap doit SIGUSR2

doit() {
    echo "USER: $USER"
}

fun() {
(
    while ((1));do
        kill -SIGUSR2 $$
        sleep 1
    done
) &
}

pseudo_wait() {
    local -i count=$1
    while (( count-- > 0 ));do
        sleep 1
    done
}

fun

pseudo_wait 10

USER=VOID

pseudo_wait 10
It is not pretty but as I understand it, this is purely for debugging purposes, so it does not have to be pretty.

@OP you can, of course, use an external process to send the signal as well but you would have to find out the script's pid, e.g., via ps command. In that case you do not need the subshell. If you want to use the pseudo_wait() function is up to you. If it is acceptable to you to wait until a sleep finishes before a signal is processed then I guess you can do without pseudo_wait(), only you can make that decision.

Last edited by crts; 05-29-2020 at 03:32 AM.
 
Old 05-29-2020, 06:45 AM   #25
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 3,358

Rep: Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452Reputation: 1452
One thing that can set environment variables are batch executions run under the same session process. Are you running any scripts or programs that modify the environment?
 
Old 05-29-2020, 05:12 PM   #26
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386
Posts: 2,106

Original Poster
Blog Entries: 20

Rep: Reputation: 150Reputation: 150
> Are you running any scripts or programs that modify the environment?

scripts and programs in separate processes won't affect the shell. but functions in the shell can and there are a some that do change environment variables, such as one intended to add a directory to PATH.

traps do sound interesting, now. a process can be run to send a signal every hour. the sleeping would be here, not in the shell. that would trigger the trap which can simply write the environment to a file with a timestamped name. that process that sent the signal can wait a minute then check the files with variables and compare the last 2. if it sees the relevant difference it would then output to the terminal.

Code:
trap envout SIGUSR2

envout(){
    env >$(exec date +%Y%m%d%H.env)
}

Last edited by Skaperen; 05-29-2020 at 06:00 PM.
 
Old 05-30-2020, 03:29 AM   #27
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,818

Rep: Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870
This is a security hole again. Your sensitive data will be put into a file which can be read by [almost] anyone.
 
Old 05-30-2020, 11:57 AM   #28
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386
Posts: 2,106

Original Poster
Blog Entries: 20

Rep: Reputation: 150Reputation: 150
> This is a security hole again. Your sensitive data will be put into a file which can be read by [almost] anyone.

putting sensitive data anywhere is a security risk. if you put it in a safer place, why was it in a less safe, first?

i suspect, but do not know for sure, that somehow, these variables got the data from some files. it may be that this is just random gibberish that happens to resemble sensitive data i recognize. i want to know what might be setting this. something has be doing it.

tell me a better place. i will certainly be storing it as safely as a file can be without encryption (which most programs already do). the umask will be set tight as i always do. there are no other users of this laptop. this is a transient activity which will be removed when done. what alternative can you suggest?
 
Old 05-30-2020, 12:12 PM   #29
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,818

Rep: Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870Reputation: 4870
if you have a problem with a specific environment variable:
you can add something to PS1 (prompt) to check if it was defined and send a mail about it (including the last command entered) or do what you wish.

If setting an environment variable is a problem you need to use subshell or something in your functions, that will solve it.
 
Old 05-30-2020, 10:14 PM   #30
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386
Posts: 2,106

Original Poster
Blog Entries: 20

Rep: Reputation: 150Reputation: 150
Quote:
Originally Posted by pan64 View Post
if you have a problem with a specific environment variable:
you can add something to PS1 (prompt) to check if it was defined and send a mail about it (including the last command entered) or do what you wish.

If setting an environment variable is a problem you need to use subshell or something in your functions, that will solve it.
if you had actually read and understood the posts then you would know that it is not one specific environment variable, but random, often gibberish names that frequently have portions of personal or confidential data in them. since it requires some specific action in the shell to add environment variables, either there is a bash bug affecting only one user, or something has infected a bash function. i'm trying to narrow down when it happens and see if it's any command i'm doing.

how would a subshell do this? setting an environment variable in a subshell does not change it i the main shell process. try it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] shell script to check availability of a file for 1 hour , after one hour if file not there , it will echo "Time Out" subir Linux - Newbie 4 03-10-2020 07:05 AM
Schedule php script every hour, half an hour, and 15 minutes manolakis Programming 5 08-22-2018 07:43 AM
[SOLVED] How to: Script run every 1 hour between 18h and 7h every day heathcliffz Linux - General 5 12-08-2015 09:02 AM
Cron job issue - every hour works, but specific hour fails lunarleviathan Linux - Newbie 6 11-20-2009 12:19 AM
Aergh. X dies on the hour, every hour l00zer Linux - Software 4 06-07-2005 10:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration