Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Started zsys class for various non-portable system functions #106

Merged
merged 6 commits into from
This page is out of date. Refresh to see the latest.
View
2  configure.in
@@ -7,7 +7,7 @@ AC_PREREQ(2.61)
# Version must be MAJOR.MINOR.PATCH otherwise things will break.
# Make sure you also updated include/czmq.h to match.
#
-AC_INIT([czmq],[1.3.2],[zeromq-dev@lists.zeromq.org])
+AC_INIT([czmq],[1.3.3],[zeromq-dev@lists.zeromq.org])
AC_CONFIG_AUX_DIR(config)
AC_CONFIG_MACRO_DIR(config)
View
1  doc/Makefile.am
@@ -13,6 +13,7 @@ MAN7 = czmq.7 \
zsocket.7 \
zsockopt.7 \
zstr.7 \
+ zsys.7 \
zthread.7
MAN_DOC = $(MAN1) $(MAN3) $(MAN7)
View
41 doc/zsys.txt
@@ -0,0 +1,41 @@
+zsys(7)
+=======
+
+NAME
+----
+zsys - system wrapper
+
+SYNOPSIS
+--------
+----
+// Create a file path if it doesn't exit
+CZMQ_EXPORT void
+ zsys_mkdir (zsys_t *self, char *pathname);
+
+// Remove a file path with multiple levels
+CZMQ_EXPORT void
+ zsys_rmdir (zsys_t *self, char *pathname, bool force);
+
+// Self test of this class
+int
+ zsys_test (bool verbose);
+----
+
+DESCRIPTION
+-----------
+
+The zsys class provides a portable wrapper for miscellaneous functions
+that we want to wrap but which don't fit into any of the existing
+classes. Eventually all non-portable functionality might be moved here
+but for now it covers only file systems.
+
+
+EXAMPLE
+-------
+.From zsys_test method
+----
+----
+
+SEE ALSO
+--------
+linkczmq:czmq[7]
View
3  include/czmq.h
@@ -35,7 +35,7 @@
#define CZMQ_VERSION_MAJOR 1
#define CZMQ_VERSION_MINOR 3
-#define CZMQ_VERSION_PATCH 2
+#define CZMQ_VERSION_PATCH 4
#define CZMQ_MAKE_VERSION(major, minor, patch) \
((major) * 10000 + (minor) * 100 + (patch))
@@ -56,6 +56,7 @@
#include "zsocket.h"
#include "zsockopt.h"
#include "zstr.h"
+#include "zsys.h"
#include "zthread.h"
#endif
View
4 include/czmq_prelude.h
@@ -468,8 +468,6 @@ static inline void *
# define CZMQ_EXPORT
#endif
-#endif
-
//- Always include ZeroMQ header file ---------------------------------------
#include "zmq.h"
@@ -499,3 +497,5 @@ static inline void *
#elif ZMQ_VERSION_MAJOR == 3 || ZMQ_VERSION_MAJOR == 4
# define ZMQ_POLL_MSEC 1 // zmq_poll is msec
#endif
+
+#endif
View
20 include/zstr.h
@@ -40,14 +40,21 @@ CZMQ_EXPORT char *
CZMQ_EXPORT char *
zstr_recv_nowait (void *socket);
-// Send a string to a socket in 0MQ string format
+// Send a formatted string to a socket
CZMQ_EXPORT int
- zstr_send (void *socket, const char *string);
+ zstr_send (void *socket, const char *format, ...);
-// Send a string to a socket in 0MQ string format, with MORE flag
+// Send a formatted string to a socket, with MORE flag
CZMQ_EXPORT int
- zstr_sendm (void *socket, const char *string);
+ zstr_sendm (void *socket, const char *format, ...);
+
+// Self test of this class
+int
+ zstr_test (bool verbose);
+// @end
+// Deprecated methods
+// -----------------------------------------------------------
// Send a formatted string to a socket
CZMQ_EXPORT int
zstr_sendf (void *socket, const char *format, ...);
@@ -56,11 +63,6 @@ CZMQ_EXPORT int
CZMQ_EXPORT int
zstr_sendfm (void *socket, const char *format, ...);
-// Self test of this class
-int
- zstr_test (bool verbose);
-// @end
-
#ifdef __cplusplus
}
#endif
View
64 include/zsys.h
@@ -0,0 +1,64 @@
+/* =========================================================================
+ zsys - system wrapper
+
+ -------------------------------------------------------------------------
+ Copyright (c) 1991-2012 iMatix Corporation <www.imatix.com>
+ Copyright other contributors as noted in the AUTHORS file.
+
+ This file is part of CZMQ, the high-level C binding for 0MQ:
+ http://czmq.zeromq.org.
+
+ This is free software; you can redistribute it and/or modify it under
+ the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or (at
+ your option) any later version.
+
+ This software is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this program. If not, see
+ <http://www.gnu.org/licenses/>.
+ =========================================================================
+*/
+
+#ifndef __ZFL_SYS_H_INCLUDED__
+#define __ZFL_SYS_H_INCLUDED__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// @interface
+// Return file mode
+CZMQ_EXPORT mode_t
+ zsys_mode (const char *filename);
+
+// Create a file path if it doesn't exit
+CZMQ_EXPORT void
+ zsys_mkdir (const char *pathname);
+
+// Remove a file path if empty
+CZMQ_EXPORT void
+ zsys_rmdir (const char *pathname);
+
+// Delete a file
+CZMQ_EXPORT void
+ zsys_unlink (const char *filename);
+
+// Check if file is 'stable'
+CZMQ_EXPORT bool
+ zsys_stable (const char *filename);
+
+// Self test of this class
+int
+ zsys_test (bool verbose);
+// @end
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
View
2  src/Makefile.am
@@ -18,6 +18,7 @@ include_HEADERS = \
../include/zsocket.h \
../include/zsockopt.h \
../include/zstr.h \
+ ../include/zsys.h \
../include/zthread.h
libczmq_la_SOURCES = \
@@ -33,6 +34,7 @@ libczmq_la_SOURCES = \
zsocket.c \
zsockopt.c \
zstr.c \
+ zsys.c \
zthread.c
INCLUDES = -I$(top_srcdir)/include
View
1  src/czmq_selftest.c
@@ -50,6 +50,7 @@ int main (int argc, char *argv [])
zsocket_test (verbose);
zsockopt_test (verbose);
zstr_test (verbose);
+ zsys_test (verbose);
zthread_test (verbose);
printf ("Tests passed OK\n");
return 0;
View
16 src/zctx.c
@@ -102,13 +102,15 @@ zctx_new (void)
self->main = true;
#if defined (__UNIX__)
- // Install signal handler for SIGINT and SIGTERM
- struct sigaction action;
- action.sa_handler = s_signal_handler;
- action.sa_flags = 0;
- sigemptyset (&action.sa_mask);
- sigaction (SIGINT, &action, NULL);
- sigaction (SIGTERM, &action, NULL);
+ if (zctx_interrupted == 0) {
+ // Install signal handler for SIGINT and SIGTERM
+ struct sigaction action;
+ action.sa_handler = s_signal_handler;
+ action.sa_flags = 0;
+ sigemptyset (&action.sa_mask);
+ sigaction (SIGINT, &action, NULL);
+ sigaction (SIGTERM, &action, NULL);
+ }
#endif
return self;
}
View
110 src/zstr.c
@@ -36,6 +36,32 @@
#include "../include/czmq.h"
+static int
+s_send_string (void *zocket, bool more, const char *format, va_list argptr)
+{
+ assert (zocket);
+
+ // Format string into buffer
+ int size = 255 + 1;
+ char *string = (char *) malloc (size);
+ int required = vsnprintf (string, size, format, argptr);
+ if (required >= size) {
+ size = required + 1;
+ string = (char *) realloc (string, size);
+ vsnprintf (string, size, format, argptr);
+ }
+ // Now send formatted string
+ zmq_msg_t message;
+ zmq_msg_init_size (&message, strlen (string));
+ memcpy (zmq_msg_data (&message), string, strlen (string));
+ int rc = zmq_sendmsg (zocket, &message, more? ZMQ_SNDMORE: 0);
+ zmq_msg_close (&message);
+
+ free (string);
+ return rc == -1? -1: 0;
+}
+
+
// --------------------------------------------------------------------------
// Receive C string from socket. Caller must free returned string. Returns
// NULL if the context is being terminated or the process was interrupted.
@@ -87,90 +113,56 @@ zstr_recv_nowait (void *zocket)
// error.
int
-zstr_send (void *zocket, const char *string)
+zstr_send (void *zocket, const char *format, ...)
{
assert (zocket);
- assert (string);
-
- zmq_msg_t message;
- zmq_msg_init_size (&message, strlen (string));
- memcpy (zmq_msg_data (&message), string, strlen (string));
- int rc = zmq_sendmsg (zocket, &message, 0);
- zmq_msg_close (&message);
- return rc == -1? -1: 0;
+ assert (format);
+ va_list argptr;
+ va_start (argptr, format);
+ int rc = s_send_string (zocket, false, format, argptr);
+ va_end (argptr);
+ return rc;
}
-
-// --------------------------------------------------------------------------
-// Send a string to a socket in 0MQ string format, with MORE flag
+// Deprecated, does exactly the same
int
-zstr_sendm (void *zocket, const char *string)
-{
- assert (zocket);
- assert (string);
-
- zmq_msg_t message;
- zmq_msg_init_size (&message, strlen (string));
- memcpy (zmq_msg_data (&message), string, strlen (string));
- int rc = zmq_sendmsg (zocket, &message, ZMQ_SNDMORE);
- zmq_msg_close (&message);
- return rc == -1? -1: 0;
-}
-
-static int
-s_zstr_sendf_impl (void *zocket, bool more, const char *format, va_list argptr)
+zstr_sendf (void *zocket, const char *format, ...)
{
assert (zocket);
-
- // Format string into buffer
- int size = 255 + 1;
- char *string = (char *) malloc (size);
- int required = vsnprintf (string, size, format, argptr);
- if (required >= size) {
- size = required + 1;
- string = (char *) realloc (string, size);
- vsnprintf (string, size, format, argptr);
- }
-
- // Now send formatted string
- int rc;
- if (more)
- rc = zstr_sendm (zocket, string);
- else
- rc = zstr_send (zocket, string);
- free (string);
+ assert (format);
+ va_list argptr;
+ va_start (argptr, format);
+ int rc = s_send_string (zocket, false, format, argptr);
+ va_end (argptr);
return rc;
}
+
// --------------------------------------------------------------------------
-// Send formatted C string to socket
+// Send a string to a socket in 0MQ string format, with MORE flag
+
int
-zstr_sendf (void *zocket, const char *format, ...)
+zstr_sendm (void *zocket, const char *format, ...)
{
assert (zocket);
-
+ assert (format);
va_list argptr;
va_start (argptr, format);
-
- int rc = s_zstr_sendf_impl (zocket, false, format, argptr);
+ int rc = s_send_string (zocket, true, format, argptr);
va_end (argptr);
-
return rc;
}
-// --------------------------------------------------------------------------
-// Send formatted C string to socket with MORE flag
+// Deprecated, does exactly the same
int
zstr_sendfm (void *zocket, const char *format, ...)
{
assert (zocket);
-
+ assert (format);
va_list argptr;
va_start (argptr, format);
-
- int rc = s_zstr_sendf_impl (zocket, true, format, argptr);
+ int rc = s_send_string (zocket, true, format, argptr);
va_end (argptr);
-
return rc;
}
@@ -196,9 +188,9 @@ zstr_test (bool verbose)
// Send ten strings, five strings with MORE flag and then END
int string_nbr;
for (string_nbr = 0; string_nbr < 10; string_nbr++)
- zstr_sendf (output, "this is string %d", string_nbr);
+ zstr_send (output, "this is string %d", string_nbr);
for (string_nbr = 0; string_nbr < 5; string_nbr++)
- zstr_sendfm (output, "this is string %d", string_nbr);
+ zstr_sendm (output, "this is string %d", string_nbr);
zstr_send (output, "END");
// Read and count until we receive END
View
170 src/zsys.c
@@ -0,0 +1,170 @@
+/* =========================================================================
+ zsys - system wrapper
+
+ -------------------------------------------------------------------------
+ Copyright (c) 1991-2012 iMatix Corporation <www.imatix.com>
+ Copyright other contributors as noted in the AUTHORS file.
+
+ This file is part of CZMQ, the high-level C binding for 0MQ:
+ http://czmq.zeromq.org.
+
+ This is free software; you can redistribute it and/or modify it under
+ the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or (at
+ your option) any later version.
+
+ This software is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this program. If not, see
+ <http://www.gnu.org/licenses/>.
+ =========================================================================
+*/
+
+/*
+@header
+ The zsys class provides a portable wrapper for miscellaneous functions
+ that we want to wrap but which don't fit into any of the existing
+ classes. Eventually all non-portable functionality might be moved here
+ but for now it covers only file systems.
+@discuss
+@end
+*/
+
+#include "../include/czmq.h"
+
+
+// --------------------------------------------------------------------------
+// Return file mode
+
+CZMQ_EXPORT mode_t
+zsys_mode (const char *filename)
+{
+#if (defined (__WINDOWS__))
+ DWORD dwfa = GetFileAttributes (filename);
+ if (dwfa == 0xffffffff)
+ return -1;
+
+ dbyte mode = 0;
+ if (dwfa & FILE_ATTRIBUTE_DIRECTORY)
+ mode |= S_IFDIR;
+ else
+ mode |= S_IFREG;
+ if (!(dwfa & FILE_ATTRIBUTE_HIDDEN))
+ mode |= S_IREAD;
+ if (!(dwfa & FILE_ATTRIBUTE_READONLY))
+ mode |= S_IWRITE;
+
+ return mode;
+#else
+ struct stat stat_buf;
+ if (stat ((char *) filename, &stat_buf) == 0)
+ return stat_buf.st_mode;
+ else
+ return -1;
+#endif
+}
+
+
+// --------------------------------------------------------------------------
+// Create a file path if it doesn't exit
+
+void
+zsys_mkdir (const char *pathname)
+{
+ // Take copy of string as we're going to mess with it
+ char *my_pathname = strdup (pathname);
+
+ // Create parent directory levels if needed
+ char *slash = strchr (my_pathname + 1, '/');
+ do {
+ if (slash)
+ *slash = 0; // Cut at slash
+ mode_t mode = zsys_mode (my_pathname);
+ if (mode == (mode_t)-1) {
+ // Does not exist, try to create it
+#if (defined (__WINDOWS__))
+ if (!CreateDirectory (my_pathname, NULL))
+#else
+ if (mkdir (my_pathname, 0775))
+#endif
+ return; // Failed
+ }
+ else
+ if ((mode & S_IFDIR) == 0) {
+ // Not a directory, abort
+ }
+ if (!slash) // End if last segment
+ break;
+ *slash = '/';
+ slash = strchr (slash + 1, '/');
+ } while (slash);
+
+ free (my_pathname);
+}
+
+
+// --------------------------------------------------------------------------
+// Remove a file path if empty
+
+void
+zsys_rmdir (const char *pathname)
+{
+#if (defined (__WINDOWS__))
+ RemoveDirectory (pathname);
+#else
+ rmdir (pathname);
+#endif
+}
+
+
+// --------------------------------------------------------------------------
+// Delete a file
+
+CZMQ_EXPORT void
+zsys_unlink (const char *filename)
+{
+#if (defined (__WINDOWS__))
+ DeleteFile (filename);
+#else
+ unlink (filename);
+#endif
+}
+
+
+// --------------------------------------------------------------------------
+// Check if file is 'stable'
+
+CZMQ_EXPORT bool
+zsys_stable (const char *filename)
+{
+ struct stat stat_buf;
+ if (stat (filename, &stat_buf) == 0) {
+ // File is 'stable' if more than 1 second old
+#if (defined (WIN32))
+# define EPOCH_DIFFERENCE 11644473600LL
+ long age = (long) (zclock_time () - EPOCH_DIFFERENCE * 1000 - (stat_buf.st_mtime * 1000));
+#else
+ long age = (long) (zclock_time () - (stat_buf.st_mtime * 1000));
+#endif
+ return (age > 1000);
+ }
+ else
+ return false; // File doesn't exist, so not stable
+}
+
+
+// --------------------------------------------------------------------------
+// Selftest
+
+int
+zsys_test (bool verbose)
+{
+ printf (" * zsys: ");
+
+ printf ("OK\n");
+ return 0;
+}
View
14 src/zthread.c
@@ -56,11 +56,13 @@
thread to a child thread, if you stop using it in the parent thread
immediately after creating the child thread. If you want to connect
sockets over inproc:// they must share the same 0MQ context, i.e. be
- attached threads. If you want to communicate over ipc:// or tcp://
- you can be sharing the same context, or use separate contexts.
+ attached threads. You should always use zthread_fork to create an
+ attached thread; it is not sufficient to pass a zctx_t structure to
+ a detached thread (this will crash).
- Thus, every detached thread usually starts by creating its own zctx_t
- instance.
+ If you want to communicate over ipc:// or tcp:// you may be sharing
+ the same context, or use separate contexts. Thus, every detached thread
+ usually starts by creating its own zctx_t instance.
@end
*/
@@ -139,9 +141,9 @@ s_thread_start (shim_t *shim)
// Set child thread priority to same as current
int priority = GetThreadPriority (GetCurrentThread ());
SetThreadPriority (shim->handle, priority);
- // Now start thread
+ // Start thread
ResumeThread (shim->handle);
-
+ // Release resources
CloseHandle (shim->handle);
shim->handle = 0;
#endif
Something went wrong with that request. Please try again.