Bug 192109

Summary: recvmsg fails to receive socket FD and subsequent functions on that FD fails with EBADF error.
Product: Base System Reporter: Abhijit <abhijit_kulkarni>
Component: kernAssignee: Mateusz Guzik <mjg>
Status: Closed Not A Bug    
Severity: Affects Only Me CC: mjg
Priority: ---    
Version: 9.2-RELEASE   
Hardware: amd64   
OS: Any   
Attachments:
Description Flags
sample program to reproduce problem
none
fixed reproducer none

Description Abhijit 2014-07-25 10:54:09 UTC
Created attachment 144963 [details]
sample program to reproduce problem

I have application that sends socket FD from one process to another using sendmsg API and receives that FD using recvmsg API. It works perfectly on FreeBSD 6 (i386-32 bits binary) but fails on FreeBSD 9.2 (amd64-64 bits binary) .  The recvmsg API does not fail but the FD, that is retuned, is no longer socket FD hence further use of that FD fails with EBADF error.
Has anyone faced this problem before? Is this a known issue?

I have tried simple program that uses Unix domain socket and shares FD between 2 processes. It reproduces my problem. The program open file '/etc/motd' and sends the FD to another process using sendmsg. The other process receives the FD and prints the contents of the file. The program works perfectly on FreeBSD 6 (i386-32 bits binary) but fails to print contents of file on FreeBSD 9.2 (amd64-64 bits binary).
Comment 1 Mateusz Guzik freebsd_committer freebsd_triage 2014-07-26 00:31:35 UTC
Created attachment 144986 [details]
fixed reproducer
Comment 2 Mateusz Guzik freebsd_committer freebsd_triage 2014-07-26 00:32:47 UTC
Your code is incorrect and works on i386 only by chance.

It should use CMSG_ macros to fil up data.

I modified your reproducer to use it in a least-resistant way and everything seems to be working fine.