2 * Copyright 2009, Sine Nomine Associates and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
15 /* only build on platforms that have SysV IPC support; i.e., when we
22 #include "audit-api.h"
24 /* message queue size will be increased to this value
25 if not already bigger */
26 #define MSGMNB (2*1024*1024)
28 /* msgsnd requires a structure with the following layout. See manpage for
29 * msgsnd() for the details */
32 char mtext[OSI_AUDIT_MAXMSG];
35 struct mqaudit_stats {
41 struct sysvmq_context {
43 struct my_msgbuf msgbuffer;
44 struct mqaudit_stats myauditstats;
48 send_msg(void *rock, const char *message, int msglen, int truncated)
50 struct sysvmq_context *ctx = rock;
52 if (msglen >= OSI_AUDIT_MAXMSG) {
54 msglen = OSI_AUDIT_MAXMSG - 1;
57 /* Copy the data into the msgsnd structure */
58 memcpy(&ctx->msgbuffer.mtext, message, msglen+1);
59 ctx->msgbuffer.mtext[msglen] = '\0';
61 /* Include the trailing zero in the msgsnd */
62 if (msgsnd(ctx->mqid, &ctx->msgbuffer, msglen+1, IPC_NOWAIT) == -1) {
63 ctx->myauditstats.lost++;
64 } else if (truncated) {
65 ctx->myauditstats.truncated++;
67 ctx->myauditstats.all++;
71 open_file(void *rock, const char *fileName)
73 struct sysvmq_context *ctx = rock;
75 struct msqid_ds msqdesc;
77 ctx->msgbuffer.mtext[0] = 0;
78 ctx->msgbuffer.mtype = 1;
80 ctx->myauditstats.all = 0;
81 ctx->myauditstats.lost = 0;
82 ctx->myauditstats.truncated = 0;
84 /* try to create file for ftok if it doesn't already exist */
85 tempfd = open(fileName, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
89 ctx->mqid = msgget(ftok(fileName, 1), S_IRUSR | S_IWUSR | IPC_CREAT);
90 if (ctx->mqid == -1) {
91 printf("Warning: auditlog message queue %s cannot be opened.\n", fileName);
95 /* increase message queue size */
96 msgctl(ctx->mqid, IPC_STAT, &msqdesc);
97 if (msqdesc.msg_qbytes < MSGMNB) {
98 msqdesc.msg_qbytes = MSGMNB;
99 msgctl(ctx->mqid, IPC_SET, &msqdesc);
106 print_interface_stats(void *rock, FILE *out)
108 struct sysvmq_context *ctx = rock;
110 fprintf(out, "audit statistics: %ld messages total, %ld truncated, %ld lost\n",
111 ctx->myauditstats.all, ctx->myauditstats.truncated, ctx->myauditstats.lost);
115 create_interface(void)
117 struct sysvmq_context *ctx;
119 ctx = calloc(1, sizeof(*ctx));
121 printf("error allocating memory\n");
128 close_interface(void **rock)
130 struct sysvmq_context *ctx = *rock;
138 const struct osi_audit_ops audit_sysvmq_ops = {
141 &print_interface_stats,
144 NULL, /* set_option */
145 NULL, /* open_interface */
148 #endif /* HAVE_SYS_IPC_H */