Bug 192109 - recvmsg fails to receive socket FD and subsequent functions on that FD fails with EBADF error.
Summary: recvmsg fails to receive socket FD and subsequent functions on that FD fails ...
Status: Closed Not A Bug
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 9.2-RELEASE
Hardware: amd64 Any
: --- Affects Only Me
Assignee: Mateusz Guzik
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-07-25 10:54 UTC by Abhijit
Modified: 2017-06-29 13:29 UTC (History)
1 user (show)

See Also:


Attachments
sample program to reproduce problem (2.43 KB, text/plain)
2014-07-25 10:54 UTC, Abhijit
no flags Details
fixed reproducer (2.49 KB, text/plain)
2014-07-26 00:31 UTC, Mateusz Guzik
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
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.