Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux, Eclipse CDT run as su

Tags:

c++

eclipse

I am using Eclipse CDT to write an application. The application uses a third part API, which requires to run as sudo, in order to open raw sockets, etc.

In command line, I can run the program something like

su
./program

But in Eclipse CDT environment, it won't work if I press Ctrl + F11 (Run->Run Last Launched), I guess the reason is that my Linux GUI login is not su.

Is there anyway that I can run as su (with su password) in Eclipse CDT?

Thanks.

like image 937
2607 Avatar asked Mar 21 '12 20:03

2607


2 Answers

Some of the options you have:

  1. Run Eclipse as root. (Not a very good idea, imho, but the simplest one)

  2. Temporarily enable your user to use that library. (Could be messy)

  3. Create a new run configuration and make a script to run your executable.(You've to enter the password everytime).

  4. (@Others, feel free to add more.)

like image 185
manasij7479 Avatar answered Nov 15 '22 19:11

manasij7479


You can create a run configuration in which you use sudo to run your application. Since sudo doesn't have access to a terminal if you launch it this way, it requires setting the SUDO_ASKPASS environment variable in your launch configuration. The steps are as follows:

  1. Creating a new Run configuration (for the sake of completeness):

    1. In eclipse, right click your executable (i.e. the result of the build of your application) to open the context menu
    2. In the context menu, go to Run as -> Run configurations ...
    3. In the Run configurations window that appears, right click C/C++ Application and click New.
  2. Modifying the Run configuration

    1. In the Run configuration's Main tab, replace the C/C++ Application field by
      /usr/bin/sudo.
    2. Go to the Arguments tab, and enter the path to your executable, e.g.
      ./bin/my-executable. Note: sudo's working directory is the eclipse project, so the path should be relative to that.
    3. If your executable requires any commandline arguments, add those, e.g.
      ./bin/my-executable arg1 arg2
  3. Adding a way for sudo to ask for your password.

    1. This is the most tricky part. Since sudo does not have a terminal (tty) at its disposal when run from the (eclipse) gui, we need to provide it with a program that can obtain the password for it, i.e. an askpass program. For more info, see this stackoverflow answer.
    2. On my system (Ubuntu 15.04), the package ssh-askpass-gnome provides an askpass program, as I found out by running dpkg --get-selections | grep askpass. Since that still didn't give me the executable name, I brute-forced the search by running
      sudo find -name *askpass*. Anyway, if no such utility is installed, search for it using your favourite package manager.
    3. Once we've installed and / or located an askpass program, we can continue creating our launch configuration in eclipse; continue from step 2.3
    4. In the Run configuration window, select the Environment tab and click new.
    5. In the dialog that pops up, enter SUDO_ASKPASS as name and the full path to the askpass program as value, e.g. /usr/bin/ssh-askpass.
    6. Press Apply to save our changes.
  4. Executing the launch configuration
    1. In the Run configurations window, press Run to launch our executable using the newly created launch configuration.
    2. A pop-up window for entering the sudo password will appear.
    3. After succesful password entry, our executable will run with root priviliges.
like image 2
tjalling Avatar answered Nov 15 '22 20:11

tjalling