【问题标题】:Error in setsockopt(): Numerical argument out of domainsetsockopt() 中的错误:数值参数超出域
【发布时间】:2012-12-14 21:49:54
【问题描述】:

我有一个工作代码在安装了 RedHat 的 Linux 上运行,内核为 2.6.18-194.el5 #1 SMP x86_64

当我将代码移动到安装了 CentOS 6.3 的新机器上时,同样的代码失败了

分配套接字选项时出错:数值参数超出域

后者机器的内核版本是2.6.32-279.el6.x86_64 #1 SMP

以下分别是机器中工作和失败的代码。

    struct timeval          tv;

    tv.tv_sec       = 0;
    tv.tv_usec      = 1500000;

    if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)) != 0)
    {
            LM_ERR("Error assigning socket option: %s", strerror( errno ));
            return FALSE;
    }

【问题讨论】:

    标签: c linux sockets


    【解决方案1】:

    尝试使用

    tv.tv_sec       = 1;
    tv.tv_usec      = 500000;
    

    我看到一些实现不接受高于 10^6 的tv_usec

    编辑:这个问题很有趣,可以稍微挖掘一下。在内核源代码中寻找SO_RCVTIMEO 我在net/core/sock.c 中找到了以下代码:

    int sock_setsockopt(struct socket *sock, int level, int optname,
                char __user *optval, unsigned int optlen)
    {
    
       / ... /    
       switch (optname) {
    
       / ... /    
    
       case SO_RCVTIMEO:
            ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen);
            break;
    
       / ... /    
    }
    

    sock_set_timeout() 函数的开头确实包含一些范围检查:

    static int sock_set_timeout(long *timeo_p, char __user *optval, int optlen)
    {
        struct timeval tv;
    
        if (optlen < sizeof(tv))
            return -EINVAL;
        if (copy_from_user(&tv, optval, sizeof(tv)))
            return -EFAULT;
        if (tv.tv_usec < 0 || tv.tv_usec >= USEC_PER_SEC)
            return -EDOM;
    
        /* ... */
    }
    

    现在我们知道的足够多,可以做一些git blame-ing :) 更改是通过以下更改集引入的:

    commit ba78073e6f70cd9c64a478a9bd901d7c8736cfbc 
    Author: Vasily Averin <vvs@sw.ru> 
    Date:   Thu May 24 16:58:54 2007 -0700
    
    [NET]: "wrong timeout value" in sk_wait_data() v2
    
    sys_setsockopt() do not check properly timeout values for 
    SO_RCVTIMEO/SO_SNDTIMEO, for example it's possible to set negative timeout
    values. POSIX do not defines behaviour for sys_setsockopt in case negative
    timeouts, but requires that setsockopt() shall fail with -EDOM if the send and
    receive timeout values are too big to fit into the timeout fields in the socket
    structure. 
    In current implementation negative timeout can lead to error messages like
    "schedule_timeout: wrong timeout value".
    
    Proposed patch:
    - checks tv_usec and returns -EDOM if it is wrong
    - do not allows to set negative timeout values (sets 0 instead) and outputs
      ratelimited information message about such attempts.
    
    Signed-off-By: Vasily Averin <vvs@sw.ru>
    Signed-off-by: David S. Miller <davem@davemloft.net>
    

    我相信提交评论解释了一切。据我所知,此更改包含在 2.6.22-rc3 中。

    【讨论】:

    • 这种方式有效。但问题仍然存在:为什么这在我之前的安装中有效?
    • 我自己也看到过这个问题(select(),很正常)——我相信实现只是在某个时候发生了变化,要么是由于修复了一些错误,要么可能是为了提高合规性等.
    • 谢谢!它在各个方面都明确回答了我的问题。再次感谢:)
    • 这对我来说几乎是 DenverCoder9 的问题。感谢调试。为什么内核不将 us 值取模为 ms 值?即 1000001 的 tv_usec 将被视为 1 s + 1 us
    【解决方案2】:

    不应该是这样的

    tv.tv_sec   = 1;
    tv.tv_usec  = 500000;
    

    因为 100 万微秒 == 1 秒?

    【讨论】:

    • 是的,但它可以在以前版本的内核中运行,这不是很奇怪吗?
    猜你喜欢
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多