-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
main,syslog: optionally connect to syslog
- Loading branch information
Showing
3 changed files
with
48 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
#include "util.h" | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/socket.h> | ||
#include <sys/un.h> | ||
#include <unistd.h> | ||
|
||
static const struct { | ||
short sun_family; | ||
char sun_path[9]; | ||
} log_addr = { | ||
AF_UNIX, | ||
"/dev/log" | ||
}; | ||
|
||
int syslog_connect_socket() { | ||
int log_fd = socket(AF_UNIX, SOCK_DGRAM, 0); | ||
if (log_fd < 0) | ||
return -1; | ||
|
||
int syslog_connect_socket(const char* syslog_path) { | ||
struct sockaddr_un addr; | ||
socklen_t addr_size; | ||
int logfd = socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0); | ||
memset(&addr, 0, sizeof(struct sockaddr_un)); | ||
/* Clear structure */ | ||
addr.sun_family = AF_UNIX; | ||
strncpy(addr.sun_path, syslog_path, | ||
sizeof(addr.sun_path) - 1); | ||
int r = connect(log_fd, (void *)&log_addr, sizeof log_addr); | ||
if (r < 0) { | ||
close(log_fd); | ||
perror("syslog"); | ||
} | ||
|
||
addr_size = sizeof(struct sockaddr_un); | ||
int ret = connect(logfd, (struct sockaddr *) &addr, | ||
addr_size); | ||
if (ret == -1) | ||
handle_error("connect"); | ||
return logfd; | ||
return r == 0 ? log_fd : -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#ifndef syslog_h_INCLUDED | ||
#define syslog_h_INCLUDED | ||
|
||
int syslog_connect_socket(const char* syslog_path); | ||
int syslog_connect_socket(); | ||
|
||
#endif // syslog_h_INCLUDED | ||
|