1ZSYS(3)                           CZMQ Manual                          ZSYS(3)
2
3
4

NAME

6       zsys - Class for system-level methods
7

SYNOPSIS

9       //  This is a stable class, and may not change except for emergencies. It
10       //  is provided in stable builds.
11       //  This class has draft methods, which may change over time. They are not
12       //  in stable releases, by default. Use --enable-drafts to enable.
13       // Callback for interrupt signal handler
14       typedef void (zsys_handler_fn) (
15           int signal_value);
16
17       //  Initialize CZMQ zsys layer; this happens automatically when you create
18       //  a socket or an actor; however this call lets you force initialization
19       //  earlier, so e.g. logging is properly set-up before you start working.
20       //  Not threadsafe, so call only from main thread. Safe to call multiple
21       //  times. Returns global CZMQ context.
22       CZMQ_EXPORT void *
23           zsys_init (void);
24
25       //  Optionally shut down the CZMQ zsys layer; this normally happens automatically
26       //  when the process exits; however this call lets you force a shutdown
27       //  earlier, avoiding any potential problems with atexit() ordering, especially
28       //  with Windows dlls.
29       CZMQ_EXPORT void
30           zsys_shutdown (void);
31
32       //  Get a new ZMQ socket, automagically creating a ZMQ context if this is
33       //  the first time. Caller is responsible for destroying the ZMQ socket
34       //  before process exits, to avoid a ZMQ deadlock. Note: you should not use
35       //  this method in CZMQ apps, use zsock_new() instead.
36       //  *** This is for CZMQ internal use only and may change arbitrarily ***
37       CZMQ_EXPORT void *
38           zsys_socket (int type, const char *filename, size_t line_nbr);
39
40       //  Destroy/close a ZMQ socket. You should call this for every socket you
41       //  create using zsys_socket().
42       //  *** This is for CZMQ internal use only and may change arbitrarily ***
43       CZMQ_EXPORT int
44           zsys_close (void *handle, const char *filename, size_t line_nbr);
45
46       //  Return ZMQ socket name for socket type
47       //  *** This is for CZMQ internal use only and may change arbitrarily ***
48       CZMQ_EXPORT char *
49           zsys_sockname (int socktype);
50
51       //  Create a pipe, which consists of two PAIR sockets connected over inproc.
52       //  The pipe is configured to use the zsys_pipehwm setting. Returns the
53       //  frontend socket successful, NULL if failed.
54       CZMQ_EXPORT zsock_t *
55           zsys_create_pipe (zsock_t **backend_p);
56
57       //  Set interrupt handler; this saves the default handlers so that a
58       //  zsys_handler_reset () can restore them. If you call this multiple times
59       //  then the last handler will take affect. If handler_fn is NULL, disables
60       //  default SIGINT/SIGTERM handling in CZMQ.
61       CZMQ_EXPORT void
62           zsys_handler_set (zsys_handler_fn *handler_fn);
63
64       //  Reset interrupt handler, call this at exit if needed
65       CZMQ_EXPORT void
66           zsys_handler_reset (void);
67
68       //  Set default interrupt handler, so Ctrl-C or SIGTERM will set
69       //  zsys_interrupted. Idempotent; safe to call multiple times.
70       //  Can be suppressed by ZSYS_SIGHANDLER=false
71       //  *** This is for CZMQ internal use only and may change arbitrarily ***
72       CZMQ_EXPORT void
73           zsys_catch_interrupts (void);
74
75       //  Return 1 if file exists, else zero
76       CZMQ_EXPORT bool
77           zsys_file_exists (const char *filename);
78
79       //  Return file modification time. Returns 0 if the file does not exist.
80       CZMQ_EXPORT time_t
81           zsys_file_modified (const char *filename);
82
83       //  Return file mode; provides at least support for the POSIX S_ISREG(m)
84       //  and S_ISDIR(m) macros and the S_IRUSR and S_IWUSR bits, on all boxes.
85       //  Returns a mode_t cast to int, or -1 in case of error.
86       CZMQ_EXPORT int
87           zsys_file_mode (const char *filename);
88
89       //  Delete file. Does not complain if the file is absent
90       CZMQ_EXPORT int
91           zsys_file_delete (const char *filename);
92
93       //  Check if file is 'stable'
94       CZMQ_EXPORT bool
95           zsys_file_stable (const char *filename);
96
97       //  Create a file path if it doesn't exist. The file path is treated as
98       //  printf format.
99       CZMQ_EXPORT int
100           zsys_dir_create (const char *pathname, ...);
101
102       //  Remove a file path if empty; the pathname is treated as printf format.
103       CZMQ_EXPORT int
104           zsys_dir_delete (const char *pathname, ...);
105
106       //  Move to a specified working directory. Returns 0 if OK, -1 if this failed.
107       CZMQ_EXPORT int
108           zsys_dir_change (const char *pathname);
109
110       //  Set private file creation mode; all files created from here will be
111       //  readable/writable by the owner only.
112       CZMQ_EXPORT void
113           zsys_file_mode_private (void);
114
115       //  Reset default file creation mode; all files created from here will use
116       //  process file mode defaults.
117       CZMQ_EXPORT void
118           zsys_file_mode_default (void);
119
120       //  Return the CZMQ version for run-time API detection; returns version
121       //  number into provided fields, providing reference isn't null in each case.
122       CZMQ_EXPORT void
123           zsys_version (int *major, int *minor, int *patch);
124
125       //  Format a string using printf formatting, returning a freshly allocated
126       //  buffer. If there was insufficient memory, returns NULL. Free the returned
127       //  string using zstr_free().
128       CZMQ_EXPORT char *
129           zsys_sprintf (const char *format, ...);
130
131       //  Format a string with a va_list argument, returning a freshly allocated
132       //  buffer. If there was insufficient memory, returns NULL. Free the returned
133       //  string using zstr_free().
134       CZMQ_EXPORT char *
135           zsys_vprintf (const char *format, va_list argptr);
136
137       //  Create UDP beacon socket; if the routable option is true, uses
138       //  multicast (not yet implemented), else uses broadcast. This method
139       //  and related ones might _eventually_ be moved to a zudp class.
140       //  *** This is for CZMQ internal use only and may change arbitrarily ***
141       CZMQ_EXPORT SOCKET
142           zsys_udp_new (bool routable);
143
144       //  Close a UDP socket
145       //  *** This is for CZMQ internal use only and may change arbitrarily ***
146       CZMQ_EXPORT int
147           zsys_udp_close (SOCKET handle);
148
149       //  Send zframe to UDP socket, return -1 if sending failed due to
150       //  interface having disappeared (happens easily with WiFi)
151       //  *** This is for CZMQ internal use only and may change arbitrarily ***
152       CZMQ_EXPORT int
153           zsys_udp_send (SOCKET udpsock, zframe_t *frame, inaddr_t *address, int addrlen);
154
155       //  Receive zframe from UDP socket, and set address of peer that sent it
156       //  The peername must be a char [INET_ADDRSTRLEN] array if IPv6 is disabled or
157       //  NI_MAXHOST if it's enabled. Returns NULL when failing to get peer address.
158       //  *** This is for CZMQ internal use only and may change arbitrarily ***
159       CZMQ_EXPORT zframe_t *
160           zsys_udp_recv (SOCKET udpsock, char *peername, int peerlen);
161
162       //  Handle an I/O error on some socket operation; will report and die on
163       //  fatal errors, and continue silently on "try again" errors.
164       //  *** This is for CZMQ internal use only and may change arbitrarily ***
165       CZMQ_EXPORT void
166           zsys_socket_error (const char *reason);
167
168       //  Return current host name, for use in public tcp:// endpoints. Caller gets
169       //  a freshly allocated string, should free it using zstr_free(). If the host
170       //  name is not resolvable, returns NULL.
171       CZMQ_EXPORT char *
172           zsys_hostname (void);
173
174       //  Move the current process into the background. The precise effect depends
175       //  on the operating system. On POSIX boxes, moves to a specified working
176       //  directory (if specified), closes all file handles, reopens stdin, stdout,
177       //  and stderr to the null device, and sets the process to ignore SIGHUP. On
178       //  Windows, does nothing. Returns 0 if OK, -1 if there was an error.
179       CZMQ_EXPORT int
180           zsys_daemonize (const char *workdir);
181
182       //  Drop the process ID into the lockfile, with exclusive lock, and switch
183       //  the process to the specified group and/or user. Any of the arguments
184       //  may be null, indicating a no-op. Returns 0 on success, -1 on failure.
185       //  Note if you combine this with zsys_daemonize, run after, not before
186       //  that method, or the lockfile will hold the wrong process ID.
187       CZMQ_EXPORT int
188           zsys_run_as (const char *lockfile, const char *group, const char *user);
189
190       //  Returns true if the underlying libzmq supports CURVE security.
191       //  Uses a heuristic probe according to the version of libzmq being used.
192       CZMQ_EXPORT bool
193           zsys_has_curve (void);
194
195       //  Configure the number of I/O threads that ZeroMQ will use. A good
196       //  rule of thumb is one thread per gigabit of traffic in or out. The
197       //  default is 1, sufficient for most applications. If the environment
198       //  variable ZSYS_IO_THREADS is defined, that provides the default.
199       //  Note that this method is valid only before any socket is created.
200       CZMQ_EXPORT void
201           zsys_set_io_threads (size_t io_threads);
202
203       //  Configure the scheduling policy of the ZMQ context thread pool.
204       //  Not available on Windows. See the sched_setscheduler man page or sched.h
205       //  for more information. If the environment variable ZSYS_THREAD_SCHED_POLICY
206       //  is defined, that provides the default.
207       //  Note that this method is valid only before any socket is created.
208       CZMQ_EXPORT void
209           zsys_set_thread_sched_policy (int policy);
210
211       //  Configure the scheduling priority of the ZMQ context thread pool.
212       //  Not available on Windows. See the sched_setscheduler man page or sched.h
213       //  for more information. If the environment variable ZSYS_THREAD_PRIORITY is
214       //  defined, that provides the default.
215       //  Note that this method is valid only before any socket is created.
216       CZMQ_EXPORT void
217           zsys_set_thread_priority (int priority);
218
219       //  Configure the numeric prefix to each thread created for the internal
220       //  context's thread pool. This option is only supported on Linux.
221       //  If the environment variable ZSYS_THREAD_NAME_PREFIX is defined, that
222       //  provides the default.
223       //  Note that this method is valid only before any socket is created.
224       CZMQ_EXPORT void
225           zsys_set_thread_name_prefix (int prefix);
226
227       //  Return thread name prefix.
228       CZMQ_EXPORT int
229           zsys_thread_name_prefix (void);
230
231       //  Adds a specific CPU to the affinity list of the ZMQ context thread pool.
232       //  This option is only supported on Linux.
233       //  Note that this method is valid only before any socket is created.
234       CZMQ_EXPORT void
235           zsys_thread_affinity_cpu_add (int cpu);
236
237       //  Removes a specific CPU to the affinity list of the ZMQ context thread pool.
238       //  This option is only supported on Linux.
239       //  Note that this method is valid only before any socket is created.
240       CZMQ_EXPORT void
241           zsys_thread_affinity_cpu_remove (int cpu);
242
243       //  Configure the number of sockets that ZeroMQ will allow. The default
244       //  is 1024. The actual limit depends on the system, and you can query it
245       //  by using zsys_socket_limit (). A value of zero means "maximum".
246       //  Note that this method is valid only before any socket is created.
247       CZMQ_EXPORT void
248           zsys_set_max_sockets (size_t max_sockets);
249
250       //  Return maximum number of ZeroMQ sockets that the system will support.
251       CZMQ_EXPORT size_t
252           zsys_socket_limit (void);
253
254       //  Configure the maximum allowed size of a message sent.
255       //  The default is INT_MAX.
256       CZMQ_EXPORT void
257           zsys_set_max_msgsz (int max_msgsz);
258
259       //  Return maximum message size.
260       CZMQ_EXPORT int
261           zsys_max_msgsz (void);
262
263       //  Configure the default linger timeout in msecs for new zsock instances.
264       //  You can also set this separately on each zsock_t instance. The default
265       //  linger time is zero, i.e. any pending messages will be dropped. If the
266       //  environment variable ZSYS_LINGER is defined, that provides the default.
267       //  Note that process exit will typically be delayed by the linger time.
268       CZMQ_EXPORT void
269           zsys_set_linger (size_t linger);
270
271       //  Configure the default outgoing pipe limit (HWM) for new zsock instances.
272       //  You can also set this separately on each zsock_t instance. The default
273       //  HWM is 1,000, on all versions of ZeroMQ. If the environment variable
274       //  ZSYS_SNDHWM is defined, that provides the default. Note that a value of
275       //  zero means no limit, i.e. infinite memory consumption.
276       CZMQ_EXPORT void
277           zsys_set_sndhwm (size_t sndhwm);
278
279       //  Configure the default incoming pipe limit (HWM) for new zsock instances.
280       //  You can also set this separately on each zsock_t instance. The default
281       //  HWM is 1,000, on all versions of ZeroMQ. If the environment variable
282       //  ZSYS_RCVHWM is defined, that provides the default. Note that a value of
283       //  zero means no limit, i.e. infinite memory consumption.
284       CZMQ_EXPORT void
285           zsys_set_rcvhwm (size_t rcvhwm);
286
287       //  Configure the default HWM for zactor internal pipes; this is set on both
288       //  ends of the pipe, for outgoing messages only (sndhwm). The default HWM is
289       //  1,000, on all versions of ZeroMQ. If the environment var ZSYS_ACTORHWM is
290       //  defined, that provides the default. Note that a value of zero means no
291       //  limit, i.e. infinite memory consumption.
292       CZMQ_EXPORT void
293           zsys_set_pipehwm (size_t pipehwm);
294
295       //  Return the HWM for zactor internal pipes.
296       CZMQ_EXPORT size_t
297           zsys_pipehwm (void);
298
299       //  Configure use of IPv6 for new zsock instances. By default sockets accept
300       //  and make only IPv4 connections. When you enable IPv6, sockets will accept
301       //  and connect to both IPv4 and IPv6 peers. You can override the setting on
302       //  each zsock_t instance. The default is IPv4 only (ipv6 set to 0). If the
303       //  environment variable ZSYS_IPV6 is defined (as 1 or 0), this provides the
304       //  default. Note: has no effect on ZMQ v2.
305       CZMQ_EXPORT void
306           zsys_set_ipv6 (int ipv6);
307
308       //  Return use of IPv6 for zsock instances.
309       CZMQ_EXPORT int
310           zsys_ipv6 (void);
311
312       //  Set network interface name to use for broadcasts, particularly zbeacon.
313       //  This lets the interface be configured for test environments where required.
314       //  For example, on Mac OS X, zbeacon cannot bind to 255.255.255.255 which is
315       //  the default when there is no specified interface. If the environment
316       //  variable ZSYS_INTERFACE is set, use that as the default interface name.
317       //  Setting the interface to "*" means "use all available interfaces".
318       CZMQ_EXPORT void
319           zsys_set_interface (const char *value);
320
321       //  Return network interface to use for broadcasts, or "" if none was set.
322       CZMQ_EXPORT const char *
323           zsys_interface (void);
324
325       //  Set IPv6 address to use zbeacon socket, particularly for receiving zbeacon.
326       //  This needs to be set IPv6 is enabled as IPv6 can have multiple addresses
327       //  on a given interface. If the environment variable ZSYS_IPV6_ADDRESS is set,
328       //  use that as the default IPv6 address.
329       CZMQ_EXPORT void
330           zsys_set_ipv6_address (const char *value);
331
332       //  Return IPv6 address to use for zbeacon reception, or "" if none was set.
333       CZMQ_EXPORT const char *
334           zsys_ipv6_address (void);
335
336       //  Set IPv6 milticast address to use for sending zbeacon messages. This needs
337       //  to be set if IPv6 is enabled. If the environment variable
338       //  ZSYS_IPV6_MCAST_ADDRESS is set, use that as the default IPv6 multicast
339       //  address.
340       CZMQ_EXPORT void
341           zsys_set_ipv6_mcast_address (const char *value);
342
343       //  Return IPv6 multicast address to use for sending zbeacon, or "" if none was
344       //  set.
345       CZMQ_EXPORT const char *
346           zsys_ipv6_mcast_address (void);
347
348       //  Configure the automatic use of pre-allocated FDs when creating new sockets.
349       //  If 0 (default), nothing will happen. Else, when a new socket is bound, the
350       //  system API will be used to check if an existing pre-allocated FD with a
351       //  matching port (if TCP) or path (if IPC) exists, and if it does it will be
352       //  set via the ZMQ_USE_FD socket option so that the library will use it
353       //  instead of creating a new socket.
354       CZMQ_EXPORT void
355           zsys_set_auto_use_fd (int auto_use_fd);
356
357       //  Return use of automatic pre-allocated FDs for zsock instances.
358       CZMQ_EXPORT int
359           zsys_auto_use_fd (void);
360
361       //  Set log identity, which is a string that prefixes all log messages sent
362       //  by this process. The log identity defaults to the environment variable
363       //  ZSYS_LOGIDENT, if that is set.
364       CZMQ_EXPORT void
365           zsys_set_logident (const char *value);
366
367       //  Set stream to receive log traffic. By default, log traffic is sent to
368       //  stdout. If you set the stream to NULL, no stream will receive the log
369       //  traffic (it may still be sent to the system facility).
370       CZMQ_EXPORT void
371           zsys_set_logstream (FILE *stream);
372
373       //  Sends log output to a PUB socket bound to the specified endpoint. To
374       //  collect such log output, create a SUB socket, subscribe to the traffic
375       //  you care about, and connect to the endpoint. Log traffic is sent as a
376       //  single string frame, in the same format as when sent to stdout. The
377       //  log system supports a single sender; multiple calls to this method will
378       //  bind the same sender to multiple endpoints. To disable the sender, call
379       //  this method with a null argument.
380       CZMQ_EXPORT void
381           zsys_set_logsender (const char *endpoint);
382
383       //  Enable or disable logging to the system facility (syslog on POSIX boxes,
384       //  event log on Windows). By default this is disabled.
385       CZMQ_EXPORT void
386           zsys_set_logsystem (bool logsystem);
387
388       //  Log error condition - highest priority
389       CZMQ_EXPORT void
390           zsys_error (const char *format, ...);
391
392       //  Log warning condition - high priority
393       CZMQ_EXPORT void
394           zsys_warning (const char *format, ...);
395
396       //  Log normal, but significant, condition - normal priority
397       CZMQ_EXPORT void
398           zsys_notice (const char *format, ...);
399
400       //  Log informational message - low priority
401       CZMQ_EXPORT void
402           zsys_info (const char *format, ...);
403
404       //  Log debug-level message - lowest priority
405       CZMQ_EXPORT void
406           zsys_debug (const char *format, ...);
407
408       //  Self test of this class.
409       CZMQ_EXPORT void
410           zsys_test (bool verbose);
411
412       #ifdef CZMQ_BUILD_DRAFT_API
413       //  *** Draft method, for development use, may change without warning ***
414       //  Check if default interrupt handler of Ctrl-C or SIGTERM was called.
415       //  Does not work if ZSYS_SIGHANDLER is false and code does not call
416       //  set interrupted on signal.
417       CZMQ_EXPORT bool
418           zsys_is_interrupted (void);
419
420       //  *** Draft method, for development use, may change without warning ***
421       //  Set interrupted flag. This is done by default signal handler, however
422       //  this can be handy for language bindings or cases without default
423       //  signal handler.
424       CZMQ_EXPORT void
425           zsys_set_interrupted (void);
426
427       //  *** Draft method, for development use, may change without warning ***
428       //  Format a string using printf formatting, returning a freshly allocated
429       //  buffer. If there was insufficient memory, returns NULL. Free the returned
430       //  string using zstr_free(). The hinted version allows to optimize by using
431       //  a larger starting buffer size (known to/assumed by the developer) and so
432       //  avoid reallocations.
433       CZMQ_EXPORT char *
434           zsys_sprintf_hint (int hint, const char *format, ...);
435
436       //  *** Draft method, for development use, may change without warning ***
437       //  Configure whether to use zero copy strategy in libzmq. If the environment
438       //  variable ZSYS_ZERO_COPY_RECV is defined, that provides the default.
439       //  Otherwise the default is 1.
440       CZMQ_EXPORT void
441           zsys_set_zero_copy_recv (int zero_copy);
442
443       //  *** Draft method, for development use, may change without warning ***
444       //  Return ZMQ_ZERO_COPY_RECV option.
445       CZMQ_EXPORT int
446           zsys_zero_copy_recv (void);
447
448       //  *** Draft method, for development use, may change without warning ***
449       //  Configure the threshold value of filesystem object age per st_mtime
450       //  that should elapse until we consider that object "stable" at the
451       //  current zclock_time() moment.
452       //  The default is S_DEFAULT_ZSYS_FILE_STABLE_AGE_MSEC defined in zsys.c
453       //  which generally depends on host OS, with fallback value of 5000.
454       CZMQ_EXPORT void
455           zsys_set_file_stable_age_msec (int64_t file_stable_age_msec);
456
457       //  *** Draft method, for development use, may change without warning ***
458       //  Return current threshold value of file stable age in msec.
459       //  This can be used in code that chooses to wait for this timeout
460       //  before testing if a filesystem object is "stable" or not.
461       CZMQ_EXPORT int64_t
462           zsys_file_stable_age_msec (void);
463
464       //  *** Draft method, for development use, may change without warning ***
465       //  Print formatted string. Format is specified by variable names
466       //  in Python-like format style
467       //
468       //  "%(KEY)s=%(VALUE)s", KEY=key, VALUE=value
469       //  become
470       //  "key=value"
471       //
472       //  Returns freshly allocated string or NULL in a case of error.
473       //  Not enough memory, invalid format specifier, name not in args
474       //  Caller owns return value and must destroy it when done.
475       CZMQ_EXPORT char *
476           zsys_zprintf (const char *format, zhash_t *args);
477
478       //  *** Draft method, for development use, may change without warning ***
479       //  Return error string for given format/args combination.
480       //  Caller owns return value and must destroy it when done.
481       CZMQ_EXPORT char *
482           zsys_zprintf_error (const char *format, zhash_t *args);
483
484       //  *** Draft method, for development use, may change without warning ***
485       //  Print formatted string. Format is specified by variable names
486       //  in Python-like format style
487       //
488       //  "%(KEY)s=%(VALUE)s", KEY=key, VALUE=value
489       //  become
490       //  "key=value"
491       //
492       //  Returns freshly allocated string or NULL in a case of error.
493       //  Not enough memory, invalid format specifier, name not in args
494       //  Caller owns return value and must destroy it when done.
495       CZMQ_EXPORT char *
496           zsys_zplprintf (const char *format, zconfig_t *args);
497
498       //  *** Draft method, for development use, may change without warning ***
499       //  Return error string for given format/args combination.
500       //  Caller owns return value and must destroy it when done.
501       CZMQ_EXPORT char *
502           zsys_zplprintf_error (const char *format, zconfig_t *args);
503
504       #endif // CZMQ_BUILD_DRAFT_API
505       Please add '@interface' section in './../src/zsys.c'.
506

DESCRIPTION

508       The zsys class provides a portable wrapper for system calls. We collect
509       them here to reduce the number of weird #ifdefs in other classes. As
510       far as possible, the bulk of CZMQ classes are fully portable.
511
512       Please add @discuss section in ./../src/zsys.c.
513

EXAMPLE

515       From zsys_test method.
516
517           zsys_catch_interrupts ();
518
519           //  Check capabilities without using the return value
520           int rc = zsys_has_curve ();
521
522           const char *SELFTEST_DIR_RW = "src/selftest-rw";
523
524           if (verbose) {
525               char *hostname = zsys_hostname ();
526               zsys_info ("host name is %s", hostname);
527               freen (hostname);
528               zsys_info ("system limit is %zu ZeroMQ sockets", zsys_socket_limit ());
529           }
530           #ifdef CZMQ_BUILD_DRAFT_API
531           zsys_set_file_stable_age_msec (5123);
532           assert (zsys_file_stable_age_msec() == 5123);
533           zsys_set_file_stable_age_msec (-1);
534           assert (zsys_file_stable_age_msec() == 5123);
535           #endif // CZMQ_BUILD_DRAFT_API
536           zsys_set_linger (0);
537           zsys_set_sndhwm (1000);
538           zsys_set_rcvhwm (1000);
539           zsys_set_pipehwm (2500);
540           assert (zsys_pipehwm () == 2500);
541           zsys_set_ipv6 (0);
542           zsys_set_thread_priority (-1);
543           zsys_set_thread_sched_policy (-1);
544           zsys_set_thread_name_prefix (0);
545           assert (0 == zsys_thread_name_prefix());
546           zsys_thread_affinity_cpu_add (0);
547           zsys_thread_affinity_cpu_remove (0);
548           zsys_set_zero_copy_recv(0);
549           assert (0 == zsys_zero_copy_recv());
550           zsys_set_zero_copy_recv(1);
551           assert (1 == zsys_zero_copy_recv());
552
553           //  Test pipe creation
554           zsock_t *pipe_back;
555           zsock_t *pipe_front = zsys_create_pipe (&pipe_back);
556           zstr_send (pipe_front, "Hello");
557           char *string = zstr_recv (pipe_back);
558           assert (streq (string, "Hello"));
559           freen (string);
560           zsock_destroy (&pipe_back);
561           zsock_destroy (&pipe_front);
562
563           //  Test file manipulation
564
565           // Don't let anyone fool our workspace
566           if (zsys_file_exists ("nosuchfile")) {
567               zsys_warning ("zsys_test() had to remove 'nosuchfile' which was not expected here at all");
568               zsys_file_delete ("nosuchfile");
569           }
570
571           rc = zsys_file_delete ("nosuchfile");
572           assert (rc == -1);
573
574           bool rc_bool = zsys_file_exists ("nosuchfile");
575           assert (rc_bool != true);
576
577           rc = (int) zsys_file_size ("nosuchfile");
578           assert (rc == -1);
579
580           time_t when = zsys_file_modified (".");
581           assert (when > 0);
582
583           int mode = zsys_file_mode (".");
584           assert (S_ISDIR (mode));
585           assert (mode & S_IRUSR);
586           assert (mode & S_IWUSR);
587
588           const char *testbasedir  = ".testsys";
589           const char *testsubdir  = "subdir";
590           char *basedirpath = NULL;   // subdir in a test, under SELFTEST_DIR_RW
591           char *dirpath = NULL;       // subdir in a test, under basedirpath
592           char *relsubdir = NULL;     // relative short "path" of subdir under testbasedir
593
594           basedirpath = zsys_sprintf ("%s/%s", SELFTEST_DIR_RW, testbasedir);
595           assert (basedirpath);
596           dirpath = zsys_sprintf ("%s/%s", basedirpath, testsubdir);
597           assert (dirpath);
598           relsubdir = zsys_sprintf ("%s/%s", testbasedir, testsubdir);
599           assert (relsubdir);
600
601           // Normally tests clean up in the end, but if a selftest run dies
602           // e.g. on assert(), workspace remains dirty. Better clean it up.
603           // We do not really care about results here - we clean up a possible
604           // dirty exit of an older build. If there are permission errors etc.
605           // the actual tests below would explode.
606           if (zsys_file_exists(dirpath)) {
607               if (verbose)
608                   zsys_debug ("zsys_test() has to remove ./%s that should not have been here", dirpath);
609               zsys_dir_delete (dirpath);
610           }
611           if (zsys_file_exists (basedirpath)) {
612               if (verbose)
613                   zsys_debug ("zsys_test() has to remove ./%s that should not have been here", basedirpath);
614               zsys_dir_delete (basedirpath);
615           }
616
617           // Added tracing because this file-age check fails on some systems
618           // presumably due to congestion in a mass-build and valgrind on top
619           zsys_file_mode_private ();
620           if (verbose)
621               printf ("zsys_test() at timestamp %" PRIi64 ": "
622                   "Creating %s\n",
623                   zclock_time(), relsubdir );
624           rc = zsys_dir_create ("%s/%s", SELFTEST_DIR_RW, relsubdir);
625           if (verbose)
626               printf ("zsys_test() at timestamp %" PRIi64 ": "
627                   "Finished creating %s with return-code %d\n",
628                   zclock_time(), relsubdir, rc );
629           assert (rc == 0);
630           when = zsys_file_modified (dirpath);
631           if (verbose)
632               printf ("zsys_test() at timestamp %" PRIi64 ": "
633                   "Finished calling zsys_file_modified(), got age %jd\n",
634                   zclock_time(), (intmax_t)when );
635           assert (when > 0);
636           if (verbose)
637               printf ("zsys_test() at timestamp %" PRIi64 ": "
638                   "Checking if file is NOT stable (is younger than 1 sec)\n",
639                   zclock_time() );
640           assert (!s_zsys_file_stable (dirpath, verbose));
641           if (verbose)
642               printf ("zsys_test() at timestamp %" PRIi64 ": "
643                   "Passed the test, file is not stable - as expected\n",
644                   zclock_time() );
645           rc = zsys_dir_delete ("%s/%s", SELFTEST_DIR_RW, relsubdir);
646           assert (rc == 0);
647           rc = zsys_dir_delete ("%s/%s", SELFTEST_DIR_RW, testbasedir);
648           assert (rc == 0);
649           zsys_file_mode_default ();
650
651           #if (defined (PATH_MAX))
652           char cwd[PATH_MAX];
653           #else
654           # if (defined (_MAX_PATH))
655           char cwd[_MAX_PATH];
656           # else
657           char cwd[1024];
658           # endif
659           #endif
660           memset (cwd, 0, sizeof(cwd));
661           #if (defined (WIN32))
662           if (_getcwd(cwd, sizeof(cwd)) != NULL) {
663           #else
664           if (getcwd(cwd, sizeof(cwd)) != NULL) {
665           #endif
666               if (verbose)
667                   printf ("zsys_test() at timestamp %" PRIi64 ": "
668                       "current working directory is %s\n",
669                       zclock_time(), cwd);
670               assert (zsys_dir_change (SELFTEST_DIR_RW) == 0);
671               assert (zsys_dir_change (cwd) == 0);
672           }
673           else {
674               zsys_warning ("zsys_test() : got getcwd() error... "
675                   "testing zsys_dir_change() anyway, but it can confuse "
676                   "subsequent tests in this process");
677               assert (zsys_dir_change (SELFTEST_DIR_RW) == 0);
678           }
679
680           zstr_free (&basedirpath);
681           zstr_free (&dirpath);
682           zstr_free (&relsubdir);
683
684           // Other subtests
685           int major, minor, patch;
686           zsys_version (&major, &minor, &patch);
687           assert (major == CZMQ_VERSION_MAJOR);
688           assert (minor == CZMQ_VERSION_MINOR);
689           assert (patch == CZMQ_VERSION_PATCH);
690
691           string = zsys_sprintf ("%s %02x", "Hello", 16);
692           assert (streq (string, "Hello 10"));
693           freen (string);
694
695           char *str64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890,.";
696           int num10 = 1234567890;
697           string = zsys_sprintf ("%s%s%s%s%d", str64, str64, str64, str64, num10);
698           assert (strlen (string) == (4 * 64 + 10));
699           freen (string);
700
701           //  Test logging system
702           zsys_set_logident ("czmq_selftest");
703           zsys_set_logsender ("inproc://logging");
704           void *logger = zsys_socket (ZMQ_SUB, NULL, 0);
705           assert (logger);
706           rc = zmq_connect (logger, "inproc://logging");
707           assert (rc == 0);
708           rc = zmq_setsockopt (logger, ZMQ_SUBSCRIBE, "", 0);
709           assert (rc == 0);
710
711           if (verbose) {
712               zsys_error ("This is an %s message", "error");
713               zsys_warning ("This is a %s message", "warning");
714               zsys_notice ("This is a %s message", "notice");
715               zsys_info ("This is a %s message", "info");
716               zsys_debug ("This is a %s message", "debug");
717               zsys_set_logident ("hello, world");
718               zsys_info ("This is a %s message", "info");
719               zsys_debug ("This is a %s message", "debug");
720
721               //  Check that logsender functionality is working
722               char *received = zstr_recv (logger);
723               assert (received);
724               zstr_free (&received);
725           }
726           zsys_close (logger, NULL, 0);
727
728           {
729               // zhash based printf
730               zhash_t *args = zhash_new ();
731               zhash_insert (args, "key", "value");
732               zhash_insert (args, "ham", "spam");
733
734               char *str = zsys_zprintf ("plain string", args);
735               assert (streq (str, "plain string"));
736               zstr_free (&str);
737
738               str = zsys_zprintf ("%%a%%", args);
739               assert (streq (str, "%a%"));
740               zstr_free (&str);
741
742               str = zsys_zprintf ("VALUE=%(key)s123", args);
743               assert (streq (str, "VALUE=value123"));
744               zstr_free (&str);
745
746               str = zsys_zprintf ("VALUE=%(key)s123, %(ham)s, %(ham)s, %%(nospam)s!!!", args);
747               assert (streq (str, "VALUE=value123, spam, spam, %(nospam)s!!!"));
748               zstr_free (&str);
749
750               str = zsys_zprintf ("VALUE=%(nokey)s123, %(ham)s, %(ham)s, %%(nospam)s!!!", args);
751               assert (!str);
752
753               str = zsys_zprintf_error ("VALUE=%(nokey)s123, %(ham)s, %(ham)s, %%(nospam)s!!!", args);
754               assert (streq (str, "Key 'nokey' not found in hash"));
755               zstr_free (&str);
756
757               str = zsys_zprintf ("VALUE=%(key)s/%%S", args);
758               assert (streq (str, "VALUE=value/%S"));
759               zstr_free (&str);
760
761               zhash_destroy (&args);
762
763               //ZPL based printf
764               zconfig_t *root = zconfig_new ("root", NULL);
765               zconfig_put (root, "zsp", "");
766               zconfig_put (root, "zsp/return_code", "0");
767
768               str = zsys_zplprintf ("return_code=%(zsp/return_code)s", root);
769               assert (streq (str, "return_code=0"));
770               zstr_free (&str);
771
772               zconfig_destroy (&root);
773           }
774
775

AUTHORS

777       The czmq manual was written by the authors in the AUTHORS file.
778

RESOURCES

780       Main web site:
781
782       Report bugs to the email <zeromq-dev@lists.zeromq.org[1]>
783
785       Copyright (c) the Contributors as noted in the AUTHORS file. This file
786       is part of CZMQ, the high-level C binding for 0MQ:
787       http://czmq.zeromq.org. This Source Code Form is subject to the terms
788       of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
789       distributed with this file, You can obtain one at
790       http://mozilla.org/MPL/2.0/. LICENSE included with the czmq
791       distribution.
792

NOTES

794        1. zeromq-dev@lists.zeromq.org
795           mailto:zeromq-dev@lists.zeromq.org
796
797
798
799CZMQ 4.2.0                        01/28/2020                           ZSYS(3)
Impressum