如何从用户定义的启动时间守护程序服务器运行 GUI 应用程序
我正在编写一个简单的服务器守护程序并使用 /etc/init.d/server 运行 它运行正常,但是当我想使用 system() 函数运行一个简单的 QT GUI 时,它无法运行它并返回 256 作为返回代码。
如果相同的守护进程从终端运行,则其工作正常,并且 system() 函数成功返回值 0,并且弹出 GUI。
我没有得到什么问题...... plzzzz 任何人都可以帮助我......
我正在使用 ubuntu-9.10 下面是代码....
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要从启动时运行图形应用程序。他们将无权访问 X 服务器。运行一个无头守护进程,并编写一个 GUI 应用程序来与其交互,该应用程序在 GUI 登录时启动。
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.
对于服务器端,使用 QtCoreApplication。
可能有一些现成的解决方案用于支持基于 Qt 的守护进程:
http://qt.nokia.com/products/appdev/add-on-products/catalog/4/Utilities/qtservice/
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/