Changing to Root & Back within FB

Linux specific questions.
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Changing to Root & Back within FB

Post by Dinosaur »

Hi All

Is that at all possible ?
I have a command that needs to be root to execute, but two days of reading and searching
and still no further.

I thought the Shell command would do it as per examples but it fails.

Code: Select all

Shell "echo Mypassword  | sudo -S su - root"
and many variations of the above


Has anyone done this in Linux ?

Regards
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Changing to Root & Back within FB

Post by badidea »

On Ubuntu, this seems to work:

Code: Select all

shell "echo " & myPassWord & " | sudo -S ls"
On Ubuntu, a normal user can have superuser rights (for some time). The actual root account is not used.
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Changing to Root & Back within FB

Post by Dinosaur »

Hi All

badidea , thanks for that.
I am running Mint 18.3 which is ubuntu based, however I have not figured out how to easily
do that.
Using your suggestion I did:

Code: Select all

Dim as String myPassword = "blahblah"
shell "echo " & myPassword & " | sudo -S ls"
But I got what looks like a file listing
-[sudo] password for barfresh: IA3125.bi Test1.bas Threading Threading3 Threading4 .bas
Speech Test2 Threading1 Threading3 Threading5
Speech.bas Test2.bas Threading1 Threading3 .bas Threading5
Test Test.bas Threading1 .bas Threading4 Threading5 .bas
Test1 Threading Threading2 .bas Threading4 Threading .bas
Sad thing is that you can google solutions for one flavour of Linux that won't work on any other.
Didn't think that was the mantra for Linux development as a competitor to Windows.
Ok, rant over and done with.

Regards
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Changing to Root & Back within FB

Post by badidea »

Dinosaur wrote:But I got what looks like a file listing
It is: "ls". But the file listing is done with 'administrative rights'
Not sure what you actually want to do, but "ls" can be replaced with the actual program or command you want to do.
To run administrative tasks in Linux, you must have root (also known as superuser) access. Having a separate root account is common in most Linux distributions, but Ubuntu disables root by default. This prevents users from making mistakes and keeps the system safe from intruders. To run commands that require root access, use sudo
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Changing to Root & Back within FB

Post by Dinosaur »

Hi All

badidea I am trying to login as root to be able to do the following command

Code: Select all

Print "7;";pthread_setschedparam(*pThread1, SCHED_FIFO, @params)
But using the following:

Code: Select all

shell "echo " & myPassword & " | sudo su "
the program appears to jump over this instruction.
The full code is:

Code: Select all

pThread1 = ThreadCreate(@mythread1, 0)
pThread2 = ThreadCreate(@mythread2, 0)

print "1;";pthread_getname_np(*pThread1, @threadName, 16)
print "2;";threadName
print "3;";pthread_setname_np(*pThread1, !"ThreadUSB\0")
print "4;";pthread_getname_np(*pThread1, @threadName, 16)
print "5;";threadName


params.sched_priority = sched_get_priority_max(SCHED_FIFO)
Print "6;";params.sched_priority
Dim as String myPassword = "blahblah"
''Now you have to be Root otherwise next line fails.
shell "echo " & myPassword & " | sudo su "
Print "7;";pthread_setschedparam(*pThread1, SCHED_FIFO, @params)
but the output goes straight to:
[sudo] password for barfresh: *-**-*-**-*-*-*--**-***-*-*-*-**---***-*---**----***--*----**-*---*--**-*-*-*--*-*-**-*-*-*-***-*--*-*
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Changing to Root & Back within FB

Post by badidea »

Dinosaur wrote:The full code is:...
That does not seem like the full code. Cannot run it, parts missing.

If your program needs superuser rights, why not run your program as superuser?
'sudo ./myprogram'
Then no need to store your password in a .bas file.

BTW: I have been using linux for ~8 years now, but I am certainly not an expert.
Last edited by badidea on Oct 19, 2018 20:21, edited 1 time in total.
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Changing to Root & Back within FB

Post by Dinosaur »

Hi All

Sorry, full code:

Code: Select all

'https://manpages.courier-mta.org/htmlman3/pthread_setname_np.3.html
#include "crt/pthread.bi"
Declare sub mythread1(params as any ptr)
Declare sub mythread2(params as any ptr)
Declare sub print_dots(ByRef char as string)
Dim Shared As Sched_param Params
Dim Shared As pthread_t Ptr pThread1 
Dim Shared As pthread_t Ptr pThread2
Dim Shared As zstring * 16 ThreadName


Sub print_dots(ByRef char As String)
    For i As Integer = 0 To 2999
        Print char;
        Sleep CInt(Rnd() * 100), 1
    Next
End Sub
Sub mythread1(params As Any Ptr)
    print_dots("*")
End Sub
Sub mythread2(params As Any Ptr)
    print_dots("-")
End Sub

Randomize(Timer())
pThread1 = ThreadCreate(@mythread1, 0)
pThread2 = ThreadCreate(@mythread2, 0)

print "1;";pthread_getname_np(*pThread1, @threadName, 16)
print "2;";threadName
print "3;";pthread_setname_np(*pThread1, !"ThreadUSB\0")
print "4;";pthread_getname_np(*pThread1, @threadName, 16)
print "5;";threadName


params.sched_priority = sched_get_priority_max(SCHED_FIFO)
Print "6;";params.sched_priority
Dim as String myPassword = "blahblah"
''Now you have to be Root otherwise next line fails.
shell "echo " & myPassword & " | sudo su "
Print "7;";pthread_setschedparam(*pThread1, SCHED_FIFO, @params)

print_dots(".")
ThreadWait(pThread1)
ThreadWait(pThread2)
Print
Sleep
I can't run the program as su because that would mean the operator of my machines would have to know/type the password.
The machines only have a touch screen with an icon for them to click on.

Regards
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Changing to Root & Back within FB

Post by badidea »

You can make 'root' the 'owner' of the file / program and set the 'SUID/GUID bit'.
This way the program is executed a superuser and can be started by a normal user.
I have never tried this myself however.

Some heavy reading:
https://unix.stackexchange.com/question ... work#79401
https://en.wikipedia.org/wiki/Setuid

Note: There can be some security issues in doing this way.

The way you try it now is not going to work I think. You cannot become a superuser halfway in in your code.
An alternative could be to call a different program or linux comand from freeBasic as superuser to change the priorities of the threads. But this sounds complicated to me, as you then have to tell the external command somehow which processes you want to change.
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Changing to Root & Back within FB

Post by Dinosaur »

Hi All

Many thanks for your help, I am trying a few things and when done will post here.
Some things I am trying are:
Using the "Expect" scripting tool with the following.

Code: Select all

#!/expect

set timeout 60

spawn sudo ./Test

expect "[sudo] password for barfresh:"

send "Blahblah";

interact
That runs the program as su BUT only if I type each line at the Terminal, however I get "There was an error creating the child process for this terminal"
when I try to run it a script. ?? There are thousands of posts about this error on the web.

The other way may be to read the PID number of the Thread and then running another program to change the priority.
Either way I don't give up easily.

Regards
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Changing to Root & Back within FB

Post by caseih »

So the expect script gives you this error?
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Changing to Root & Back within FB

Post by Dinosaur »

Hi All

Yes it does, it is a very difficult syntax to get right.
I have a Test program that I want to run as superuser.
At the moment I am breaking it up into two to avoid that error, but now I get new errors.
userswap

Code: Select all

#!/bin/bash

expect -f swapcmnds.txt
no error

swapcmnds.txt

Code: Select all

spawn sudo su ./Test

expect {[sudo] password for barfresh:}

send "Blahblah";

interact

./Test
Then I run ./userswap in the terminal, which now at least executes the expect command and opens the swapcmnds.txt file.
However the response is
barfresh@jvp ~/projects/ThreadTest $ ./userswap
spawn sudo su ./Test
[sudo] password for barfresh: invalid command name "send "$Blahblah\r""
while executing
"{send "$Blahblah\r"}"
(file "swapcmnds.txt" line 5)
There are 1000 different opinions out there on how to format the password.???

Regards
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Changing to Root & Back within FB

Post by Dinosaur »

Hi All

Another way advocated for running a program as superuser without a password:
Create or edit the file in sudoers.d which in my case is "mintupdate"
I added the line:
ALL ALL = NOPASSWD:/Projects/ThreadTest/Test
rebooted, yet no luck.

Then I changed the ownership and group to root, and then ran the file without being in root.
???? The file ran but didn't ask for a password.So, the above worked.

Yet the function that relies on being a superuser failed.
Then I ran the program again, but this time the terminal was in root.
The program worked correctly.

The only other odd thing is that I have set the file as executable, but clicking on it does nothing.
To run it, I have to type ./Test in the terminal.

So, it appears that making the program root without password does not allow a priority statement in the program to work.

Code: Select all

''Now you have to be Root otherwise next line fails.
Print "7;";pthread_setschedparam(*pThread1, SCHED_FIFO, @params)
It means that I have to enter into root before calling my program.
Not happy about that.

Regards
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Changing to Root & Back within FB

Post by badidea »

I know nothing about the expect script, but it only seems to complicate things.

I changed the testing code. Irrelevant stuff out. Added error checking.
I also added a graphics screen, because I don't how to start a terminal screen when starting the program from desktop.

Code: Select all

#Include "crt/pthread.bi"

Sub mythread1(params As Any Ptr)
	Print "thread1: start"
	Sleep 3000, 1
	Print "thread1: end"
End Sub

Dim As Sched_param Params
Dim As pthread_t Ptr pThread1
Dim As Integer result

screenres 800, 600, 32
width 800 \ 8, 600 \ 16

'start thread
pThread1 = Threadcreate(@mythread1, 0)

'set thread name
result = pthread_setname_np(*pThread1, !"ThreadUSB\0")
If result = 0 Then
	Print "pthread_setname_np: OK (" + Str(result) + ")"
Else
	Print "pthread_setname_np: FAIL (" + Str(result) + ")"
End If

'set thread priority
params.sched_priority = sched_get_priority_max(SCHED_FIFO)
result = pthread_setschedparam(*pThread1, SCHED_FIFO, @params)
If result = 0 Then
	Print "pthread_setschedparam: OK (" + Str(result) + ")"
Else
	Print "pthread_setschedparam: FAIL (" + Str(result) + ")"
End If

Threadwait(pThread1)
Print "end"
I named the program 'dino', with:
"-rwxrwxr-x 1 badidea badidea 24296 Oct 20 11:14 dino" pthread_setschedparam: FAIL (1)
"-rwxrwxr-x 1 root root 24296 Oct 20 11:14 dino" pthread_setschedparam: FAIL (1)
"-rwsrwxr-x 1 root root 24296 Oct 20 11:14 dino" pthread_setschedparam: OK (0)
Note the SUID bit in last case.
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Changing to Root & Back within FB

Post by Dinosaur »

Hi All

You have done well badidea.
Took me a while to get the method right, but got it in the end.
There is no need to edit the sudoers.d file with the NOPASSWD line, so I deleted it.

FYI:
To write and test the program, I simply open the editor (Poseidon in my case) as root.
After compiling I can run from within Poseidon and all is good.
By opening a Terminal and using "htop" I can see the thread is in rt mode.
The generic "top" program does not show the rt.

Once happy with the program:
Open the folder as root and set ownership & group to root.
Then by opening a terminal (still in root) I used:
chmod u+s "ProgramName"

Quitting out of root and running the program as a normal user works (again proven by htop).

Many thanks for your help on this.

PS: To run from Terminal simply type ./ProgramName

Regards
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Changing to Root & Back within FB

Post by badidea »

Dinosaur wrote:You have done well badidea.
Thanks, with all this linux stuff I am another day behind on the game development competition. Luckily, this thread issue seems solved.
Post Reply