XY problem?
Do you really need to know what came on the left hand side of "perl" on the command line, or is your goal to determine whether you're running as a particular user, with certain permissions, or do you need to know if you're running with different real and effective user or group ID's?
For what it's worth, when operating under 'sudo', the environment variable $ENV{USER} will contain the login of the person who invoked sudo, and $ENV{SUDO_USER} will contain the login of the sudo target user (typically root, unless configured for some other user or otherwise specified). Sudo does set both real and effective uid and gid to the target user/group, so it wouldn't do any good detecting a mismatch there. But by detecting the USER adn SUDO_USER mismatch, you can detect sudo's effects.
So you could just check for the existence of $ENV{SUDO_USER}, or possibly, for $ENV{USER} and $ENV{SUDO_USER} to disagree. If either of those cases occur, you're probably running under sudo.
('probably', unless someone went to the trouble of tampering with those variables -- though I can't think of any reason why someone might.)
| [reply] [Watch: Dir/Any] [d/l] [select] |
Thanks very much, I now know what I need to do.
| [reply] [Watch: Dir/Any] |
Is there any variable which stores the parameter passed before the actual script name
No. That information is not available anywhere within your perl script.
When you type the command line sudo perl myscript.pl the following steps occur:
- The command shell starts the sudo program passing 'sudo' as it first argument, 'perl' as the second, 'myscript.pl' as the third.
- The sudo program then starts perl passing 'perl' as it first argument and 'myscript.pl' as the second.
Perl is never aware of the original first argument.
However, there are a couple of approaches you might use to discover it:
- If your OS provides an api that allows you to screen-scrape the terminal session, that might work.
For example, on windows you could use Win32::Console::ReadRect() to grab the text from the terminal window. You could then search it for the line containing your script name.
- Alternatively, you could use getppid to obtain the pid of your parent process.
With that, most OSs provide a mechanism for finding out the name of the process with that pid.
On windows you might use Win32::Process::Info, or grep the output of the tasklist command.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
| [reply] [Watch: Dir/Any] [d/l] [select] |
Thanks very much for your response.
| [reply] [Watch: Dir/Any] |