Create a process with fork and launch a gui process , getting error No protocol specified

I have a service which invoke a process say cust, In cust i did fork and exec to run firefox or any gui application. I am using path for firefox /usr/bin/X11/firefox.
When i run my application and invoke fork to run firefox, i am getting below errors. 1.No protocol specified 2.Error: cannot open display: :0.0
With different forum readings i found that X11 environment is not set. I tried to set environmental variables in service,process but nothing is help me.
i tried to set XAUTHORITY=$HOME/.Xauthority and DISPLAY=:0.0 variables.
I am using OpenSuSE 12.3 with GNOME.
Service and application cust running as a root user.
Please help to solve this issue.
Thanks in advance.

And how do you run it as root?
Normally programs run as root are not allowed to open windows on your user’s X display.

Try to use gnomesu to start your program as root, that should take care of the environment and authentication.

PS: See also this thread: https://forums.opensuse.org/english/get-technical-help-here/applications/405624-sudo-doesnt-open-x-programs.html (especially page 4)

Basically you would have to grant root access to your display with “xhost” and you need to pass the “DISPLAY” environment variable so the process knows where to open the window.

But a word of warning here: it is really not the best idea to run firefox as root…:wink:

Thanks for valuable information.

My service is running as a root.
if i want to do fork and then do exec to launch firefox application for a logged in user(setuid() can use) or root,
should i use execl(“gnomesu”, /usr/bin/X11/firefox",NULL ); or first call “xhost +local:root” later require application.
Since i am not using shell to execute this commands, I am doing with programmatic way.

Another thing i understood, make any non-root user powerful so that not ask for password(using file /etc/sudoers).

Still i did not get clear picture , how can i use these in my application.

I got it that your service runs as root. But how do you start it, so it runs as root?
What I proposed was to run your service by using “gnomesu”. Calling execl(“gnomesu”, /usr/bin/X11/firefox",NULL ); in your service won’t help you in any way, since the service is already running as root, not as user.

or first call “xhost +local:root” later require application.

You have to run xhost before that application tries to open a window. And you have to run xhost on the user’s X display with the user’s rights (not root!), so no use in calling it from inside your service.
And it’s “xhost +si:localuser:root” actually. “xhost +local:root” would grant access to ALL users on the local machine… (well, should be no problem if there aren’t any others :wink: )

Still i did not get clear picture , how can i use these in my application.

That’s a good question. Running the service with “gnomesu” should pass anything necessary to the service and later on through to that GUI application hopefully. (but I’m not sure, never tried that…)

I still don’t think it’s wise to run a GUI application from that root service (and especially not a web browser).
What are you trying to achieve exactly?

I guess if you run the GUI program as the currently logged in user and pass a correctly set DISPLAY environment variable, it should work (even without xhost). But how would you know which user is logged in to an X session currently?

On 2013-09-02 14:46, enggjiten wrote:

> My service is running as a root.
> if i want to do fork and then do exec to launch firefox application for
> a logged in user(setuid() can use) or root,

I don’t remember I’d ever seen a service open directly any type of
window or gui application. In Windows, yes, maybe, but not in Linux.

Typically you ask user, in the documentation, to open separately firefox
or whatever, after the service is started. A service does not display
anything, it is silent. Instead a service logs output on files or syslog.

On the other hand, there is a programming subforum here where you can
ask all kinds of programming questions. You may ask a moderator to move
this thread there - use the report button below and ask.


Cheers / Saludos,

Carlos E. R.
(from 12.3 x86_64 “Dartmouth” at Telcontar)

On Mon 02 Sep 2013 11:56:02 AM CDT, enggjiten wrote:

I have a service which invoke a process say cust, In cust i did fork and
exec to run firefox or any gui application. I am using path for firefox
/usr/bin/X11/firefox.
When i run my application and invoke fork to run firefox, i am getting
below errors. 1.No protocol specified 2.Error: cannot open display: :0.0
With different forum readings i found that X11 environment is not set. I
tried to set environmental variables in service,process but nothing is
help me.
i tried to set XAUTHORITY=$HOME/.Xauthority and DISPLAY=:0.0 variables.
I am using OpenSuSE 12.3 with GNOME.
Service and application cust running as a root user.
Please help to solve this issue.
Thanks in advance.

Hi
So does firefox need to be visible, or just running? I ask this because
I had a radius monitoring program that needed to run in a GUI (as root)
but no display, Xvfb came to the rescue here…

Your not seeing an MIT-MAGIC-COOKIE error when you try to run your
service?


Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
openSUSE 12.3 (x86_64) GNOME 3.8.4 Kernel 3.7.10-1.16-desktop
If you find this post helpful and are logged into the web interface,
show your appreciation and click on the star below…

I guess if you run the GUI program as the currently logged in user and pass a correctly set DISPLAY environment variable, it should work (even without xhost). But how would you know which user is logged in to an X session currently?[/QUOTE]

Let me explain you scenario.

  1. My service is just check for a process whether it is running or not, if some how it has stopped or exited service will revoke the process.

  2. Process is binary which is running many functionalists, one of that is run any binary. When i send binary name it should run it.

  3. firefox is just an example , i can pass wireshark or calc anything which is installed on desktop. For achieve this scenario i tried (in child mode): execlp(pX11Cmd,pX11Cmd,pArg,NULL); (pX11Cmd = /usr/bin/X11/firefox or /usr/bin/X11/wireshark).
    If i want to run netstat then output should come in some window. For window output i tried
    execl("/usr/bin/xterm","/usr/bin/xterm","-hold","-e", “bash”, “-c”,pCmd,pArg,(char*)0);

  4. For changing root session to non-root session, i tried setuid and setgid function to change the scope of the child process.

  5. Goal to achieve whatever binary name passed , that should display.

  6. Service and process should run for all users so that whatever user logged in he can use my application.

Correct me i am doing something wrong.

Thanks for comment.
I explained the scenario in comment#8.
Service launch another process to do other thing.Process should launch the any application.
If i move my process to some user mode will it help me to launch any application in gui format ?

I’m pretty sure that Gnome does not use $HOME/.Xauthority – it uses a temporary file somewhere. Check “$XAUTHORITY” when you are next logged into Gnome.

Agreed,
I found at /run/gdm/auth-for-<USER>-RhUPpp/database.
Another thing i found that this entry is not created for all user available and two entries created for same user.

I logged in as non-root user and try below command and got errors.
“gnomesu -c firefox”

No protocol specified

** (gnomesu:442): WARNING **: Could not open X display
No protocol specified
Cannot open display:

Now i tried following thing.
New process create in non-user session. I hard coded many thing to launch process in X mode.

char *envVar]={ “USER=anshul”, “PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11”,“XNLSPATH=/usr/share/X11/nls”,“HOME=/home/anshul”,“LOGNAME=anshul”,“DISPLAY=local-desktop:0.0”,“XDG_RUNTIME_DIR=/run/user/1000”,“XAUTHLOCALHOSTNAME=linuxTesting.site”,“XAUTHORITY=/run/gdm/auth-for-anshul-1h8njx/database”,“TERM=xterm”,“XDG_CONFIG_DIRS=/etc/xdg”,“XDG_DATA_DIRS=/usr/local/share:/usr/share”,“SHELL=/bin/bash”,“PWD=/home/anshul”, (char *) 0 };

rpid=fork();
if(rpid == -1)
{
ErrInt(constErrChildProcessFailed,errno);
}
if(rpid == 0)
{
setsid();
setuid(1000);
seteuid(1000);
setgid(100);

            rpid = execle(/usr/bin/X11/firefox,pArg,(char *)0,envVar); 
            //in case of netstat command 
             //rpid = execle("/usr/bin/xterm","/usr/bin/xterm","-hold","-e", "bash", "-c",pCmd,pArg,(char*)0,envVar);

   }

Process launched in non-user context but became defunt due to error “Error: no display specified” or “/usr/bin/xterm: Xt error: Can’t open display:”, “/usr/bin/xterm: DISPLAY is not set”.
Process displayed as below

anshul 4286 0.0 0.0 0 0 ? Zs 20:05 0:00 [firefox] <defunct>
anshul 4317 0.0 0.0 0 0 ? Zs 20:12 0:00 [xterm] <defunct>

please correct me if i am doing something wrong.
I changed process session also ,still getting same error.

On 2013-09-03 07:26, enggjiten wrote:

> If i move my process to some user mode will it help me to launch any
> application in gui format ?

The problem, is that, as far as I know, a service is not connected to a
display, so it can not open any GUI.

On the other hand, a GUI can start a detached service.


Cheers / Saludos,

Carlos E. R.
(from 11.4, with Evergreen, x86_64 “Celadon” (Minas Tirith))

I’m reading this thread,
and it just seems to me the object task (launch FF automatically on login) seems to be more complicated than it might have to be. Part of the problem to me seems to be working at both the CLI and Graphical modes, plus permission requirements.

Without looking into this more deeply,
I’m wondering if it might be possible create a systemd Unit that launches, maybe as a dependency to some some Unit that initiates only after login. That should address the timing and permissions required to invoke the app, the only thing left would seem to be to make sure FF is running with the logged in User permissions (if the invoking context isn’t already based on the User and not a Super User).

In fact, at least on KDE FF should only launch with non-root permissions.

Just some thoughts,
TSU

On 2013-09-05 18:46, tsu2 wrote:
> make sure FF is
> running with the logged in User permissions

Which one? There may be a hundred logged in. :stuck_out_tongue:


Cheers / Saludos,

Carlos E. R.
(from 12.3 x86_64 “Dartmouth” at Telcontar)

Curious to know is it possible to do it or it is impossible to do.
I searched over net for 2 weeks, i did not get any solution yet.
Even SuSE forum did help much. :‘(:’(

Well, it seems to be (nearly) impossible the way you want to do it.

But if it’s just about making sure a certain application is running for the logged in user, why not start a script on login with something like this:


#!/bin/bash
while true; do
   if program_is_not_running run_program
   sleep 3600s
done

That’s pseudo code of course, but I guess you can figure out what I mean… :wink:
And you could expand that to more than one program as well.