-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.c
1883 lines (1333 loc) · 42.6 KB
/
msg.c
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* libslack - https://libslack.org
*
* Copyright (C) 1999-2004, 2010, 2020-2023 raf <raf@raf.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*
* 20230824 raf <raf@raf.org>
*/
/*
=head1 NAME
I<libslack(msg)> - message module
=head1 SYNOPSIS
#include <slack/std.h>
#include <slack/msg.h>
typedef struct Msg Msg;
typedef void msg_out_t(void *data, const void *mesg, size_t mesglen);
typedef int msg_filter_t(void **mesgp, const void *mesg, size_t mesglen);
typedef void msg_release_t(void *data);
Msg *msg_create(int type, msg_out_t *out, void *data, msg_release_t *destroy);
Msg *msg_create_with_locker(Locker *locker, int type, msg_out_t *out, void *data, msg_release_t *destroy);
int msg_rdlock(Msg *mesg);
int msg_wrlock(Msg *mesg);
int msg_unlock(Msg *mesg);
void msg_release(Msg *mesg);
void *msg_destroy(Msg **mesg);
void msg_out(Msg *dst, const char *format, ...);
void msg_out_unlocked(Msg *dst, const char *format, ...);
void vmsg_out(Msg *dst, const char *format, va_list args);
void vmsg_out_unlocked(Msg *dst, const char *format, va_list args);
Msg *msg_create_fd(int fd);
Msg *msg_create_fd_with_locker(Locker *locker, int fd);
Msg *msg_create_stderr(void);
Msg *msg_create_stderr_with_locker(Locker *locker);
Msg *msg_create_stdout(void);
Msg *msg_create_stdout_with_locker(Locker *locker);
Msg *msg_create_file(const char *path);
Msg *msg_create_file_with_locker(Locker *locker, const char *path);
Msg *msg_create_syslog(const char *ident, int option, int facility, int priority);
Msg *msg_create_syslog_with_locker(Locker *locker, const char *ident, int option, int facility, int priority);
Msg *msg_syslog_set_facility(Msg *mesg, int facility);
Msg *msg_syslog_set_facility_unlocked(Msg *mesg, int facility);
Msg *msg_syslog_set_priority(Msg *mesg, int priority);
Msg *msg_syslog_set_priority_unlocked(Msg *mesg, int priority);
Msg *msg_create_plex(Msg *msg1, Msg *msg2);
Msg *msg_create_plex_with_locker(Locker *locker, Msg *msg1, Msg *msg2);
int msg_add_plex(Msg *mesg, Msg *item);
int msg_add_plex_unlocked(Msg *mesg, Msg *item);
Msg *msg_create_filter(msg_filter_t *filter, Msg *mesg);
Msg *msg_create_filter_with_locker(Locker *locker, msg_filter_t *filter, Msg *mesg);
const char *msg_set_timestamp_format(const char *format);
int msg_set_timestamp_format_locker(Locker *locker);
int syslog_lookup_facility(const char *facility);
int syslog_lookup_priority(const char *priority);
const char *syslog_facility_str(int spec);
const char *syslog_priority_str(int spec);
int syslog_parse(const char *spec, int *facility, int *priority);
=head1 DESCRIPTION
This module provides general messaging functions. Message channels can be
created that send messages to a file descriptor, a file, I<syslog> or a
client defined message handler or that multiplexes messages to any
combination of the above. Messages sent to files are timestamped using (by
default) the I<strftime(3)> format: C<"%Y%m%d %H:%M:%S">.
It also provides functions for parsing I<syslog> targets, converting between
I<syslog> facility names and codes, and converting between I<syslog>
priority names and codes.
=over 4
=cut
*/
#ifndef _BSD_SOURCE
#define _BSD_SOURCE /* For snprintf() on OpenBSD-4.7 */
#endif
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE /* New name for _BSD_SOURCE */
#endif
#include "config.h"
#include "std.h"
#include <syslog.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
#include "msg.h"
#include "mem.h"
#include "err.h"
#include "str.h"
#ifndef HAVE_SNPRINTF
#include "snprintf.h"
#endif
typedef int MsgFDData;
typedef struct MsgFileData MsgFileData;
typedef struct MsgSyslogData MsgSyslogData;
typedef struct MsgFilterData MsgFilterData;
typedef struct MsgPlexData MsgPlexData;
#define MSG_FD 1
#define MSG_FILE 2
#define MSG_SYSLOG 3
#define MSG_PLEX 4
#define MSG_FILTER 5
struct Msg
{
int type; /* subtype */
msg_out_t *out; /* message handling function */
void *data; /* subtype specific data */
msg_release_t *destroy; /* destructor function for data */
Locker *locker; /* locking strategy for this structure */
};
struct MsgFileData
{
int fd; /* file descriptor (-1 if open failed) */
};
struct MsgSyslogData
{
int facility; /* syslog(3) priority */
int priority; /* syslog(3) priority */
};
struct MsgPlexData
{
size_t size; /* elements allocated */
size_t length; /* length of Msg list */
Msg **list; /* list of Msg objects */
};
struct MsgFilterData
{
msg_filter_t *filter; /* filter function */
Msg *mesg; /* destination Msg */
};
typedef struct syslog_map_t syslog_map_t;
struct syslog_map_t
{
char *name;
int val;
};
/*
** The following masks might be wrong on some systems.
*/
#ifndef LOG_PRIMASK
#define LOG_PRIMASK 0x0007
#endif
#ifndef LOG_FACMASK
#define LOG_FACMASK 0x03f8
#endif
static const syslog_map_t syslog_facility_map[] =
{
{ "kern", LOG_KERN },
{ "user", LOG_USER },
{ "mail", LOG_MAIL },
{ "daemon", LOG_DAEMON },
{ "auth", LOG_AUTH },
{ "syslog", LOG_SYSLOG },
{ "lpr", LOG_LPR },
{ "news", LOG_NEWS },
{ "uucp", LOG_UUCP },
{ "cron", LOG_CRON },
{ "local0", LOG_LOCAL0 },
{ "local1", LOG_LOCAL1 },
{ "local2", LOG_LOCAL2 },
{ "local3", LOG_LOCAL3 },
{ "local4", LOG_LOCAL4 },
{ "local5", LOG_LOCAL5 },
{ "local6", LOG_LOCAL6 },
{ "local7", LOG_LOCAL7 },
{ NULL, -1 }
};
static const syslog_map_t syslog_priority_map[] =
{
{ "emerg", LOG_EMERG },
{ "alert", LOG_ALERT },
{ "crit", LOG_CRIT },
{ "err", LOG_ERR },
{ "warning", LOG_WARNING },
#ifdef LOG_NOTICE
{ "notice", LOG_NOTICE },
#endif
{ "info", LOG_INFO },
{ "debug", LOG_DEBUG },
{ NULL, -1 }
};
#ifndef TEST
static const char *timestamp_format = "%Y%m%d %H:%M:%S ";
static Locker *timestamp_format_locker = NULL;
/*
=item C<Msg *msg_create(int type, msg_out_t *out, void *data, msg_release_t *destroy)>
Creates a I<Msg> object initialised with C<type>, C<out>, C<data> and
C<destroy>. Client-defined message handlers must specify a C<type> greater
than C<5>. It is the caller's responsibility to deallocate the new I<Msg>
with I<msg_release(3)> or I<msg_destroy>. It is strongly recommended to use
I<msg_destroy(3)>, because it also sets the pointer variable to C<null>. On
success, returns the new I<Msg> object. On error, returns C<null>.
=cut
*/
Msg *msg_create(int type, msg_out_t *out, void *data, msg_release_t *destroy)
{
return msg_create_with_locker(NULL, type, out, data, destroy);
}
/*
=item C<Msg *msg_create_with_locker(Locker *locker, int type, msg_out_t *out, void *data, msg_release_t *destroy)>
Equivalent to I<msg_create(3)> except that multiple threads accessing the
new I<Msg> will be synchronised by C<locker>.
=cut
*/
Msg *msg_create_with_locker(Locker *locker, int type, msg_out_t *out, void *data, msg_release_t *destroy)
{
Msg *mesg;
if (!(mesg = mem_new(Msg)))
return NULL;
mesg->type = type;
mesg->out = out;
mesg->data = data;
mesg->destroy = destroy;
mesg->locker = locker;
return mesg;
}
/*
=item C<int msg_rdlock(Msg *mesg)>
Claims a read lock on C<mesg> (if C<mesg> was created with a I<Locker>).
This is needed when multiple read-only I<msg(3)> module functions
need to be called atomically. It is the caller's responsibility to call
I<msg_unlock(3)> after the atomic operation. The only functions that may be
called on C<mesg> between calls to I<msg_rdlock(3)> and I<msg_unlock(3)> are
any read-only I<msg(3)> module functions whose name ends with
C<_unlocked>. On success, returns C<0>. On error, returns an error code.
=cut
*/
#define msg_rdlock(mesg) ((mesg) ? locker_rdlock((mesg)->locker) : EINVAL)
#define msg_wrlock(mesg) ((mesg) ? locker_wrlock((mesg)->locker) : EINVAL)
#define msg_unlock(mesg) ((mesg) ? locker_unlock((mesg)->locker) : EINVAL)
int (msg_rdlock)(Msg *mesg)
{
return msg_rdlock(mesg);
}
/*
=item C<int msg_wrlock(Msg *mesg)>
Claims a write lock on C<mesg>.
Claims a write lock on C<mesg> (if C<mesg> was created with a I<Locker>).
This is needed when multiple read/write I<msg(3)> module functions
need to be called atomically. It is the caller's responsibility to call
I<msg_unlock(3)> after the atomic operation. The only functions that may be
called on C<mesg> between calls to I<msg_rdlock(3)> and I<msg_unlock(3)> are
any I<msg(3)> module functions whose name ends with C<_unlocked>. On
success, returns C<0>. On error, returns an error code.
=cut
*/
int (msg_wrlock)(Msg *mesg)
{
return msg_wrlock(mesg);
}
/*
=item C<int msg_unlock(Msg *mesg)>
Unlocks a read or write lock on C<mesg> obtained with I<msg_rdlock(3)> or
I<msg_wrlock(3)> (if C<mesg> was created with a I<Locker>). On success,
returns C<0>. On error, returns an error code.
=cut
*/
int (msg_unlock)(Msg *mesg)
{
return msg_unlock(mesg);
}
/*
=item C<void msg_release(Msg *mesg)>
Releases (deallocates) C<mesg> and its internal data.
=cut
*/
void msg_release(Msg *mesg)
{
if (!mesg)
return;
if (mesg->destroy)
mesg->destroy(mesg->data);
mem_release(mesg);
}
/*
=item C<void *msg_destroy(Msg **mesg)>
Destroys (deallocates and sets to C<null>) C<*mesg>. Returns C<null>.
=cut
*/
void *msg_destroy(Msg **mesg)
{
if (mesg && *mesg)
{
msg_release(*mesg);
*mesg = NULL;
}
return NULL;
}
/*
=item C<void msg_out(Msg *dst, const char *format, ...)>
Sends a message to C<dst>. C<format> is a I<printf(3)>-like format string.
Any remaining arguments are processed as in I<printf(3)>.
B<Warning: Do not under any circumstances ever pass a non-literal string as
the format argument unless you know exactly how many conversions will take
place. Being careless with this is a very good way to build potential
security vulnerabilities into your programs. The same is true for all
functions that take a printf()-like format string as an argument.>
msg_out(dst, buf); // EVIL
msg_out(dst, "%s", buf); // GOOD
=cut
*/
void msg_out(Msg *dst, const char *format, ...)
{
va_list args;
va_start(args, format);
vmsg_out(dst, format, args);
va_end(args);
}
/*
=item C<void msg_out_unlocked(Msg *dst, const char *format, ...)>
Equivalent to I<msg_out(3)> except that C<dst> is not read-locked.
=cut
*/
void msg_out_unlocked(Msg *dst, const char *format, ...)
{
va_list args;
va_start(args, format);
vmsg_out_unlocked(dst, format, args);
va_end(args);
}
/*
=item C<void vmsg_out(Msg *dst, const char *format, va_list args)>
Sends a message to C<dst>. C<format> is a I<printf(3)>-like format string.
C<args> is processed as in I<vprintf(3)>.
=cut
*/
void vmsg_out(Msg *dst, const char *format, va_list args)
{
int err;
if (!dst)
return;
if ((err = msg_rdlock(dst)))
{
set_errno(err);
return;
}
vmsg_out_unlocked(dst, format, args);
if ((err = msg_unlock(dst)))
set_errno(err);
}
/*
=item C<void vmsg_out_unlocked(Msg *dst, const char *format, va_list args)>
Equivalent to I<vmsg_out(3)> except that C<dst> is not read-locked.
=cut
*/
void vmsg_out_unlocked(Msg *dst, const char *format, va_list args)
{
if (!dst)
return;
if (dst->out)
{
char mesg[MSG_SIZE];
vsnprintf(mesg, MSG_SIZE, format, args);
dst->out(dst->data, mesg, strlen(mesg));
}
}
/*
C<MsgFDData *msg_fddata_create(int fd)>
Creates and initialises the internal data needed by a I<Msg> object that
sends messages to file descriptor C<fd>. On success, returns the data. On
error, returns C<null>.
*/
static MsgFDData *msg_fddata_create(int fd)
{
MsgFDData *data;
if (!(data = mem_new(MsgFDData)))
return NULL;
*data = fd;
return data;
}
/*
C<void msg_fddata_release(MsgFDData *data)>
Releases (deallocates) the internal data needed by a I<Msg> object that
sends messages to a file descriptor. The file descriptor is not closed.
*/
static void msg_fddata_release(MsgFDData *data)
{
mem_release(data);
}
/*
C<void msg_out_fd(void *data, const void *mesg, size_t mesglen)>
Sends a message to a file descriptor. C<data> is a pointer to the file
descriptor. C<mesg> is the message. C<mesglen> is its length.
*/
static void msg_out_fd(void *data, const void *mesg, size_t mesglen)
{
if (data && mesg)
if (write(*(MsgFDData *)data, mesg, mesglen) == -1)
/* Avoid gcc warning */;
}
/*
=item C<Msg *msg_create_fd(int fd)>
Creates a I<Msg> object that sends messages to file descriptor C<fd>. It is
the caller's responsibility to deallocate the new I<Msg> with
I<msg_release(3)> or I<msg_destroy(3)>. On success, returns the new I<Msg>
object. On error, returns C<null>.
=cut
*/
Msg *msg_create_fd(int fd)
{
return msg_create_fd_with_locker(NULL, fd);
}
/*
=item C<Msg *msg_create_fd_with_locker(Locker *locker, int fd)>
Equivalent to I<msg_create_fd(3)> except that multiple threads accessing the
new I<Msg> will be synchronised by C<locker>.
=cut
*/
Msg *msg_create_fd_with_locker(Locker *locker, int fd)
{
MsgFDData *data;
Msg *mesg;
if (!(data = msg_fddata_create(fd)))
return NULL;
if (!(mesg = msg_create_with_locker(locker, MSG_FD, msg_out_fd, data, (msg_release_t *)msg_fddata_release)))
{
msg_fddata_release(data);
return NULL;
}
return mesg;
}
/*
=item C<Msg *msg_create_stderr(void)>
Creates a I<Msg> object that sends messages to standard error. It is the
caller's responsibility to deallocate the new I<Msg> with I<msg_release(3)>
or I<msg_destroy(3)>. It is strongly recommended to use I<msg_destroy(3)>,
because it also sets the pointer variable to C<null>. On success, returns
the new I<Msg> object. On error, returns C<null>.
=cut
*/
Msg *msg_create_stderr(void)
{
return msg_create_fd_with_locker(NULL, STDERR_FILENO);
}
/*
=item C<Msg *msg_create_stderr_with_locker(Locker *locker)>
Equivalent to I<msg_create_stderr(3)> except that multiple threads accessing
the new I<Msg> will be synchronised by C<locker>.
=cut
*/
Msg *msg_create_stderr_with_locker(Locker *locker)
{
return msg_create_fd_with_locker(locker, STDERR_FILENO);
}
/*
=item C<Msg *msg_create_stdout(void)>
Creates a I<Msg> object that sends messages to standard output. It is the
caller's responsibility to deallocate the new I<Msg> with I<msg_release(3)>
or I<msg_destroy(3)>. It is strongly recommended to use I<msg_destroy(3)>,
because it also sets the pointer variable to C<null>. On success, returns
the new I<Msg> object. On error, returns C<null>.
=cut
*/
Msg *msg_create_stdout(void)
{
return msg_create_fd_with_locker(NULL, STDOUT_FILENO);
}
/*
=item C<Msg *msg_create_stdout_with_locker(Locker *locker)>
Equivalent to I<msg_create_stdout(3)> except that multiple threads accessing
the new I<Msg> will be synchronised by C<locker>.
=cut
*/
Msg *msg_create_stdout_with_locker(Locker *locker)
{
return msg_create_fd_with_locker(locker, STDOUT_FILENO);
}
/*
C<int msg_filedata_init(MsgFileData *data, const char *path)>
Initialises the internal data needed by a I<Msg> object that sends messages
to the file specified by C<path>. This data consists of a copy of C<path>
and an open file descriptor to the file. The file descriptor is opened with
the C<O_WRONLY>, C<O_CREAT>, and C<O_APPEND> flags. On success, returns
C<0>. On error, returns C<-1> with C<errno> set appropriately.
*/
static int msg_filedata_init(MsgFileData *data, const char *path)
{
mode_t mode;
if (!data || !path)
return set_errno(EINVAL);
mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
if ((data->fd = open(path, O_WRONLY | O_CREAT | O_APPEND, mode)) == -1)
return -1;
return 0;
}
/*
C<MsgFileData *msg_filedata_create(const char *path)>
Creates the internal data needed by a I<Msg> object that sends messages to
the file specified by C<path>. On success, returns the data. On error,
returns C<null> with C<errno> set appropriately.
*/
static MsgFileData *msg_filedata_create(const char *path)
{
MsgFileData *data;
if (!(data = mem_new(MsgFileData)))
return NULL;
if (msg_filedata_init(data, path) == -1)
{
mem_release(data);
return NULL;
}
return data;
}
/*
C<void msg_filedata_release(MsgFileData *data)>
Releases (deallocates) the internal data needed by a I<Msg> object that
sends messages to a file. The file descriptor is closed first.
*/
static void msg_filedata_release(MsgFileData *data)
{
if (!data)
return;
if (data->fd != -1)
close(data->fd);
mem_release(data);
}
/*
C<void msg_out_file(void *data, const void *mesg, size_t mesglen)>
Sends a message to a file. C<data> contains the file descriptor. C<mesg> is
the message. C<mesglen> is its length. On error, sets C<errno>
appropriately.
*/
static void msg_out_file(void *data, const void *mesg, size_t mesglen)
{
MsgFileData *dst = data;
char buf[MSG_SIZE];
size_t buflen;
int err;
time_t t = time(NULL);
if ((err = locker_rdlock(timestamp_format_locker)))
{
set_errno(err);
return;
}
strftime(buf, MSG_SIZE, timestamp_format, localtime(&t));
if ((err = locker_unlock(timestamp_format_locker)))
{
set_errno(err);
return;
}
buflen = strlen(buf);
if (buflen + mesglen >= MSG_SIZE)
mesglen -= MSG_SIZE - buflen;
memmove(buf + buflen, mesg, mesglen);
if (mesg && dst && dst->fd != -1)
if (write(dst->fd, buf, buflen + mesglen) == -1)
/* Avoid gcc warning */;
}
/*
=item C<Msg *msg_create_file(const char *path)>
Creates a I<Msg> object that sends messages to the file specified by
C<path>. It is the caller's responsibility to deallocate the new I<Msg> with
I<msg_release(3)> or I<msg_destroy(3)>. It is strongly recommended to use
I<msg_destroy(3)>, because it also sets the pointer variable to C<null>. On
success, returns the new I<Msg> object. On error, returns C<null> with
C<errno> set appropriately.
=cut
*/
Msg *msg_create_file(const char *path)
{
return msg_create_file_with_locker(NULL, path);
}
/*
=item C<Msg *msg_create_file_with_locker(Locker *locker, const char *path)>
Equivalent to I<msg_create_file(3)> except that multiple threads accessing
the new I<Msg> will be synchronised by C<locker>.
=cut
*/
Msg *msg_create_file_with_locker(Locker *locker, const char *path)
{
MsgFileData *data;
Msg *mesg;
if (!(data = msg_filedata_create(path)))
return NULL;
if (!(mesg = msg_create_with_locker(locker, MSG_FILE, msg_out_file, data, (msg_release_t *)msg_filedata_release)))
{
msg_filedata_release(data);
return NULL;
}
return mesg;
}
/*
C<int msg_sysdata_init(MsgSyslogData *data, const char *ident, int option, int facility, int priority)>
Initialises the internal data needed by a I<Msg> object that sends messages
to I<syslog>. I<openlog(3)> is called with C<ident> and C<option>.
C<facility> and C<priority> are stored to be used when sending messages. On
success, returns C<0>. On error, returns C<-1> with C<errno> set
appropriately.
*/
static int msg_sysdata_init(MsgSyslogData *data, const char *ident, int option, int facility, int priority)
{
if (!data || facility == -1)
return set_errno(EINVAL);
data->facility = facility & LOG_FACMASK;
data->priority = priority & LOG_PRIMASK;
openlog(ident, option, 0);
return 0;
}
/*
C<MsgSyslogData *msg_sysdata_create(const char *ident, int option, int facility, int priority)>
Creates the internal data needed by a I<Msg> object that sends messages to
I<syslog>. C<ident>, C<option>, C<facility> and C<priority> are used to
initialise the connection to I<syslog>. On success, returns the data. On
error, returns C<null> with C<errno> set appropriately.
*/
static MsgSyslogData *msg_sysdata_create(const char *ident, int option, int facility, int priority)
{
MsgSyslogData *data;
if (!(data = mem_new(MsgSyslogData)))
return NULL;
if (msg_sysdata_init(data, ident, option, facility, priority) == -1)
{
mem_release(data);
return NULL;
}
return data;
}
/*
C<void msg_sysdata_release(MsgSyslogData *data)>
Releases (deallocates) the internal data needed by a I<Msg> object that
sends messages to I<syslog>. Calls I<closelog(3)>.
*/
static void msg_sysdata_release(MsgSyslogData *data)
{
if (!data)
return;
mem_release(data);
closelog();
}
/*
C<void msg_out_syslog(void *data, const void *mesg, size_t mesglen)>
Sends a message to I<syslog>. C<data> is a pointer to a C<MsgSyslogData>
object that contains the facility and priority to use. C<mesg> is the
message. C<mesglen> is its length.
*/
static void msg_out_syslog(void *data, const void *mesg, size_t mesglen)
{
MsgSyslogData *dst = data;
if (mesg && dst && dst->facility != -1)
syslog(dst->facility | dst->priority, "%*.*s", (int)mesglen, (int)mesglen, (char *)mesg);
}
/*
=item C<Msg *msg_create_syslog(const char *ident, int option, int facility, int priority)>
Creates a I<Msg> object that sends messages to I<syslog> initialised with
C<ident>, C<option>, C<facility> and C<priority>. It is the caller's
responsibility to deallocate the new I<Msg> with I<msg_release(3)> or
I<msg_destroy(3)>. It is strongly recommended to use I<msg_destroy(3)>,
because it also sets the pointer variable to C<null>. On success, returns
the new I<Msg> object. On error, returns C<null> with C<errno> set
appropriately.
=cut
*/
Msg *msg_create_syslog(const char *ident, int option, int facility, int priority)
{
return msg_create_syslog_with_locker(NULL, ident, option, facility, priority);
}
/*
=item C<Msg *msg_create_syslog_with_locker(Locker *locker, const char *ident, int option, int facility, int priority)>
Equivalent to I<msg_create_syslog(3)> except that multiple threads accessing
the new I<Msg> will be synchronised by C<locker>.
=cut
*/
Msg *msg_create_syslog_with_locker(Locker *locker, const char *ident, int option, int facility, int priority)
{
MsgSyslogData *data;
Msg *mesg;
if (!(data = msg_sysdata_create(ident, option, facility, priority)))
return NULL;
if (!(mesg = msg_create_with_locker(locker, MSG_SYSLOG, msg_out_syslog, data, (msg_release_t *)msg_sysdata_release)))
{
msg_sysdata_release(data);
return NULL;
}
return mesg;
}
/*
=item C<Msg *msg_syslog_set_facility(Msg *mesg, int facility)>
Sets the facility field in C<mesg>'s data to C<facility>. On success,
returns C<mesg>. On error, returns C<null> with C<errno> set appropriately.
=cut
*/
Msg *msg_syslog_set_facility(Msg *mesg, int facility)
{
Msg *ret;
int err;
if (!mesg)
return set_errnull(EINVAL);
if ((err = msg_wrlock(mesg)))
return set_errnull(err);
ret = msg_syslog_set_facility_unlocked(mesg, facility);
if ((err = msg_unlock(mesg)))
return set_errnull(err);
return ret;
}
/*
=item C<Msg *msg_syslog_set_facility_unlocked(Msg *mesg, int facility)>
Equivalent to I<msg_syslog_set_facility(3)> except that C<mesg> is not
write-locked.
=cut
*/
Msg *msg_syslog_set_facility_unlocked(Msg *mesg, int facility)
{
MsgSyslogData *data;
if (!mesg || mesg->type != MSG_SYSLOG)
return set_errnull(EINVAL);
data = (MsgSyslogData *)mesg->data;
data->facility = facility;