views:

241

answers:

2

Dear All, I am writing one simple server daemon and running with /etc/init.d/server its running properly but when i want to run one simple QT GUI with system() function , its not able to run it and returning 256 as return code .

if same daemon i m running from terminal then its working properly and also system() function is getting success with return value 0 and GUI is getting popped up.

What is the problem i m not getting .... plzzzz can any one help me..........

i m using ubuntu-9.10 below is the code ....

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>

int main()
{
    //local variables
int sockfd,clifd,ret;
int client_flag = 0;
int server_flag = 1;
struct sockaddr_in server_addr,client_addr;
socklen_t client_len ;
int server_len;

daemon(0, 0);


/* open log file */
setlogmask(LOG_UPTO(LOG_INFO));
openlog("server:", LOG_CONS | LOG_PID, LOG_LOCAL2);


//creating the socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
perror("Error socket creation:");
return sockfd;
}

//filling the socket address detail

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8181);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_len = sizeof(server_addr);

//binding the socket
ret = bind(sockfd, (struct sockaddr *)&server_addr,server_len);
if(ret < 0){
perror("Error in bind");
return ret;
}

//creating the listening queue
ret = listen(sockfd, 10);
if(ret < 0){
perror("Error in listen");
return ret;
}

while(1){
//accepting the connection from client

client_len = sizeof(client_addr);
clifd = accept(sockfd, (struct sockaddr *)&client_addr, &client_len);
if(clifd < 0){
perror("Error in accept in server_init ");
return clifd;
}

client_flag = 0;

//reading the client_flag
ret = read(clifd, &client_flag,sizeof(client_flag));
if(ret < 0){
perror("Error in read");
return ret;
}

// if flag is true i want to run the GUI "console"
//console is Qt4 application
if(client_flag) {
syslog(LOG_NOTICE," *************** \n");

/* Here its returning 256 is if it is running from boot time and if after boot i will restrat it from terminal like "/etc/init.d/server restart" then it will return 0 success and GUI will popped up properly.*/

ret = system("/usr/sbin/test/console 1") ;

syslog(LOG_NOTICE," *************** %d\n",ret);


}
//writing the server_flag
ret = write(clifd, &server_flag,sizeof(server_flag));
if(ret < 0){
perror("Error in write");
return ret;
}


close(clifd);
}

closelog();
close(sockfd);
return 0;

}
+2  A: 

Do not run graphical applications from boot. They will not have access to the X server. Run a headless daemon, and write a GUI app to interact with it that starts on GUI login.

Ignacio Vazquez-Abrams
what is headless daemon?i did not get ?
It's a daemon that doesn't use a display.
Ignacio Vazquez-Abrams
then how to start GUI from daemon because my solution is like that when i will send a request then that system() function start the gui?...plz if any concept tell me .... and what ever u are telling that is working perfectlly that is another part of my solution....
You CAN'T start a GUI from a daemon. That's the point.
Ignacio Vazquez-Abrams
A: 

Hi,

For server side use QtCoreApplication. There might be some ready made solution sfor supporting Qt based daemons:

http://qt.nokia.com/products/appdev/add-on-products/catalog/4/Utilities/qtservice/

aleksandar sasha babic