diff --git a/.gitignore b/.gitignore index 4b8d27e..0e9005d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,19 +1,6 @@ *.swp +*~ _build/ -setup.data -setup.log -setup.bin -*.docdir -*.native -*.byte - -# OASIS autogen: -Makefile -_tags -configure -lib/META -lib/launchd.mldylib -lib/launchd.mllib -lib/liblaunchd_stubs.clib -myocamlbuild.ml -setup.ml +*.install +.merlin +_opam/ diff --git a/.travis.yml b/.travis.yml index 635620d..579340e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,9 @@ language: c -install: wget https://raw.githubusercontent.com/ocaml/ocaml-travisci-skeleton/master/.travis-opam.sh +install: wget https://raw.githubusercontent.com/ocaml/ocaml-ci-scripts/master/.travis-opam.sh script: bash -ex .travis-opam.sh sudo: required os: - osx env: - - PACKAGE="launchd" OCAML_VERSION=4.02 + - PACKAGE="launchd" OCAML_VERSION=4.03 + - PACKAGE="launchd" OCAML_VERSION=4.07 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b00648f --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +.PHONY: all test clean + +all: + dune build + +test: + dune runtest + +clean: + dune clean diff --git a/_oasis b/_oasis deleted file mode 100644 index d2339ae..0000000 --- a/_oasis +++ /dev/null @@ -1,54 +0,0 @@ -OASISFormat: 0.3 -Name: launchd -Version: 1.2 -Synopsis: Binding to the launchd API -License: ISC -Authors: David Scott -Copyrights: (C) 2015 Unikernel Systems -BuildTools: ocamlbuild -Plugins: DevFiles (0.3), META (0.3) - -Library launchd - Path: lib - Modules: Launchd - BuildDepends: unix, result - Findlibname: launchd - CSources: launchd_stubs.c - -Library launchd_lwt - Path: lwt - Modules: Lwt_launchd - BuildDepends: launchd, lwt, lwt.unix, cstruct, cstruct.lwt - CSources: launchd_stubs.c - Findlibname: lwt - Findlibparent: launchd - -Document launchd - Title: Launchd bindings - Type: ocamlbuild (0.3) - BuildTools+: ocamldoc - Install: true - XOCamlbuildPath: lib - XOCamlbuildLibraries: launchd - -Document lwt_launchd - Title: Launchd Lwt bindings - Type: ocamlbuild (0.3) - BuildTools+: ocamldoc - Install: true - XOCamlbuildPath: lwt - XOCamlbuildLibraries: launchd.lwt - -Executable example - Install: false - CompiledObject: best - Path: example - MainIs: example.ml - BuildDepends: launchd - -Executable example_lwt - Install: false - CompiledObject: best - Path: example - MainIs: example_lwt.ml - BuildDepends: launchd.lwt diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..41f608f --- /dev/null +++ b/dune-project @@ -0,0 +1,2 @@ +(lang dune 1.1) +(name launchd) diff --git a/example/dune b/example/dune new file mode 100644 index 0000000..7cbaeb4 --- /dev/null +++ b/example/dune @@ -0,0 +1,13 @@ +(executable + (name example) + (modules example) + (libraries launchd)) + +(executable + (name example_lwt) + (modules example_lwt) + (libraries launchd.lwt)) + +(alias + (name runtest) + (deps ./example.exe ./example_lwt.exe)) \ No newline at end of file diff --git a/example/example.ml b/example/example.ml index 3831712..b99aa29 100644 --- a/example/example.ml +++ b/example/example.ml @@ -17,16 +17,16 @@ let _ = match Launchd.(error_to_msg (activate_socket "Listener")) with - | Result.Ok fds -> + | Ok fds -> while true do let ready_fds, _, _ = Unix.select fds [] [] (-1.) in List.iter (fun fd -> let client, _ = Unix.accept fd in - let message = "Hello there!\n" in - let (_: int) = Unix.write client message 0 (String.length message) in + let message = Bytes.of_string "Hello there!\n" in + let (_: int) = Unix.write client message 0 (Bytes.length message) in Unix.close client ) ready_fds done - | Result.Error (`Msg m) -> + | Error (`Msg m) -> Printf.fprintf stderr "%s\n%!" m; exit (-1) diff --git a/example/example_lwt.ml b/example/example_lwt.ml index f84d6bd..760bae4 100644 --- a/example/example_lwt.ml +++ b/example/example_lwt.ml @@ -14,24 +14,25 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * *) -open Lwt + +open Lwt.Infix let t = Lwt_launchd.activate_socket "Listener" >>= fun x -> match Launchd.error_to_msg x with - | Result.Ok fds -> + | Ok fds -> Lwt_list.iter_p (fun fd -> - let rec loop () = + let loop () = Lwt_unix.accept fd >>= fun (client, _) -> - let message = "Hello there!\n" in - Lwt_unix.write client message 0 (String.length message) + let message = Bytes.of_string "Hello there!\n" in + Lwt_unix.write client message 0 (Bytes.length message) >>= fun (_: int) -> Lwt_unix.close client in loop () ) fds - | Result.Error (`Msg m) -> + | Error (`Msg m) -> Printf.fprintf stderr "%s\n%!" m; exit (-1) diff --git a/launchd.opam b/launchd.opam new file mode 100644 index 0000000..11355e2 --- /dev/null +++ b/launchd.opam @@ -0,0 +1,20 @@ +opam-version: "1.2" +maintainer: "dave@recoil.org" +authors: [ "David Scott" ] +license: "ISC" +homepage: "https://github.com/mirage/ocaml-launchd" +dev-repo: "https://github.com/mirage/ocaml-launchd.git" +bug-reports: "https://github.com/mirage/ocaml-launchd/issues" + +build: [ + ["dune" "subst"]{pinned} + ["dune" "build" "-p" name "-j" jobs] +] + +depends: [ + "dune" {build} + "lwt" {>= "2.4.3"} + "cstruct" {>= "1.0.0"} + "cstruct-lwt" +] +available: [ os = "darwin" ] diff --git a/lib/dune b/lib/dune new file mode 100644 index 0000000..be16d7e --- /dev/null +++ b/lib/dune @@ -0,0 +1,4 @@ +(library + (public_name launchd) + (c_names launchd_stubs) + (libraries unix)) diff --git a/lib/launchd.ml b/lib/launchd.ml index e5b7f07..7fdd308 100644 --- a/lib/launchd.ml +++ b/lib/launchd.ml @@ -22,13 +22,11 @@ | `Ealready ] - type 'a result = ('a, error) Result.result - let error_to_msg = function - | Result.Ok x -> Result.Ok x - | Result.Error (`Enoent x) -> Result.Error (`Msg ("There was no socket named " ^ x)) - | Result.Error `Esrch -> Result.Error (`Msg "This process is not managed by launchd") - | Result.Error `Ealready -> Result.Error (`Msg "The socket has already been activated") + | Ok x -> Ok x + | Error (`Enoent x) -> Error (`Msg ("There was no socket named " ^ x)) + | Error `Esrch -> Error (`Msg "This process is not managed by launchd") + | Error `Ealready -> Error (`Msg "The socket has already been activated") module LowLevel = struct external launch_activate_socket: string -> Unix.file_descr array = "stub_launch_activate_socket" @@ -36,10 +34,8 @@ end let activate_socket name = try - Result.Ok (Array.to_list (LowLevel.launch_activate_socket name)) - with Unix.Unix_error(Unix.ENOENT, _, _) -> - Result.Error (`Enoent name) - | Unix.Unix_error(Unix.ESRCH, _, _) -> - Result.Error `Esrch - | Unix.Unix_error(Unix.EALREADY, _, _) -> - Result.Error `Ealready + Ok (Array.to_list (LowLevel.launch_activate_socket name)) + with +| Unix.Unix_error(Unix.ENOENT, _, _) -> Error (`Enoent name) + | Unix.Unix_error(Unix.ESRCH, _, _) -> Error `Esrch + | Unix.Unix_error(Unix.EALREADY, _, _) -> Error `Ealready diff --git a/lib/launchd.mli b/lib/launchd.mli index 58642a9..700ad85 100644 --- a/lib/launchd.mli +++ b/lib/launchd.mli @@ -42,11 +42,9 @@ type error = [ | `Ealready (** The socket has already been activated *) ] -type 'a result = ('a, error) Result.result +val error_to_msg: ('a, error) result -> ('a, [ `Msg of string ]) result -val error_to_msg: 'a result -> ('a, [ `Msg of string ]) Result.result - -val activate_socket: string -> Unix.file_descr list result +val activate_socket: string -> (Unix.file_descr list, error) result (** [activate_socket name]: retrieve the file descriptors for the sockets associated with the given [name] in the .plist file. This should be called once per invocation; subsequent calls will fail with [`Ealready]. diff --git a/lwt/dune b/lwt/dune new file mode 100644 index 0000000..79be586 --- /dev/null +++ b/lwt/dune @@ -0,0 +1,5 @@ +(library + (name lwt_launchd) + (public_name launchd.lwt) + (c_names launchd_stubs) + (libraries launchd lwt lwt.unix cstruct cstruct-lwt)) diff --git a/lwt/lwt_launchd.mli b/lwt/lwt_launchd.mli index 3d68153..d5ab432 100644 --- a/lwt/lwt_launchd.mli +++ b/lwt/lwt_launchd.mli @@ -21,7 +21,7 @@ open Launchd -val activate_socket: string -> Lwt_unix.file_descr list result Lwt.t +val activate_socket: string -> (Lwt_unix.file_descr list, error) result Lwt.t (** [activate_socket name]: retrieve the file descriptors for the sockets associated with the given [name] in the .plist file. This should be called once per invocation; subsequent calls will fail with [`Ealready]. diff --git a/opam b/opam deleted file mode 100644 index b9e59d3..0000000 --- a/opam +++ /dev/null @@ -1,32 +0,0 @@ -opam-version: "1.2" -name: "launchd" -maintainer: "dave@recoil.org" -version: "1.2" -authors: [ "David Scott" ] -license: "ISC" -homepage: "https://github.com/mirage/ocaml-launchd" -dev-repo: "https://github.com/mirage/ocaml-launchd.git" -bug-reports: "https://github.com/mirage/ocaml-launchd/issues" - -tags: [ -] - -build: [ - ["oasis" "setup"] - [make "PREFIX=%{prefix}%"] -] - -install: [make "PREFIX=%{prefix}%" "install"] - -remove: [["ocamlfind" "remove" "launchd"]] - -depends: [ - "result" - "lwt" {>= "2.4.3"} - "cstruct" {>= "0.7.0"} - "ocamlfind" {build} - "oasis" {build} -] -build-doc: ["ocaml" "setup.ml" "-doc"] - -available: [ os = "darwin" ]