[dovecot-cvs] dovecot/src/lib ioloop-notify-dn.c, 1.16, 1.17 ioloop.c, 1.37, 1.38 ioloop.h, 1.19, 1.20 lib-signals.c, 1.14, 1.15 lib-signals.h, 1.7, 1.8 macros.h, 1.19, 1.20 ostream.c, 1.17, 1.18 ostream.h, 1.15, 1.16

tss at dovecot.org tss at dovecot.org
Fri Dec 15 16:55:36 UTC 2006


Update of /var/lib/cvs/dovecot/src/lib
In directory talvi:/tmp/cvs-serv346/lib

Modified Files:
	ioloop-notify-dn.c ioloop.c ioloop.h lib-signals.c 
	lib-signals.h macros.h ostream.c ostream.h 
Log Message:
Added context parameter type safety checks for most callback APIs.



Index: ioloop-notify-dn.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/ioloop-notify-dn.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- ioloop-notify-dn.c	10 Aug 2006 15:54:24 -0000	1.16
+++ ioloop-notify-dn.c	15 Dec 2006 16:55:32 -0000	1.17
@@ -41,9 +41,8 @@
 	errno = saved_errno;
 }
 
-static void event_callback(void *context)
+static void event_callback(struct ioloop *ioloop)
 {
-	struct ioloop *ioloop = context;
 	struct ioloop_notify_handler_context *ctx =
 		ioloop->notify_handler_context;
 	struct io *io;

Index: ioloop.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/ioloop.c,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -d -r1.37 -r1.38
--- ioloop.c	17 Aug 2006 21:16:49 -0000	1.37
+++ ioloop.c	15 Dec 2006 16:55:32 -0000	1.38
@@ -14,6 +14,7 @@
 
 struct ioloop *current_ioloop = NULL;
 
+#undef io_add
 struct io *io_add(int fd, enum io_condition condition,
 		  io_callback_t *callback, void *context)
 {
@@ -41,6 +42,7 @@
 	return io;
 }
 
+#undef io_add_notify
 struct io *io_add_notify(const char *path, io_callback_t *callback,
 			 void *context)
 {
@@ -138,6 +140,7 @@
 	}
 }
 
+#undef timeout_add
 struct timeout *timeout_add(unsigned int msecs, timeout_callback_t *callback,
 			    void *context)
 {

Index: ioloop.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/ioloop.h,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- ioloop.h	17 Aug 2006 18:46:42 -0000	1.19
+++ ioloop.h	15 Dec 2006 16:55:32 -0000	1.20
@@ -37,14 +37,22 @@
    the behavior will be undefined. */
 struct io *io_add(int fd, enum io_condition condition,
 		  io_callback_t *callback, void *context);
+#define io_add(fd, condition, callback, context) \
+	CONTEXT_CALLBACK(io_add, io_callback_t, \
+			 callback, context, fd, condition)
 struct io *io_add_notify(const char *path, io_callback_t *callback,
 			 void *context);
+#define io_add_notify(path, callback, context) \
+	CONTEXT_CALLBACK(io_add_notify, io_callback_t, callback, context, path)
 /* Remove I/O handler, and set io pointer to NULL. */
 void io_remove(struct io **io);
 
 /* Timeout handlers */
 struct timeout *timeout_add(unsigned int msecs, timeout_callback_t *callback,
 			    void *context);
+#define timeout_add(msecs, callback, context) \
+	CONTEXT_CALLBACK(timeout_add, timeout_callback_t, \
+			 callback, context, msecs)
 /* Remove timeout handler, and set timeout pointer to NULL. */
 void timeout_remove(struct timeout **timeout);
 

Index: lib-signals.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/lib-signals.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- lib-signals.c	18 Nov 2006 14:07:40 -0000	1.14
+++ lib-signals.c	15 Dec 2006 16:55:32 -0000	1.15
@@ -99,11 +99,14 @@
 	}
 }
 
+#undef lib_signals_set_handler
 void lib_signals_set_handler(int signo, bool delayed,
 			     signal_handler_t *handler, void *context)
 {
 	struct signal_handler *h;
 
+	i_assert(handler != NULL);
+
 	if (signo < 0 || signo > MAX_SIGNAL_VALUE) {
 		i_panic("Trying to set signal %d handler, but max is %d",
 			signo, MAX_SIGNAL_VALUE);
@@ -116,16 +119,10 @@
 		if (sigemptyset(&act.sa_mask) < 0)
 			i_fatal("sigemptyset(): %m");
 		act.sa_flags = 0;
-		act.sa_handler = handler != NULL ? sig_handler : sig_ignore;
+		act.sa_handler = sig_handler;
 		if (sigaction(signo, &act, NULL) < 0)
 			i_fatal("sigaction(%d): %m", signo);
-
-		if (handler == NULL) {
-			/* we're ignoring the handler, just return */
-			return;
-		}
 	}
-	i_assert(handler != NULL);
 
 	if (delayed && sig_pipe_fd[0] == -1) {
 		/* first delayed handler */
@@ -146,7 +143,7 @@
 	signal_handlers[signo] = h;
 }
 
-void lib_signals_ignore(int signo)
+void lib_signals_ignore(int signo, bool restart_syscalls)
 {
 	struct sigaction act;
 
@@ -159,13 +156,14 @@
 
 	if (sigemptyset(&act.sa_mask) < 0)
 		i_fatal("sigemptyset(): %m");
-	act.sa_flags = SA_RESTART;
-	act.sa_handler = SIG_IGN;
+	act.sa_flags = restart_syscalls ? SA_RESTART : 0;
+	act.sa_handler = restart_syscalls ? SIG_IGN : sig_ignore;
 
 	if (sigaction(signo, &act, NULL) < 0)
 		i_fatal("sigaction(%d): %m", signo);
 }
 
+#undef lib_signals_unset_handler
 void lib_signals_unset_handler(int signo, signal_handler_t *handler,
 			       void *context)
 {

Index: lib-signals.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/lib-signals.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- lib-signals.h	3 May 2006 22:58:15 -0000	1.7
+++ lib-signals.h	15 Dec 2006 16:55:32 -0000	1.8
@@ -6,15 +6,19 @@
 typedef void signal_handler_t(int signo, void *context);
 
 /* Set signal handler for specific signal. If delayed is TRUE, the handler
-   will be called later, ie. not as a real signal handler. If handler is NULL,
-   the signal is ignored. */
+   will be called later, ie. not as a real signal handler. */
 void lib_signals_set_handler(int signo, bool delayed,
 			     signal_handler_t *handler, void *context);
-/* Ignore given signal. The difference to lib_signals_set_handler() with NULL
-   handler is that this function tries to restart the system calls. */
-void lib_signals_ignore(int signo);
+#define lib_signals_set_handler(signo, delayed, handler, context) \
+	CONTEXT_CALLBACK2(lib_signals_set_handler, signal_handler_t, \
+			  handler, context, signo, delayed)
+/* Ignore given signal. */
+void lib_signals_ignore(int signo, bool restart_syscalls);
 void lib_signals_unset_handler(int signo,
 			       signal_handler_t *handler, void *context);
+#define lib_signals_unset_handler(signo, handler, context) \
+	CONTEXT_CALLBACK2(lib_signals_unset_handler, signal_handler_t, \
+			  handler, context, signo)
 
 void lib_signals_init(void);
 void lib_signals_deinit(void);

Index: macros.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/macros.h,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -d -r1.19 -r1.20
--- macros.h	18 Nov 2006 14:05:14 -0000	1.19
+++ macros.h	15 Dec 2006 16:55:32 -0000	1.20
@@ -128,6 +128,35 @@
 #  define MEMBER(name)
 #endif
 
+/* Macros to provide type safety for callback functions' context parameters */
+#ifdef __GNUC__
+#  define CONTEXT_TYPE_SAFETY
+#endif
+#ifdef CONTEXT_TYPE_SAFETY
+#  define CONTEXT_CALLBACK(name, callback_type, callback, context, ...) \
+	({(void)(1 ? 0 : callback(context)); \
+	name(__VA_ARGS__, (callback_type *)callback, context); })
+#  define CONTEXT_CALLBACK2(name, callback_type, callback, context, ...) \
+	({(void)(1 ? 0 : callback(0, context)); \
+	name(__VA_ARGS__, (callback_type *)callback, context); })
+#  define CONTEXT_CALLBACK3(name, callback_type, callback, context, ...) \
+	({(void)(1 ? 0 : callback(0, 0, context)); \
+	name(__VA_ARGS__, (callback_type *)callback, context); })
+#  define CONTEXT_CALLBACK4(name, callback_type, callback, context, ...) \
+	({(void)(1 ? 0 : callback(0, 0, 0, context)); \
+	name(__VA_ARGS__, (callback_type *)callback, context); })
+#  define CONTEXT_CALLBACK5(name, callback_type, callback, context, ...) \
+	({(void)(1 ? 0 : callback(0, 0, 0, 0, context)); \
+	name(__VA_ARGS__, (callback_type *)callback, context); })
+#else
+#  define CONTEXT_CALLBACK(name, callback_type, callback, context, ...) \
+	name(__VA_ARGS__, (callback_type *)callback, context)
+#  define CONTEXT_CALLBACK2 CONTEXT_CALLBACK
+#  define CONTEXT_CALLBACK3 CONTEXT_CALLBACK
+#  define CONTEXT_CALLBACK4 CONTEXT_CALLBACK
+#  define CONTEXT_CALLBACK5 CONTEXT_CALLBACK
+#endif
+
 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
    macros, so we can refer to them as strings unconditionally. */
 #ifdef __GNUC__

Index: ostream.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/ostream.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- ostream.c	18 Nov 2006 23:31:16 -0000	1.17
+++ ostream.c	15 Dec 2006 16:55:33 -0000	1.18
@@ -27,6 +27,7 @@
 	stream->closed = TRUE;
 }
 
+#undef o_stream_set_flush_callback
 void o_stream_set_flush_callback(struct ostream *stream,
 				 stream_flush_callback_t *callback,
 				 void *context)
@@ -37,6 +38,14 @@
 	_stream->context = context;
 }
 
+void o_stream_unset_flush_callback(struct ostream *stream)
+{
+	struct _ostream *_stream = stream->real_stream;
+
+	_stream->callback = NULL;
+	_stream->context = NULL;
+}
+
 void o_stream_set_max_buffer_size(struct ostream *stream, size_t max_size)
 {
 	_io_stream_set_max_buffer_size(&stream->real_stream->iostream,

Index: ostream.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/ostream.h,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- ostream.h	26 Feb 2006 10:05:06 -0000	1.15
+++ ostream.h	15 Dec 2006 16:55:33 -0000	1.16
@@ -43,6 +43,10 @@
 void o_stream_set_flush_callback(struct ostream *stream,
 				 stream_flush_callback_t *callback,
 				 void *context);
+#define o_stream_set_flush_callback(stream, callback, context) \
+	CONTEXT_CALLBACK(o_stream_set_flush_callback, stream_flush_callback_t, \
+			 callback, context, stream)
+void o_stream_unset_flush_callback(struct ostream *stream);
 /* Change the maximum size for stream's output buffer to grow. */
 void o_stream_set_max_buffer_size(struct ostream *stream, size_t max_size);
 



More information about the dovecot-cvs mailing list