Thread: getsockopt: Error EFAULT

  1. #1
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42

    getsockopt: Error EFAULT

    Here's the code:
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <errno.h>
    
    int main()
    {
    	int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
    	if(sock == -1)
    	   printf("Error\n");
    	
    	char *s[100];
    	socklen_t *x;
    	int ret = getsockopt(sock, SOL_SOCKET, 1, s, x);
    	if(ret==0)
           printf("Sucess!\n");
    	else
    	{
    		if(errno==EBADF)
                printf("The argument sockfd is not a valid file descriptor.\n");
    		else if(errno==EFAULT)
    		    printf("The address pointed to by optval is not in a valid part of.the process address space.  For getsockopt(), this error may also be returned if optlen is not in a valid part of the process address space.\n");
    		else if(errno==EINVAL)
    		    printf("optlen invalid in setsockopt().  In some cases this error.can also occur for an invalid value in optval (e.g., for the IP_ADD_MEMBERSHIP option described in ip(7)).\n");
    		else if(errno==ENOPROTOOPT)
    		    printf("The option is unknown at the level indicated.\n");
    		else if(errno==ENOTSOCK)
    		    printf("The file descriptor sockfd does not refer to a socket.\n");
    	}
    }
    When executed, it displays the text of the EFAULT error. I did everything according to the instructions and still can not understand what the problem is! Who can tell?
    The computer is just a dumb machine, people are foolish by the turbidity, but a person is better better as he is not a programmed object but an endless subject.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,063
    Do you know the difference between
    Code:
        socklen_t *x;
        int ret = getsockopt(sock, SOL_SOCKET, 1, s, x);
    and
    Code:
        char s[100];
        socklen_t x = sizeof(s);
        int ret = getsockopt(sock, SOL_SOCKET, 1, s, &x);
    Also, replace 1 with some suitable numeric constant.

    For getsockopt(), optlen is a value-result argument, initially containing the size
    of the buffer pointed to by optval, and modified on return to indicate
    the actual size of the value returned. If no option value is to be
    supplied or returned, optval may be NULL.
    You can replace lines 19 to 28 with a call to perror().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    programmer hacker DevZero's Avatar
    Join Date
    Jul 2017
    Posts
    42
    Quote Originally Posted by Salem View Post
    Do you know the difference between
    Code:
        socklen_t *x;
        int ret = getsockopt(sock, SOL_SOCKET, 1, s, x);
    and
    Code:
        char s[100];
        socklen_t x = sizeof(s);
        int ret = getsockopt(sock, SOL_SOCKET, 1, s, &x);
    Also, replace 1 with some suitable numeric constant.



    You can replace lines 19 to 28 with a call to perror().
    Now everything has worked. I do not like the perror function because it produces too little informative messages and it's better to do it manually. Problem solved

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Quote Originally Posted by DevZero View Post
    I do not like the perror function because it produces too little informative messages and it's better to do it manually.
    You never take anyone's advice or even seem to understand what they are telling you.
    I can't believe anyone still bothers to help you.
    Problem solved
    Not the real problem, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile error? Logic error? Syntax Error? Please help
    By Khody Afkhami in forum C Programming
    Replies: 4
    Last Post: 10-11-2014, 01:36 AM
  2. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  3. Replies: 3
    Last Post: 10-02-2007, 09:12 PM
  4. getsockopt
    By rohit in forum Linux Programming
    Replies: 0
    Last Post: 02-20-2002, 03:34 AM
  5. Efault
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 01-09-2002, 06:51 PM

Tags for this Thread