Run a C or C++ program in CLion as root
Asked Answered
W

6

14

I'm trying to run a program written in C/C++ by using CLion 2022.3.2 as superuser. Anyone here knows how to run it as superuser? I'm using CMake.

Welldone answered 21/1, 2016 at 15:31 Comment(1)
See if this helps: intellij-support.jetbrains.com/hc/en-us/community/posts/…Balthasar
M
4

I solved this by remote debugging on local machine. I run gdbserver with root privileges and connected to it from CLion.

To start gdbserver sudo gdbserver :port myapp, configure remote GDB debug with port

Mariammarian answered 23/12, 2016 at 16:20 Comment(0)
A
2

If you are asking to run the debugger as sudo/administrator, I had the same issue. On Linux you can open CLion as sudo and gdb will not have root permission issues running an executable:

sudo ./clion.sh

I would guess that you could right click in Windows and "Run as administrator" to get the same result.

There is a feature request into JetBrains to run the debugger as root that could use more up voting.

Agronomy answered 5/5, 2016 at 16:35 Comment(4)
This doesn't work, Clion crashes before launch when you run as rootWilen
On Windows or Linux? Doesn't surprise me either way. I gave up on Clion soon after I wrote that. I like Android Studio, but can't justify paying for Clion with the problems I had.Agronomy
Linux. Seems to be a Ubuntu bug though from their forumsWilen
I do sudo -i then clion.sh -- haven't had an issue with it crashing. Ubuntu 16.04 / 18.04Mcneill
A
2

This was already implemented on CLion, starting 2020.3:

https://www.jetbrains.com/help/clion/debug-as-root.html

Algerian answered 26/12, 2020 at 1:15 Comment(0)
T
1

A workaround is:

From https://www.jetbrains.com/help/clion/attaching-to-local-process.html#prereq-ubuntu

If you are using CLion on Ubuntu (or probably, on some other Linux distribution), upon the first attempt to attach to the local process you can get the ptrace: Operation not permitted. error message. To disable it and enable attach to the local process feature, do the following:

To disable this restriction temporarily, enter the command:

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

To disable this restriction permanently, open the file /etc/sysctl.d/10-ptrace.conf for editing and change the line kernel.yama.ptrace_scope = 1 to kernel.yama.ptrace_scope = 0. To apply the changes, enter: sudo service procps restart or restart your system, at your choice.

Timber answered 12/2, 2018 at 13:12 Comment(0)
L
0

TL;DR

One liner copy pasta. Paste this command into a terminal window, run your process, come back to the terminal window and type your password.

pkttyagent --process $(ps -o ppid= $(pgrep -f clion) | sort -n | tail -n1)

Solution

Find CLion's top-most (parent of all open projects) process ID (PID) whose PPID is 1.

$ ps -f $(pgrep -f clion)
UID          PID    PPID  C STIME TTY      STAT   TIME CMD
thisisme  348892       1  7 Jun05 ?        Sl   1041:34 /opt/clion...

Launch the pkttyagent with this process as its argument.

$ pkttyagent --process 348892

Launch your process with CLion's run configuration set to run the process as root.

Run/Debug Configurations

The pkexec process will connect to the tty agent running in your console and prompt you for the password. Enter your password in the console window and your process will continue to run as root in CLion.

0 thisisme@iamhere:~/indir$ pkttyagent --process 348892
==== AUTHENTICATING FOR org.freedesktop.policykit.exec ===
Authentication is needed to run `/opt/clion-2022.2.4/jbr/bin/java' as the super user
Authenticating as: thisisme,,, (thisisme)
Password: 
==== AUTHENTICATION COMPLETE ===

Discovering This Solution

Found these interesting bits of info while using Ubuntu 22.04 and Mint 21 vanessa.

Assuming you are not the root user, notice that when you run something like systemctl restart polkit, you are prompted to enter a password. Before entering your password press ctrl-c, then type systemctl status polkit and you'll see that a pkttyagent process is launched.

Similarly, in the status output when running pkexec ls in another terminal, you'll see that polkit.service status prints the executable and arguments like pkexec ls. This was the clue that we should launch the agent in another process instead of invoking pkexec directly. It seems like CLion is launching pkexec without first starting an agent, so we need to wire up the agent for CLion ourselves.

Reading the man page on the pkttyagent, it takes a PID which listens for elevation requests. All you gotta do is start an agent, and elevation requests from that PID show up in your console.

Note on Build Systems

It does not matter if CMake or Makefiles are used to build the project; the set up steps are the same.

Lapp answered 14/6, 2023 at 16:35 Comment(0)
C
-1

You can create a bash script to help you. For example, run.sh:

#!/usr/bin/env bash
echo $LOGIN_PASSWD | sudo -S yourExecutable --with=some-arguments

Don't forget to chmod a+x run.sh, and export the password of current account as an environment variable in Run/Debug Configurations.

Cocktail answered 21/6, 2018 at 17:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.