答复:Linux下守护进程使用Netlink与内核模块通信问题

58 views
Skip to first unread message

栾利刚

unread,
Oct 24, 2008, 4:06:07 AM10/24/08
to linux-...@zh-kernel.org
我也用过netlink机制主动从内核向应用层发送数据,我用的是组播方式,没出现问题 ,你可以试试。



_______________________________________________
Linux 内核开发中文邮件列表
Linux-...@zh-kernel.org
http://zh-kernel.org/mailman/listinfo/linux-kernel
Linux 内核开发中文社区: http://zh-kernel.org

Docoocoo

unread,
Oct 24, 2008, 7:29:20 AM10/24/08
to linux-...@zh-kernel.org
netlink机制主动从内核向应用层发送数据,我用的也没问题,但是把程序改为守护进程后就有问题了;
具体看我的问题描述!

2008/10/24 栾利刚 <letme...@163.com>

Docoocoo

unread,
Oct 26, 2008, 10:22:04 AM10/26/08
to linux-...@zh-kernel.org
最新进展:

经过调试,发现问题和内核部分没有关系了,问题是:

全局变量sock_fd用于接收内核数据的套接字=0;
sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_TEST);

而应用层还有其他创建的套接字socklog也等于0,使得close(socklog)时等于将sock_fd关闭,无法接受内核数据;
socklog=socket(AF_INET, SOCK_STREAM, 0);

不知道如何解决,连续几次的socket调用,期间不释放,返回都为0!!


请高手看看!!谢谢!!

2008/10/24 Docoocoo <doco...@gmail.com>

Luke Zhang (luzhan)

unread,
Oct 27, 2008, 10:02:40 PM10/27/08
to Docoocoo, linux-...@zh-kernel.org
狐说几句哈:

sock_fd=0, 显然是创建PF_NETLINK, RAW sock 失败。应该在检测到这个失败后,打印error看看是什么错误。

不会是需要root权限吧?赫赫。

Docoocoo

unread,
Oct 28, 2008, 8:39:43 PM10/28/08
to Luke Zhang (luzhan), linux-...@zh-kernel.org
socket返回-1是错误,0是正常的吧!!

而且我的sock_fd能够正常工作,只是新创建的socklog=socket(AF_INET, SOCK_STREAM, 0);
也为0,使得关闭socklog的时候把sock_fd关掉了,而sock_fd是全局的,需要一直存活,接收内核模块过来的数据。

而且一个问题就是:
socklog=socket(AF_INET, SOCK_STREAM, 0);
socklog1=socket(AF_INET, SOCK_STREAM, 0);
连续两次创建socklog和socklog1为0,即使中间不close(socklog);

2008/10/28 Luke Zhang (luzhan) <luz...@cisco.com>

Xinyu Chen

unread,
Oct 28, 2008, 9:39:33 PM10/28/08
to Docoocoo, linux-...@zh-kernel.org
Daemon起的有问题
看看http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html

2008/10/29 Docoocoo <doco...@gmail.com>:

--
Best Regards
Xinyu

Luke Zhang (luzhan)

unread,
Oct 28, 2008, 9:44:39 PM10/28/08
to Docoocoo, linux-...@zh-kernel.org
你是对的。

man socket

RETURN VALUE
Upon successful completion, socket() shall return a non-negative integer, the socket file
descriptor. Otherwise, a value of -1 shall be returned and errno set to indicate the
error.

又学习了下,看起来还是daemon的不寻常之处造成的。作为daemon执行时,socket()会返回stdin,也就是0。
找到一个很老的线索:
http://www.sunmanagers.org/archives/1998/1642.html
另外,这里有些怎么正确地把程序作为daemon来运行的说明,重定向stdin, stdout, stderr什么的。也许可以有帮助。
http://www.linuxjournal.com/article/2335

--张录


________________________________

Xinyu Chen

unread,
Oct 28, 2008, 11:08:50 PM10/28/08
to Luke Zhang (luzhan), Docoocoo, linux-...@zh-kernel.org
才发现已经有现成的了:

NAME
daemon - run in the background

SYNOPSIS
#include <unistd.h>

int daemon(int nochdir, int noclose);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

daemon(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)

DESCRIPTION
The daemon() function is for programs wishing to detach themselves from
the controlling terminal and run in the background as system daemons.

Unless the argument nochdir is nonzero, daemon() changes the current
working directory to the root ("/").

Unless the argument noclose is nonzero, daemon() will redirect standard
input, standard output and standard error to /dev/null.


2008/10/29 Luke Zhang (luzhan) <luz...@cisco.com>:

--
Best Regards
Xinyu

xifan

unread,
Oct 29, 2008, 12:21:21 AM10/29/08
to linux-kernel
[root@Server01 ~]# dmesg
TCP: Treason uncloaked! Peer 59.41.252.227:45645/80 shrinks window 3634452323:3634455243. Repaired.
TCP: Treason uncloaked! Peer 58.213.148.170:64073/80 shrinks window 3684414598:3684417518. Repaired.
TCP: Treason uncloaked! Peer 58.213.127.186:41913/80 shrinks window 3681738408:3681739868. Repaired.
TCP: Treason uncloaked! Peer 58.213.148.170:64073/80 shrinks window 3684490518:3684491978. Repaired.
TCP: Treason uncloaked! Peer 58.213.148.170:64073/80 shrinks window 3684875958:3684877418. Repaired.
TCP: Treason uncloaked! Peer 58.213.127.186:41913/80 shrinks window 3682493228:3682496148. Repaired.
TCP: Treason uncloaked! Peer 124.129.172.35:2303/80 shrinks window 890967709:890969117. Repaired.
TCP: Treason uncloaked! Peer 124.129.172.35:2303/80 shrinks window 890967709:890969117. Repaired.
TCP: Treason uncloaked! Peer 124.129.172.35:2303/80 shrinks window 890967709:890969117. Repaired.
TCP: Treason uncloaked! Peer 124.129.172.35:2303/80 shrinks window 890967709:890969117. Repaired.
我通过dmesg看见很多这样信息.
我gg了一下.众说分云...
说是被人攻击.有的说是内核问题.
我的内核是
uname -a
Linux media-82-W 2.6.9-78.0.1.ELsmp #1 SMP Tue Aug 5 11:02:47 EDT 2008 i686 i686 i386 GNU/Linux
[root@media-82-W ~]#
这台服务器用来做web的
安装了.apache和squid php zend
其他的基本没有做设置...
谢谢大家能帮我解答一下...


2008-10-29



xifan

Docoocoo

unread,
Nov 18, 2008, 8:10:05 AM11/18/08
to linux-...@zh-kernel.org
对不起各位,感谢各位的关注,这一段时间在忙着做其他事情,过几天再接着跟进,倒时候有什么进展再贴出来讨论!!

2008/10/29 Xinyu Chen <mudd...@gmail.com>

Docoocoo

unread,
Nov 24, 2008, 10:38:10 PM11/24/08
to linux-...@zh-kernel.org
The problem has been solved,which in the code of create daemon,not in
Netlink communication!

Thanks Peter Teoh,for your continued attention!
Thanks Xinyu Chen,told me checking daemon code!
Thanks 栾利刚,Luke Zhang and linux-kernel ^^ ^^!
Best Regards!

//the code of create daemon
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int init_daemon ()
{
pid_t pid;
pid = fork ();
if (pid < 0)
{
perror ("fork");
return -1;
}

if (pid != 0)
exit (0);

pid = setsid();

if (pid < -1)
{
perror ("setsid");
return -1;
}

chdir ("/");

int fd;
fd = open ("/dev/null", O_RDWR, 0);
if (fd != -1)
{
dup2 (fd, STDIN_FILENO);
dup2 (fd, STDOUT_FILENO);
dup2 (fd, STDERR_FILENO);

if (fd > 2)
close (fd);
}

umask (0027);
return 0;
}

2008/11/18 Docoocoo <doco...@gmail.com>

Reply all
Reply to author
Forward
0 new messages