This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH 2/3] network: recvmsg and sendmsg standard compliance (BZ#16919)


Hello Szabolcs

On 21 April 2016 at 16:01, Szabolcs Nagy <szabolcs.nagy@arm.com> wrote:
> On 28/03/16 15:34, Adhemerval Zanella wrote:
>>  /* Structure used for storage of ancillary data object information.  */
>>  struct cmsghdr
>>    {
>> -    size_t cmsg_len;         /* Length of data in cmsg_data plus length
>> -                                of cmsghdr structure.
>> -                                !! The type should be socklen_t but the
>> -                                definition of the kernel is incompatible
>> -                                with this.  */
>> +#if __BYTE_ORDER == __BIG_ENDIAN
>> +    int __glibc_reserved1;   /* Pad toadjust Linux size to POSIX defined
>> +                                size for cmsg_len.  */
>> +    socklen_t cmsg_len;              /* Length of data in cmsg_data plus length
>> +                                of cmsghdr structure.  */
>> +#else
>> +    socklen_t cmsg_len;
>> +    int __glibc_reserved1;
>> +#endif
>>      int cmsg_level;          /* Originating protocol.  */
>>      int cmsg_type;           /* Protocol specific type.  */
>
> i think #if __WORDSIZE == 64 is missing here.
>
> but even in that case there is a subtle issue:
> if the size_t member is removed all other
> members have 4byte alignment, so the struct
> alignment changes from 8byte to 4byte.
>
> it is not clear from the standard how the
> msg_control buffer may be allocated (since
> only CMSG_* macros can access it), on linux
> the kernel makes a copy so it does not care
> about alignment in user-space, but the struct
> alignment is still visible in the c and c++ abi.
>
> msg_control usage should be probably documented
> in the linux man-page: glibc sunrpc sometimes
> uses plain char[], nscd uses a union with struct
> cmsghdr, i think neither of them makes a
>
>   CMSG_FIRSTHDR (&msg)->cmsg_len
>
> access strictly iso c confrom, but the later at
> least uses correct alignment.
>
> maybe a posix issue should be filed to the
> austin group.

Below are the current snippets of code shown in the cmsg(3) man page.
Could you please tell me more exactly what you think needs fixing?

[[
    EXAMPLE
       This code looks for the IP_TTL option in a  received  ancillary
       buffer:

           struct msghdr msgh;
           struct cmsghdr *cmsg;
           int *ttlptr;
           int received_ttl;

           /* Receive auxiliary data in msgh */

           for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
                   cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
               if (cmsg->cmsg_level == IPPROTO_IP
                       && cmsg->cmsg_type == IP_TTL) {
                   ttlptr = (int *) CMSG_DATA(cmsg);
                   received_ttl = *ttlptr;
                   break;
               }
           }

           if (cmsg == NULL) {
               /* Error: IP_TTL not enabled or small buffer or I/O error */
           }

       The  code below passes an array of file descriptors over a UNIX
       domain socket using SCM_RIGHTS:

           struct msghdr msg = {0};
           struct cmsghdr *cmsg;
           int myfds[NUM_FD];  /* Contains the file descriptors to pass */
           int *fdptr;
           union {         /* Ancillary data buffer, wrapped in a union
                              in order to ensure it is suitably aligned */
               char buf[CMSG_SPACE(sizeof myfds)];
               struct cmsghdr align;
           } u;

           msg.msg_control = u.buf;
           msg.msg_controllen = sizeof u.buf;
           cmsg = CMSG_FIRSTHDR(&msg);
           cmsg->cmsg_level = SOL_SOCKET;
           cmsg->cmsg_type = SCM_RIGHTS;
           cmsg->cmsg_len = CMSG_LEN(sizeof(int) * NUM_FD);
           fdptr = (int *) CMSG_DATA(cmsg);    /* Initialize the payload */
           memcpy(fdptr, myfds, NUM_FD * sizeof(int));
]]

Cheers,

Michael

-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]