Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: all test clean

all:
dune build

test:
dune runtest

clean:
dune clean
54 changes: 0 additions & 54 deletions _oasis

This file was deleted.

2 changes: 2 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(lang dune 1.1)
(name launchd)
13 changes: 13 additions & 0 deletions example/dune
Original file line number Diff line number Diff line change
@@ -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))
8 changes: 4 additions & 4 deletions example/example.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
13 changes: 7 additions & 6 deletions example/example_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
20 changes: 20 additions & 0 deletions launchd.opam
Original file line number Diff line number Diff line change
@@ -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" ]
4 changes: 4 additions & 0 deletions lib/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(library
(public_name launchd)
(c_names launchd_stubs)
(libraries unix))
22 changes: 9 additions & 13 deletions lib/launchd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,20 @@
| `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"
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
6 changes: 2 additions & 4 deletions lib/launchd.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down
5 changes: 5 additions & 0 deletions lwt/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(name lwt_launchd)
(public_name launchd.lwt)
(c_names launchd_stubs)
(libraries launchd lwt lwt.unix cstruct cstruct-lwt))
2 changes: 1 addition & 1 deletion lwt/lwt_launchd.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down
32 changes: 0 additions & 32 deletions opam

This file was deleted.