PERFORCE change 122196 for review
Alexey Mikhailov
karma at FreeBSD.org
Sat Jun 23 09:48:05 UTC 2007
http://perforce.freebsd.org/chv.cgi?CH=122196
Change 122196 by karma at karma_ez on 2007/06/23 09:47:03
Initial hacking around distribute logging user-space library
Affected files ...
.. //depot/projects/soc2007/karma_audit/dlog/config.h#2 edit
.. //depot/projects/soc2007/karma_audit/dlog/lib/Makefile#2 edit
.. //depot/projects/soc2007/karma_audit/dlog/lib/libdlogd.c#2 edit
.. //depot/projects/soc2007/karma_audit/dlog/lib/libdlogd.h#2 edit
Differences ...
==== //depot/projects/soc2007/karma_audit/dlog/config.h#2 (text+ko) ====
==== //depot/projects/soc2007/karma_audit/dlog/lib/Makefile#2 (text+ko) ====
==== //depot/projects/soc2007/karma_audit/dlog/lib/libdlogd.c#2 (text+ko) ====
@@ -1,0 +1,160 @@
+#include "../config.h"
+#include <stdio.h>
+#include <strings.h>
+#include <string.h>
+#include <stddef.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/poll.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/syslimits.h>
+#include <sys/un.h>
+
+#define BUF_SIZE PATH_MAX+KEYWORD_MAX+2
+
+static int timeout = 1000;
+
+static int dlogd_send (int fd, const char * buf, size_t len);
+static int dlogd_connect ();
+
+/* Submit log file
+
+ On success: 0 returned
+*/
+
+int
+dlogd_submit (const char * pathname, const char * keyword)
+{
+ int fd, rc;
+ size_t len;
+ char buf[BUF_SIZE];
+
+ if (strlen(pathname) > PATH_MAX)
+ {
+ return (-1);
+ }
+ else if (strlen(keyword) > KEYWORD_MAX)
+ {
+ return (-1);
+ }
+
+ if ((fd = dlogd_connect()) < 0)
+ {
+ return fd;
+ }
+
+ snprintf(buf, BUF_SIZE, "%s\n%s\n", keyword, pathname);
+ len = strlen(buf);
+
+ rc = dlogd_send(fd, buf, len);
+
+ if (rc < 0)
+ return rc;
+
+ /* TODO: write dlogd_receive and wait for server's reply */
+
+ return 0;
+}
+
+/* Send buf through fd descriptor
+
+ On success: 0 returned
+
+ On failure: -1 returned
+*/
+
+static int
+dlogd_send (int fd, const char * buf, size_t len)
+{
+ struct pollfd fds;
+ int rc, bytes;
+
+ fds.fd = fd;
+ fds.events = POLLOUT;
+
+ while (len)
+ {
+ fds.revents = 0;
+ rc = poll(&fds, 1, timeout * 1000);
+ if (rc == 0)
+ return (-1); /* timeout */
+ else if (rc < 0)
+ {
+ if (errno == EINTR || errno == EAGAIN)
+ continue;
+ else
+ return (-1);
+ }
+
+ if (!fds.revents)
+ return (-1);
+
+ bytes = write(fd,buf,len);
+
+ if (bytes < 0)
+ {
+ if (errno == EINTR)
+ continue;
+ else
+ return (-1);
+ }
+
+ len -= bytes;
+ buf += bytes;
+ }
+
+ return 0;
+}
+
+/* Connect to local daemon through PF_LOCAL socket.
+
+ On success: descriptor returned
+
+ On failure: -1 -- can't create socket
+ -2 -- can't bind
+ -3 -- can't chmod
+ -4 -- can't connect
+*/
+
+static int
+dlogd_connect ()
+{
+ int fd, len;
+ struct sockaddr_un un;
+
+ if ((fd = socket(AF_UNIX, SOCK_STREAM,0)) < 0)
+ return (-1); /* can't create socket */
+
+ bzero(&un, sizeof(un));
+ un.sun_family = AF_UNIX;
+ sprintf(un.sun_path, "%sdlog.%05d", DL_SOCKET_DIR, getpid());
+ len = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path);
+ unlink(un.sun_path);
+
+ if (bind(fd, (struct sockaddr *) &un, len) < 0)
+ {
+ close (fd);
+ return (-2); /* can't bind */
+ }
+
+ if (chmod(un.sun_path, S_IRWXU) < 0)
+ {
+ close (fd);
+ return (-3); /* can't chmod */
+ }
+
+ bzero(&un, sizeof(un));
+ un.sun_family = AF_UNIX;
+ strcpy(un.sun_path, DL_SOCKET);
+ len = offsetof(struct sockaddr_un, sun_path) + strlen(DL_SOCKET);
+ if (connect(fd, (struct sockaddr *) &un, len) < 0)
+ {
+ close(fd);
+ return (-4); /* can't connect */
+ }
+
+ return fd;
+
+}
==== //depot/projects/soc2007/karma_audit/dlog/lib/libdlogd.h#2 (text+ko) ====
@@ -1,0 +1,6 @@
+#ifndef DLOG_LIB_H
+#define DLOG_LIB_H
+
+int dlogd_submit(const char * pathname, const char * keyword);
+
+#endif
More information about the p4-projects
mailing list