File: aux.dpatch

package info (click to toggle)
syslog-ocaml 1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 192 kB
  • ctags: 96
  • sloc: ml: 211; sh: 104; makefile: 21
file content (94 lines) | stat: -rw-r--r-- 3,316 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#! /bin/sh /usr/share/dpatch/dpatch-run
## aux.dpatch by Eric Cooper <ecc@cmu.edu>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: patch to add wrapper for send(2) with MSG_NOSIGNAL option

@DPATCH@
diff -urNad syslog-ocaml-1.4~/Makefile syslog-ocaml-1.4/Makefile
--- syslog-ocaml-1.4~/Makefile	2007-11-01 15:42:18.000000000 -0400
+++ syslog-ocaml-1.4/Makefile	2007-11-01 15:42:18.000000000 -0400
@@ -1,8 +1,9 @@
 OCAMLMAKEFILE = /usr/share/ocamlmakefile/OCamlMakefile
 DESTDIR = $(shell ocamlc -where | sed s:/usr/lib/:/usr/local/lib/:)
 OCAMLFIND_INSTFLAGS = -destdir $(DESTDIR)
+NO_CUSTOM = yes
 
-SOURCES=syslog.mli syslog.ml
+SOURCES=syslog.mli aux.ml syslog.ml aux_stubs.c
 RESULT=syslog
 PACKS=unix
 
diff -urNad syslog-ocaml-1.4~/aux.ml syslog-ocaml-1.4/aux.ml
--- syslog-ocaml-1.4~/aux.ml	1969-12-31 19:00:00.000000000 -0500
+++ syslog-ocaml-1.4/aux.ml	2007-11-01 15:42:18.000000000 -0400
@@ -0,0 +1,14 @@
+type msg_flag =
+    MSG_OOB
+  | MSG_DONTROUTE
+  | MSG_PEEK
+  | MSG_NOSIGNAL
+
+external unsafe_send :
+  Unix.file_descr -> string -> int -> int -> msg_flag list -> int
+                                  = "aux_send"
+
+let send fd buf ofs len flags =
+  if ofs < 0 || len < 0 || ofs > String.length buf - len
+  then invalid_arg "Unix.send"
+  else unsafe_send fd buf ofs len flags
diff -urNad syslog-ocaml-1.4~/aux_stubs.c syslog-ocaml-1.4/aux_stubs.c
--- syslog-ocaml-1.4~/aux_stubs.c	1969-12-31 19:00:00.000000000 -0500
+++ syslog-ocaml-1.4/aux_stubs.c	2007-11-01 15:42:18.000000000 -0400
@@ -0,0 +1,31 @@
+#include <string.h>
+#include <sys/socket.h>
+#include <caml/mlvalues.h>
+#include <caml/alloc.h>
+#include <caml/signals.h>
+
+// These are from .../otherlibs/unix/unixsupport.h
+#define UNIX_BUFFER_SIZE 16384
+extern void uerror (char * cmdname, value arg) Noreturn;
+
+static int msg_flag_table[] = {
+  MSG_OOB, MSG_DONTROUTE, MSG_PEEK, MSG_NOSIGNAL
+};
+
+CAMLprim value aux_send(value sock, value buff, value ofs, value len,
+                        value flags)
+{
+  int ret, cv_flags;
+  long numbytes;
+  char iobuf[UNIX_BUFFER_SIZE];
+
+  cv_flags = convert_flag_list(flags, msg_flag_table);
+  numbytes = Long_val(len);
+  if (numbytes > UNIX_BUFFER_SIZE) numbytes = UNIX_BUFFER_SIZE;
+  memmove(iobuf, &Byte(buff, Long_val(ofs)), numbytes);
+  enter_blocking_section();
+  ret = send(Int_val(sock), iobuf, (int) numbytes, cv_flags);
+  leave_blocking_section();
+  if (ret == -1) uerror("send", (value) 0);
+  return Val_int(ret);
+}
diff -urNad syslog-ocaml-1.4~/syslog.ml syslog-ocaml-1.4/syslog.ml
--- syslog-ocaml-1.4~/syslog.ml	2007-11-01 15:42:17.000000000 -0400
+++ syslog-ocaml-1.4/syslog.ml	2007-11-01 15:51:55.000000000 -0400
@@ -191,14 +191,11 @@
     (try open_connection loginfo with _ -> ());
     if List.mem `LOG_CONS loginfo.flags then log_console str
   in
-  let prev = Sys.signal Sys.sigpipe (Sys.Signal_handle fallback) in
   try
-    ignore (write loginfo.fd str 0 (String.length str));
-    Sys.set_signal Sys.sigpipe prev
+    ignore (Aux.send loginfo.fd str 0 (String.length str) [Aux.MSG_NOSIGNAL])
   with Unix_error (_, _, _) ->
     (* on error, attempt to reconnect *)
-    fallback ();
-    Sys.set_signal Sys.sigpipe prev
+    fallback ()
 
 let syslog ?fac loginfo lev str =
   let msg = Buffer.create 64 in