一个Linux守护程序和标准输入/输出 [英] A Linux Daemon and the STDIN/STDOUT

查看:155
本文介绍了一个Linux守护程序和标准输入/输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Linux守护进程,并具有与标准输入/输出的一些问题。通常情况下,由于一个守护进程的性质没有任何标准输入或标准输出。不过,我有我的守护一种功能,当守护首次指定为守护程序成功运行需要不同的参数运行时调用。当这个函数被调用终端变得如此低迷,我要推出一个单独的外壳和杀死顶部的守护得到响应及时回来。现在,我怀疑这事做的分叉过程中关闭标准输入/输出,但我不能肯定我怎么能解决这个问题。如果你们能透露这将是最AP preciated情况的一些情况。谢谢你。

编辑:

  INT主(ARGC,CHAR *的argv []){/ *设置的信号处理* // *检查命令行参数* /将为pid_t PID,SID;PID =叉();如果(PID℃,){
出口(EXIT_FAILURE);
}如果(PID大于0){
出口(EXIT_SUCCESS);
}SID = setsid();如果(SID℃,){
出口(EXIT_FAILURE);
}的umask(027);/ *设置sys -log消息* // *做一些逻辑来确定羯羊,我们正在运行的守护进程的第一次,如果我们调用它使用与fgets的一次函数()来收到一些输入* /而(1){/ *需要做的工作* /}/ *做一些清理程序和退出* /返回0;
}

你们提到使用配置文件。这正是我做存储通过输入收到的参数。不过,我还是首先需要从通过​​标准输入用户得到这些。用于确定是否我们正在运行首次逻辑不基于配置文件的存在。


解决方案

  

你们提到使用配置文件。这正是我做存储通过输入收到的参数。不过,我还是首先需要从通过​​标准输入用户得到这些。用于确定在第一时间是否我们所运行的逻辑是基于离配置文件的存在。


而不是阅读标准输入,让用户写配置文件本身;的前检查其存在的分叉,并退出与一个错误,如果它没有。包括与后台程序的示例配置文件,并记录其在守护进程的手册页格式。您的的有一个手册页,是吗?你的配置文件的的文字,是吗?

此外,您的系统守护进程逻辑缺少的关键一步。分叉后,但调用 setsid 之前,你需要关闭FDS 0,1和2,并将其重新打开,以的/ dev / null的(做的的尝试与 FCLOSE 和的fopen )。这应该解决您的呆滞终端问题。

I am working on a linux daemon and having some issues with the stdin/stdout. Normally because of the nature of a daemon you do not have any stdin or stdout. However, I do have a function in my daemon that is called when the daemon runs for the first time to specify different parameters that are required for the daemon to run successfully. When this function is called the terminal becomes so sluggish that I have to launch a seperate shell and kill the daemon with top to get a responsive prompt back. Now I suspect that this has something to do with the forking process closing the stdin/stdout but I am not quite sure how I could work around this. If you guys could shed some light on the situation that would be most appreciated. Thanks.

Edit:

int main(argc, char *argv[]) {

/* setup signal handling */

/* check command line arguments */

pid_t pid, sid;

pid = fork();

if (pid < 0) {
exit(EXIT_FAILURE);
}

if(pid > 0){
exit(EXIT_SUCCESS);
}

sid = setsid();

if(sid < 0) {
exit(EXIT_FAILURE);
}

umask(027);

/* set syslogging */

/* do some logic to determine wether we are running the daemon for the first time and if we are call the one time function which uses fgets() to recieve some input */

while(1) {

/* do required work */

}

/* do some clean up procedures and exit */

return 0;
}

You guys mention using a config file. This is is exactly what I do to store the parameters recieved via input. However I still initially need to get these from the user via the stdin. The logic for determining whether we are running for the first time is based off of the existence of the config file.

解决方案

You guys mention using a config file. This is is exactly what I do to store the parameters recieved via input. However I still initially need to get these from the user via the stdin. The logic for determining whether we are running for the first time is based off of the existence of the config file.

Instead of reading stdin, have the user write the config file themselves; check for its existence before forking, and exit with an error if it doesn't. Include a sample config file with the daemon, and document its format in your daemon's manpage. You do have a manpage, yes? Your config file is textual, yes?

Also, your daemonization logic is missing a key step. After forking, but before calling setsid, you need to close fds 0, 1, and 2 and reopen them to /dev/null (do not attempt to do this with fclose and fopen). That should fix your sluggish terminal problem.

这篇关于一个Linux守护程序和标准输入/输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆