Skip to content

Commit

Permalink
Merge pull request #1 from euanh/sync-with-djs55
Browse files Browse the repository at this point in the history
Sync with djs55
  • Loading branch information
euanh committed Feb 5, 2016
2 parents d7bd058 + 50ffd63 commit de77414
Show file tree
Hide file tree
Showing 13 changed files with 2,493 additions and 626 deletions.
17 changes: 8 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
*.annot
*.cmo
*.cma
*.cmi
*.a
*.o
*.cmx
*.cmxs
*.cmxa
setup.bin
setup.data
setup.log
dist
_build
api.docdir
*.native
*.byte
55 changes: 31 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
.PHONY: all clean install build
all: build doc
# OASIS_START
# DO NOT EDIT (digest: a3c674b4239234cbbe53afe090018954)

NAME=fd-send-recv
J=4
SETUP = ocaml setup.ml

export OCAMLRUNPARAM=b
build: setup.data
$(SETUP) -build $(BUILDFLAGS)

setup.bin: setup.ml
@ocamlopt.opt -o $@ $< || ocamlopt -o $@ $< || ocamlc -o $@ $<
@rm -f setup.cmx setup.cmi setup.o setup.cmo
doc: setup.data build
$(SETUP) -doc $(DOCFLAGS)

setup.data: setup.bin
@./setup.bin -configure
test: setup.data build
$(SETUP) -test $(TESTFLAGS)

build: setup.data setup.bin
@./setup.bin -build -j $(J)
all:
$(SETUP) -all $(ALLFLAGS)

doc: setup.data setup.bin
@./setup.bin -doc -j $(J)
install: setup.data
$(SETUP) -install $(INSTALLFLAGS)

install: setup.bin
@./setup.bin -install
uninstall: setup.data
$(SETUP) -uninstall $(UNINSTALLFLAGS)

test: setup.bin build
@./setup.bin -test

reinstall: setup.bin
@ocamlfind remove $(NAME) || true
@./setup.bin -reinstall
reinstall: setup.data
$(SETUP) -reinstall $(REINSTALLFLAGS)

clean:
@ocamlbuild -clean
@rm -f setup.data setup.log setup.bin
$(SETUP) -clean $(CLEANFLAGS)

distclean:
$(SETUP) -distclean $(DISTCLEANFLAGS)

setup.data:
$(SETUP) -configure $(CONFIGUREFLAGS)

configure:
$(SETUP) -configure $(CONFIGUREFLAGS)

.PHONY: build doc test all install uninstall reinstall clean distclean configure

# OASIS_STOP
50 changes: 48 additions & 2 deletions _oasis
Original file line number Diff line number Diff line change
@@ -1,16 +1,62 @@
OASISFormat: 0.2
OASISFormat: 0.3
Name: fd-send-recv
Version: 1.0.1
Synopsis: Bindings to sendmsg/recvmsg which allow fds to be sent/received
Authors: Jonathan Ludlam <jonathan.ludlam@eu.citrix.com>
License: LGPL-2.1 with OCaml linking exception
Plugins: META (0.2)
Plugins: META (0.3), DevFiles (0.3)
BuildTools: ocamlbuild

Flag tuntap
Description: Enable the tuntap test (require package tuntap)
Default: false

Library fd_send_recv
CompiledObject: best
Path: lib
Findlibname: fd-send-recv
Modules: Fd_send_recv
CSources: fd_send_recv_stubs.c
BuildDepends: unix

Document api
Title: Documentation and API reference
Type: ocamlbuild (0.3)
BuildTools+: ocamldoc
XOCamlbuildLibraries: fd-send-recv
XOcamlbuildPath: doc
Install: false

Executable test
Path: test
MainIs: test.ml
BuildDepends: threads, fd-send-recv
CompiledObject: best
Install: false

Executable test_fork
Path: test
MainIs: test_fork.ml
BuildDepends: fd-send-recv
CompiledObject: best
Install: false

Executable test_tuntap
Build$: flag(tuntap)
Install: false
Path: test
MainIs: test_tuntap.ml
BuildDepends: fd-send-recv, tuntap
CompiledObject: best

Test test
Command: ./test.native
Run: true

Test test_fork
Command: ./test_fork.native
Run: true

Test test_tuntap
Command: ./test_tuntap.native
Run$: flag(tuntap)
25 changes: 21 additions & 4 deletions _tags
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# OASIS_START
# DO NOT EDIT (digest: 4206d3c65bf245bd4b7dd568003eb92a)
# Ignore VCS directories, you can use the same kind of rule outside
# OASIS_START/STOP if you want to exclude directories that contains
# DO NOT EDIT (digest: 31b466dfab10d97e7109d7a6c9172be1)
# Ignore VCS directories, you can use the same kind of rule outside
# OASIS_START/STOP if you want to exclude directories that contains
# useless stuff for the build process
true: annot, bin_annot
<**/.svn>: -traverse
<**/.svn>: not_hygienic
".bzr": -traverse
Expand All @@ -16,6 +17,22 @@
# Library fd_send_recv
"lib/fd_send_recv.cmxs": use_fd_send_recv
<lib/fd_send_recv.{cma,cmxa}>: use_libfd_send_recv_stubs
<lib/*.ml{,i}>: pkg_unix
<lib/*.ml{,i,y}>: pkg_unix
"lib/fd_send_recv_stubs.c": pkg_unix
# Executable test
<test/test.{native,byte}>: pkg_threads
<test/test.{native,byte}>: pkg_unix
<test/test.{native,byte}>: use_fd_send_recv
<test/*.ml{,i,y}>: pkg_threads
# Executable test_fork
<test/test_fork.{native,byte}>: pkg_unix
<test/test_fork.{native,byte}>: use_fd_send_recv
# Executable test_tuntap
<test/test_tuntap.{native,byte}>: pkg_tuntap
<test/test_tuntap.{native,byte}>: pkg_unix
<test/test_tuntap.{native,byte}>: use_fd_send_recv
<test/*.ml{,i,y}>: pkg_tuntap
<test/*.ml{,i,y}>: pkg_unix
<test/*.ml{,i,y}>: use_fd_send_recv
# OASIS_STOP
<lib>: include
27 changes: 27 additions & 0 deletions configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

# OASIS_START
# DO NOT EDIT (digest: dc86c2ad450f91ca10c931b6045d0499)
set -e

FST=true
for i in "$@"; do
if $FST; then
set --
FST=false
fi

case $i in
--*=*)
ARG=${i%%=*}
VAL=${i##*=}
set -- "$@" "$ARG" "$VAL"
;;
*)
set -- "$@" "$i"
;;
esac
done

ocaml setup.ml -configure "$@"
# OASIS_STOP
1 change: 1 addition & 0 deletions doc/api.odocl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fd_send_recv
27 changes: 19 additions & 8 deletions lib/fd_send_recv.mli
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,30 @@
* GNU Lesser General Public License for more details.
*)

(** Module to send and receive file descriptors over UNIX domain sockets. *)

exception Unix_error of int
(** Thrown by the low-level C functions *)

val send_fd : Unix.file_descr -> string -> int -> int -> Unix.msg_flag list -> Unix.file_descr -> int
(** [send_fd channel_fd buf ofs len flags fd_to_send] sends a message over [channel_fd]
containing the [buf] [ofs] [len] substring, with [flags] and file descriptor [fd_to_send] *)
val send_fd : Unix.file_descr -> string -> int -> int ->
Unix.msg_flag list -> Unix.file_descr -> int
(** [send_fd channel_fd buf ofs len flags fd_to_send] sends a message
over [channel_fd] containing the [buf] [ofs] [len] substring, with
[flags] and file descriptor [fd_to_send], and returns the number
of bytes sent. Note that you have to send a non-empty message
(e.g. of size greater than zero) to actually have the fd
passed. *)

val recv_fd : Unix.file_descr -> string -> int -> int -> Unix.msg_flag list -> int * Unix.sockaddr * Unix.file_descr
(** [recv_fd channel_fd buf ofs len flags] receives a message into substring [buf] [ofs] [len]
with [flags], returning the number of bytes read, the address of the peer and a file descriptor *)
val recv_fd : Unix.file_descr -> string -> int -> int ->
Unix.msg_flag list -> int * Unix.sockaddr * Unix.file_descr
(** [recv_fd channel_fd buf ofs len flags] receives a message into
substring [buf] [ofs] [len] with [flags], returning the number of
bytes read, the address of the peer and a file descriptor. *)

val int_of_fd : Unix.file_descr -> int
(** [int_of_fd fd] returns the underlying unix integer file descriptor associated with OCaml Unix.file_descr [fd] *)
(** [int_of_fd fd] returns the underlying unix integer file descriptor
associated with OCaml Unix.file_descr [fd]. *)

val fd_of_int : int -> Unix.file_descr
(** [fd_of_int fd] returns the OCaml Unix.file_descr associated with underlying unix integer [fd] *)
(** [fd_of_int fd] returns the OCaml Unix.file_descr associated with
underlying unix integer [fd]. *)
8 changes: 6 additions & 2 deletions lib/fd_send_recv_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ CAMLprim value stub_unix_send_fd(value sock, value buff, value ofs, value len, v
ret=sendmsg(Int_val(sock), &msg, cv_flags);
caml_leave_blocking_section();

if(ret == -1)
if(ret == -1) {
perror("sendmsg");
raise_error(errno);
}

CAMLreturn(Val_int(ret));
}
Expand Down Expand Up @@ -142,8 +144,10 @@ CAMLprim value stub_unix_recv_fd(value sock, value buff, value ofs, value len, v
ret=recvmsg(Int_val(sock), &msg, cv_flags);
caml_leave_blocking_section();

if(ret == -1)
if(ret == -1) {
perror("recvmsg");
raise_error(errno);
}

if(ret>0 && msg.msg_controllen>0) {
cmsg = CMSG_FIRSTHDR(&msg);
Expand Down
Loading

0 comments on commit de77414

Please sign in to comment.