Find the answer to your Linux question:
Results 1 to 5 of 5
I'm using virtual file-system in /proc/ to print out current working directory (CWD) : Code: ls /proc/$PID/cwd # part of an argument to printf (full printf below) Now, some processes ...
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    15

    Question [SOLVED] Any way to know beforehand if SUDO is (going to be) needed?

    I'm using virtual file-system in /proc/ to print out current working directory (CWD):
    Code:
    ls /proc/$PID/cwd # part of an argument to printf (full printf below)
    Now, some processes complain about SUDO privileges (e.g. init: ls: cannot access /proc/1/cwd: Permission denied).
    Is it possible to know beforehand if action/command will require SUDO? For example, is there a "maximum PID number", above which no process needs SUDO? Is the following snippet (that checks if user ran the script as SUDO) good enough for behorand-knowledge?
    Code:
    if [ $UID -eq 0 ] ; then
    # do something



    BTW, in following printf snippet with 2 arguments,
    Code:
    printf "%15s  %s\n" CWD: $((ls /proc/$PID/cwd) | tr '\n' ' ')
    if ls returns error cannot open directory /proc/1/fd: Permission denied it outputs this error before "CWD:", like so:
    Code:
    ls: cannot access /proc/1/cwd: Permission denied
                CWD:
    Why this happens?
    Last edited by courteous; 12-12-2010 at 06:35 AM.

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    There is no way to know if a program requires sudo access before the program is actually run. This is because you do not know what files will be opened by a process until they are actually opened.

    As for your question about the ls error output:

    $(...) uses the STDOUT (fd 1) of the process that you are running. However, error output is usually printed to STDERR (fd 2) instead. Therefore, error output does not get picked up by $(...).

    If you want the error output, you can run:
    Code:
    ls 2>&1
    which redirects STDERR to point to STDOUT (and the $(...) will pick it up). If you always want to ignore the error output, you can run:
    Code:
    ls 2> /dev/null
    which will cause the error output to never appear.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Dec 2010
    Posts
    15
    Cabhan, thank you for the reply.
    Quote Originally Posted by Cabhan View Post
    There is no way to know if a program requires sudo access before the program is actually run. This is because you do not know what files will be opened by a process until they are actually opened.
    And there is also no "max. PID number" above which there is no SUDO required? Anything else I could try?

    Quote Originally Posted by Cabhan View Post
    If you always want to ignore the error output, you can run:
    Code:
    ls 2> /dev/null
    which will cause the error output to never appear.
    Is it possible to check whether there was error ignored (with 2> /dev/null redirect) and, if it was, echo/print something else (say, "You need sudo privilege.")?

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    For your first question, there is no max PID or anything. PIDs are assigned randomly, as I recall, and again, there is no way to tell when a process is first spawned what files it will attempt to access.

    "sudo access" is not a thing. All that sudo does is allow you to run a process as a user other than yourself. And there are no user-specific PIDs.

    The way I see it, there are two possibilities:

    1) Your program allows the user to tell it what to do in some instance, and the user may choose something that they are not allowed to do. In this instance, I believe that throwing an error is the correct behavior.

    2) Your program requires access to information that is only root-accessible. In this instance, you know that you will require root privileges, and checking $UID is appropriate to ensure that you are running as root.


    As for your question about checking if an error happened, you can do something like the following:
    Code:
    ls_output=$(ls 2> /dev/null)
    
    if [ $? != 0 ]; then
        echo "ls failed to execute." >&2
    fi
    Here we execute ls, and save its output in a variable. We then check the $? variable, which contains the exit status of the last command. "0" means that the program executed successfully, while anything else indicates a failure of some sort. In this way, we can check whether the command worked.
    DISTRO=Arch
    Registered Linux User #388732

  5. #5
    Just Joined!
    Join Date
    Dec 2010
    Posts
    15
    Thank you very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...