LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-02-2010, 03:20 PM   #1
El Mago
LQ Newbie
 
Registered: Mar 2004
Posts: 26

Rep: Reputation: 15
Can php script execute X11 applications?


If I put
Code:
 
$r = exec('whoami'); 
echo $r;
into my php script, it works fine. However, if I put
Code:
 
$r = exec('myX11application'); 
echo $r;
it doesn't (to be precise, the script still works, but myX11application is not executed). Of course, scripts are run by user "apache", who doesn't have access to X11 server and doesn't even have DISPLAY variable defined.

I installed virtual framebuffer Xvfb, created a small bash script:
Code:
Xvfb :2
export DISPLAY=:2
myX11application
and called it with exec from php, but it still doesn't run since it looks like Xvfb can't start if the normal X server is running (I need that for development purposes). The reported error from Xvfb is
Code:
(EE) config/hal: NewInputDeviceRequest failed (2)

Is there a solution to have php running your X11 applications with normal X server running?

Last edited by El Mago; 06-02-2010 at 03:24 PM.
 
Old 06-03-2010, 06:00 AM   #2
meingbg
LQ Newbie
 
Registered: Mar 2010
Posts: 8

Rep: Reputation: 1
I have 2 possible solutions for you.

Solution #1:

Try something like

Code:
export DISPLAY=:0
myX11application
and set the set-UID bit on the script.

This way, apache can have permission to run the script at the same time as myX11application has permission to access the X server of another user. You do this by setting the other user as owner of the file, then chmod 4755 scriptfile. If it doesn't work, try to do the same with a compiled program instead of a shell script; refer to http://www.tuxation.com/setuid-on-shell-scripts.html for how to do this.



Solution #2:

Instead of Xvfb, try Xvnc. It can run at the same time as your X server. You can run it as apache user, so apache will have no problems displaying X applications on that buffer. To view the applications started by apache, just start vncviewer from your X server and connect to Xvnc. This connection is only password authenticated, so it doesn't matter what user is connecting with vncviewer. Note however that the VNC protocol isn't necessarily safe, so be sure to use it locally only and configure your firewall appropriately.
 
Old 06-03-2010, 01:44 PM   #3
El Mago
LQ Newbie
 
Registered: Mar 2004
Posts: 26

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by meingbg View Post
I have 2 possible solutions for you.

Solution #1:

Try something like

Code:
export DISPLAY=:0
myX11application
and set the set-UID bit on the script.

This way, apache can have permission to run the script at the same time as myX11application has permission to access the X server of another user. You do this by setting the other user as owner of the file, then chmod 4755 scriptfile. If it doesn't work, try to do the same with a compiled program instead of a shell script; refer to http://www.tuxation.com/setuid-on-shell-scripts.html for how to do this.



Solution #2:

Instead of Xvfb, try Xvnc. It can run at the same time as your X server. You can run it as apache user, so apache will have no problems displaying X applications on that buffer. To view the applications started by apache, just start vncviewer from your X server and connect to Xvnc. This connection is only password authenticated, so it doesn't matter what user is connecting with vncviewer. Note however that the VNC protocol isn't necessarily safe, so be sure to use it locally only and configure your firewall appropriately.
Thanks for your input, but unfortunately both methods failed.
#1: I followed also the advice on the link you provided, creating a C program, which sets UID and runs the script, but when it comes to executing myX11application, it still reports:
Code:
No protocol specified
myX11application: cannot connect to X server :0
#2: I tried Xvnc and vncserver, but it is almost the same thing:
Code:
New 'X' desktop is myServerURL:2

Starting applications specified in /var/www/.vnc/xstartup
Log file is /var/www/.vnc/myServerURL:2.log

No protocol specified
myX11application: cannot connect to X server :2

The code I used in the script is:
Code:
vncviewer :2 -geometry 1024x768
export DISPLAY=:2
./myX11application

Last edited by El Mago; 06-03-2010 at 01:53 PM.
 
Old 06-03-2010, 01:52 PM   #4
El Mago
LQ Newbie
 
Registered: Mar 2004
Posts: 26

Original Poster
Rep: Reputation: 15
Oops, just a few moments after I posted my complaints, solution #2 started to work. Not sure why, I guess I only changed screen from :2 to :1 and vncserver back to Xvnc.

Many thanks for this, I hope 40 virgins will wait for you when your time to join the heaven comes...

Last edited by El Mago; 06-03-2010 at 01:57 PM.
 
Old 06-03-2010, 06:11 PM   #5
El Mago
LQ Newbie
 
Registered: Mar 2004
Posts: 26

Original Poster
Rep: Reputation: 15
And now it doesn't work anymore. Xvnc doesn't start, and I didn't change a single line. This is the output of Xvnc:

Code:
04/06/2010 01:10:06 Xvnc version X.org/xf4vnc custom version
04/06/2010 01:10:06 Copyright (C) 2001-2004 Alan Hourihane.
04/06/2010 01:10:06 Copyright (C) 2000-2004 Constantin Kaplinsky
04/06/2010 01:10:06 Copyright (C) 1999 AT&T Laboratories Cambridge
04/06/2010 01:10:06 All Rights Reserved.
04/06/2010 01:10:06 See http://www.tightvnc.com/ for information on TightVNC
04/06/2010 01:10:06 See http://xf4vnc.sf.net for xf4vnc-specific information
04/06/2010 01:10:06 Desktop name 'x11' (myServer:1)
04/06/2010 01:10:06 Protocol versions supported: 3.7, 3.3
04/06/2010 01:10:06 Listening for VNC connections on TCP port 5901
(EE) config/hal: NewInputDeviceRequest failed (2)
(EE) config/hal: NewInputDeviceRequest failed (2)
(EE) config/hal: NewInputDeviceRequest failed (2)
(EE) config/hal: NewInputDeviceRequest failed (2)
(EE) config/hal: NewInputDeviceRequest failed (2)
I am confused.
 
Old 06-04-2010, 07:10 AM   #6
meingbg
LQ Newbie
 
Registered: Mar 2010
Posts: 8

Rep: Reputation: 1
Quote:
Originally Posted by El Mago View Post
The code I used in the script is:
Code:
vncviewer :2 -geometry 1024x768
export DISPLAY=:2
./myX11application
The script run by apache should not start vncviewer, nor should it start vncserver. The -geometry option only goes on the server. Now, do like this:
  • Start the vnc server as user apache:
    Code:
    vncserver :2 -geometry 1024x768
    This is to be done only once
  • In the script run by apache, only run the application; don't start any vncserver or vncviewer:
    Code:
    DISPLAY=:2.0 ./myX11application
  • When you want to look at the applications, you start vncviewer from your regular X environment:
    Code:
    vncviewer localhost:2
Don't hesitate to get back here if it still doesn't work.
 
Old 06-04-2010, 10:44 AM   #7
El Mago
LQ Newbie
 
Registered: Mar 2004
Posts: 26

Original Poster
Rep: Reputation: 15
Oh, now I get it. Thanks for your help, this solution worked fine. I had a few issues with the application crashing, but it was because it tried to write a log and it couldn't because it had no permissions, but I solved that as well, so everything works fine now. Many thanks again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Script Does Not Execute Ukee Linux - Server 5 02-24-2008 08:00 PM
execute php script by a time () afbase Linux - General 1 12-30-2006 03:30 PM
How to Execute PHP Script? notooth Linux - General 2 06-02-2005 05:43 AM
execute php script automatically? brandnewbie Linux - Newbie 1 08-14-2004 05:42 AM
PHP -- How to execute a shell script from PHP using FTP functions?? zoonalex Programming 3 07-29-2004 11:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration