transfused: Use Linux coding style (mostly)

Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
This commit is contained in:
Rolf Neugebauer 2016-09-02 16:57:52 +01:00
parent 1a289d04e5
commit 3c30271f3c
6 changed files with 1294 additions and 1158 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,37 @@
#ifndef _TRANSFUSED_H_
#define _TRANSFUSED_H_
#include <pthread.h> #include <pthread.h>
#include <sys/socket.h> #include <sys/socket.h>
#define IN_BUFSZ ((1 << 20) + 16)
#define OUT_BUFSZ ((1 << 20) + 64)
#define EVENT_BUFSZ 4096
#define CTL_BUFSZ 65536
#define DEFAULT_FUSERMOUNT "/bin/fusermount"
#define DEFAULT_SOCKET "v:_:1525"
#define DEFAULT_SERVER "v:2:1524"
#define PING 128
#define RMDIR_SYSCALL 0
#define UNLINK_SYSCALL 1
#define MKDIR_SYSCALL 2
#define SYMLINK_SYSCALL 3
#define TRUNCATE_SYSCALL 4
#define CHMOD_SYSCALL 5
#define MKNOD_REG_SYSCALL 6
/* these could be turned into an enum probably but...C standard nausea */
#define MOUNT_SUITABILITY_REQUEST 1
#define EXPORT_SUITABILITY_REQUEST 2
#define TRANSFUSE_LOG_ERROR 1
#define TRANSFUSE_LOG_NOTICE 2
#define PONG_REPLY 3
#define MOUNT_SUITABILITY_REPLY 4
#define TRANSFUSE_NOTIFY_CHANNEL 5
typedef struct { typedef struct {
char *server; char *server;
char *socket; char *socket;
@ -25,37 +56,8 @@ typedef struct {
pthread_attr_t detached; pthread_attr_t detached;
void *must_malloc(char *const descr, size_t size); void *must_malloc(char *const descr, size_t size);
void lock(char *const descr, pthread_mutex_t *mutex); void lock(char *const descr, pthread_mutex_t *mutex);
void unlock(char *const descr, pthread_mutex_t *mutex); void unlock(char *const descr, pthread_mutex_t *mutex);
void write_exactly(char *descr, int fd, void *buf, size_t nbyte); void write_exactly(char *descr, int fd, void *buf, size_t nbyte);
#define IN_BUFSZ ((1 << 20) + 16) #endif /* _TRANSFUSED_H_ */
#define OUT_BUFSZ ((1 << 20) + 64)
#define EVENT_BUFSZ 4096
#define CTL_BUFSZ 65536
#define DEFAULT_FUSERMOUNT "/bin/fusermount"
#define DEFAULT_SOCKET "v:_:1525"
#define DEFAULT_SERVER "v:2:1524"
#define PING 128
#define RMDIR_SYSCALL 0
#define UNLINK_SYSCALL 1
#define MKDIR_SYSCALL 2
#define SYMLINK_SYSCALL 3
#define TRUNCATE_SYSCALL 4
#define CHMOD_SYSCALL 5
#define MKNOD_REG_SYSCALL 6
// these could be turned into an enum probably but... C standard nausea
#define MOUNT_SUITABILITY_REQUEST 1
#define EXPORT_SUITABILITY_REQUEST 2
#define TRANSFUSE_LOG_ERROR 1
#define TRANSFUSE_LOG_NOTICE 2
#define PONG_REPLY 3
#define MOUNT_SUITABILITY_REPLY 4
#define TRANSFUSE_NOTIFY_CHANNEL 5

View File

@ -15,8 +15,10 @@
#include <inttypes.h> #include <inttypes.h>
#include "transfused.h" #include "transfused.h"
#include "transfused_log.h"
void log_timestamp(int fd) { void log_timestamp(int fd)
{
char timestamp[26]; char timestamp[26];
int msec; int msec;
struct tm *tm_info; struct tm *tm_info;
@ -29,56 +31,57 @@ void log_timestamp(int fd) {
msec -= 1000; msec -= 1000;
tv.tv_sec++; tv.tv_sec++;
} }
tm_info = localtime(&tv.tv_sec); tm_info = localtime(&tv.tv_sec);
strftime(timestamp, 26, "%Y-%m-%d %H:%M:%S", tm_info); strftime(timestamp, 26, "%Y-%m-%d %H:%M:%S", tm_info);
dprintf(fd, "%s.%03d ", timestamp, msec); dprintf(fd, "%s.%03d ", timestamp, msec);
} }
void die void vlog_sock_locked(int fd, uint16_t msg_type, const char *fmt, va_list args)
(int exit_code, parameters * params, const char * parg, const char * fmt, ...); {
void vlog_sock_locked
(int fd, uint16_t msg_type, const char * fmt, va_list args) {
int rc, len; int rc, len;
va_list targs; va_list targs;
char *fill; char *fill;
va_copy(targs, args); va_copy(targs, args);
len = vsnprintf(NULL, 0, fmt, targs); len = vsnprintf(NULL, 0, fmt, targs);
if (len < 0) die(1, NULL, NULL, "Couldn't log due to vsnprintf failure"); if (len < 0)
die(1, NULL, NULL, "Couldn't log due to vsnprintf failure");
va_end(targs); va_end(targs);
rc = len + 4 + 2; // 4 for length itself and 2 for message type /* 4 for length itself and 2 for message type */
write_exactly("vlog_sock_locked", fd, (uint32_t *) &rc, sizeof(uint32_t)); rc = len + 4 + 2;
write_exactly("vlog_sock_locked", fd,
(uint32_t *)&rc, sizeof(uint32_t));
write_exactly("vlog_sock_locked", fd, &msg_type, sizeof(uint16_t)); write_exactly("vlog_sock_locked", fd, &msg_type, sizeof(uint16_t));
va_copy(targs, args); va_copy(targs, args);
rc = vdprintf(fd, fmt, targs); rc = vdprintf(fd, fmt, targs);
if (rc < 0) die(1, NULL, "Couldn't send log message with vdprintf", ""); if (rc < 0)
die(1, NULL, "Couldn't send log message with vdprintf", "");
va_end(targs); va_end(targs);
if (rc < len) { // we didn't write the whole message :-( if (rc < len) {
/* we didn't write the whole message :-( */
rc = len - rc; rc = len - rc;
fill = (char *)calloc(rc, 1); fill = (char *)calloc(rc, 1);
if (fill == NULL) die(1, NULL, "vlog_sock_locked fill", ""); if (fill == NULL)
die(1, NULL, "vlog_sock_locked fill", "");
write_exactly("vlog_sock_locked fill", fd, fill, rc); write_exactly("vlog_sock_locked fill", fd, fill, rc);
} }
} }
void log_sock_locked(int fd, uint16_t msg_type, const char * fmt, ...) { void log_sock_locked(int fd, uint16_t msg_type, const char *fmt, ...)
{
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
vlog_sock_locked(fd, msg_type, fmt, args); vlog_sock_locked(fd, msg_type, fmt, args);
va_end(args); va_end(args);
} }
void die void die(int exit_code, parameters *params, const char *parg,
(int exit_code, parameters * params, const char * parg, const char * fmt, ...) const char *fmt, ...)
{ {
va_list argp, targs; va_list argp, targs;
int in_errno = errno; int in_errno = errno;
@ -94,7 +97,8 @@ void die
vsyslog(LOG_CRIT, fmt, targs); vsyslog(LOG_CRIT, fmt, targs);
va_end(targs); va_end(targs);
if (fd != 0) vlog_sock_locked(fd, TRANSFUSE_LOG_ERROR, fmt, argp); if (fd != 0)
vlog_sock_locked(fd, TRANSFUSE_LOG_ERROR, fmt, argp);
va_end(argp); va_end(argp);
if (parg != NULL) { if (parg != NULL) {
@ -106,76 +110,81 @@ void die
} else { } else {
syslog(LOG_CRIT, "%s", strerror(in_errno)); syslog(LOG_CRIT, "%s", strerror(in_errno));
if (fd != 0) if (fd != 0)
log_sock_locked(fd, TRANSFUSE_LOG_ERROR, "%s", strerror(in_errno)); log_sock_locked(fd, TRANSFUSE_LOG_ERROR,
"%s", strerror(in_errno));
} }
} }
if (fd != 0)
if (fd != 0) close(fd); // flush close(fd); /* flush */
exit(exit_code); exit(exit_code);
// Nobody else should die before we terminate everything /* Nobody else should die before we terminate everything */
unlock("die ctl_lock", &params->ctl_lock); unlock("die ctl_lock", &params->ctl_lock);
} }
void vlog_locked void vlog_locked(parameters *params, uint16_t msg_type,
(parameters * params, uint16_t msg_type, const char * fmt, va_list args) { const char *fmt, va_list args)
{
int rc; int rc;
int fd = params->ctl_sock; int fd = params->ctl_sock;
va_list targs; va_list targs;
if (fd != 0) vlog_sock_locked(fd, msg_type, fmt, args); if (fd != 0) {
else { vlog_sock_locked(fd, msg_type, fmt, args);
} else {
va_copy(targs, args); va_copy(targs, args);
// TODO: translate msg_type to syslog message type /* TODO: translate msg_type to syslog message type */
vsyslog(LOG_INFO, fmt, targs); vsyslog(LOG_INFO, fmt, targs);
va_end(targs); va_end(targs);
fd = params->logfile_fd; fd = params->logfile_fd;
if (fd != 0) { if (fd != 0) {
va_copy(targs, args); va_copy(targs, args);
// TODO: include message type? /* TODO: include message type? */
rc = vdprintf(fd, fmt, targs); rc = vdprintf(fd, fmt, targs);
if (rc < 0) die(1, NULL, "Couldn't write log message with vdprintf", ""); if (rc < 0)
die(1, NULL,
"Couldn't write log message with vdprintf", "");
va_end(targs); va_end(targs);
} }
} }
} }
void vlog_time_locked void vlog_time_locked(parameters *params, uint16_t msg_type,
(parameters * params, uint16_t msg_type, const char * fmt, va_list args) { const char *fmt, va_list args)
{
int fd = params->logfile_fd; int fd = params->logfile_fd;
if (fd != 0 && params->ctl_sock == 0) log_timestamp(fd); if (fd != 0 && params->ctl_sock == 0)
log_timestamp(fd);
vlog_locked(params, msg_type, fmt, args); vlog_locked(params, msg_type, fmt, args);
} }
void log_time_locked void log_time_locked(parameters *params, uint16_t msg_type,
(parameters * params, uint16_t msg_type, const char * fmt, ...) { const char *fmt, ...)
{
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
vlog_time_locked(params, msg_type, fmt, args); vlog_time_locked(params, msg_type, fmt, args);
va_end(args); va_end(args);
} }
void log_time(parameters * params, const char * fmt, ...) { void log_time(parameters *params, const char *fmt, ...)
{
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
lock("log_time ctl_lock", &params->ctl_lock); lock("log_time ctl_lock", &params->ctl_lock);
vlog_time_locked(params, TRANSFUSE_LOG_ERROR, fmt, args); vlog_time_locked(params, TRANSFUSE_LOG_ERROR, fmt, args);
unlock("log_time ctl_lock", &params->ctl_lock); unlock("log_time ctl_lock", &params->ctl_lock);
va_end(args); va_end(args);
} }
void log_notice_time(parameters * params, const char * fmt, ...) { void log_notice_time(parameters *params, const char *fmt, ...)
{
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
lock("log_time ctl_lock", &params->ctl_lock); lock("log_time ctl_lock", &params->ctl_lock);
vlog_time_locked(params, TRANSFUSE_LOG_NOTICE, fmt, args); vlog_time_locked(params, TRANSFUSE_LOG_NOTICE, fmt, args);
unlock("log_time ctl_lock", &params->ctl_lock); unlock("log_time ctl_lock", &params->ctl_lock);
@ -188,21 +197,22 @@ typedef struct {
char *msg; char *msg;
} log_thread_state; } log_thread_state;
void * log_time_thread(void * log_state_ptr) { void *log_time_thread(void *log_state_ptr)
{
log_thread_state *log_state = log_state_ptr; log_thread_state *log_state = log_state_ptr;
log_time(log_state->params, log_state->msg); log_time(log_state->params, log_state->msg);
free(log_state->msg); free(log_state->msg);
free(log_state); free(log_state);
return NULL; return NULL;
} }
void thread_log_time(connection_t * conn, const char * fmt, ...) { void thread_log_time(connection_t *conn, const char *fmt, ...)
{
log_thread_state *log_state;
pthread_t logger; pthread_t logger;
va_list args; va_list args;
log_thread_state * log_state;
log_state = must_malloc("thread_log_time log_state", log_state = must_malloc("thread_log_time log_state",
sizeof(log_thread_state)); sizeof(log_thread_state));
@ -210,30 +220,31 @@ void thread_log_time(connection_t * conn, const char * fmt, ...) {
va_start(args, fmt); va_start(args, fmt);
if (vasprintf(&log_state->msg, fmt, args) == -1) if (vasprintf(&log_state->msg, fmt, args) == -1)
die(1, conn->params, "Couldn't allocate thread_log_time message", ""); die(1, conn->params,
"Couldn't allocate thread_log_time message", "");
va_end(args); va_end(args);
// TODO: We currently spawn a new thread for every message. This is /* TODO: We currently spawn a new thread for every
// far from ideal but fine for now as we anticipate thread-sensitive * message. This is far from ideal but fine for now as we
// log demand to be low. * anticipate thread-sensitive log demand to be low. */
errno = pthread_create(&logger, &detached, log_time_thread, log_state);
if ((errno = pthread_create(&logger, &detached, log_time_thread, log_state))) if (errno)
die(1, conn->params, "", die(1, conn->params, "",
"Couldn't create log thread for %s connection %s: ", "Couldn't create log thread for %s connection %s: ",
conn->type_descr, conn->mount_point); conn->type_descr, conn->mount_point);
} }
void log_continue_locked(parameters * params, const char * fmt, ...) { void log_continue_locked(parameters *params, const char *fmt, ...)
{
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
vlog_locked(params, TRANSFUSE_LOG_ERROR, fmt, args); vlog_locked(params, TRANSFUSE_LOG_ERROR, fmt, args);
va_end(args); va_end(args);
} }
void log_continue(parameters * params, const char * fmt, ...) { void log_continue(parameters *params, const char *fmt, ...)
{
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);

View File

@ -1,27 +1,26 @@
#ifndef _TRANSFUSED_LOG_H_
#define _TRANSFUSED_LOG_H_
#include <stdarg.h> #include <stdarg.h>
#include <inttypes.h> #include <inttypes.h>
#include "transfused.h" #include "transfused.h"
void die void die(int exit_code, parameters *params, const char *perror_arg,
(int exit_code, parameters * params, const char * perror_arg,
const char *fmt, ...); const char *fmt, ...);
void vlog_locked void vlog_locked(parameters *params, uint16_t msg_type,
(parameters * params, uint16_t msg_type, const char * fmt, va_list args); const char *fmt, va_list args);
void vlog_time_locked(parameters *params, uint16_t msg_type,
const char *fmt, va_list args);
void vlog_time_locked void log_time_locked(parameters *params, uint16_t msg_type,
(parameters * params, uint16_t msg_type, const char * fmt, va_list args); const char *fmt, ...);
void log_time_locked
(parameters * params, uint16_t msg_type, const char * fmt, ...);
void log_time(parameters *params, const char *fmt, ...); void log_time(parameters *params, const char *fmt, ...);
void log_notice_time(parameters *params, const char *fmt, ...); void log_notice_time(parameters *params, const char *fmt, ...);
void thread_log_time(connection_t *conn, const char *fmt, ...); void thread_log_time(connection_t *conn, const char *fmt, ...);
void log_continue_locked(parameters *params, const char *fmt, ...); void log_continue_locked(parameters *params, const char *fmt, ...);
void log_continue(parameters *params, const char *fmt, ...); void log_continue(parameters *params, const char *fmt, ...);
#endif /* _TRANSFUSED_LOG_H_ */

View File

@ -7,9 +7,11 @@
#include "transfused_log.h" #include "transfused_log.h"
long parse_cid(const char * address) { long parse_cid(const char *address)
{
char *end = NULL; char *end = NULL;
long cid = strtol(address, &end, 10); long cid = strtol(address, &end, 10);
if (address == end || *end != ':') { if (address == end || *end != ':') {
*end = 0; *end = 0;
die(2, NULL, NULL, "Invalid vsock cid: %s", address); die(2, NULL, NULL, "Invalid vsock cid: %s", address);
@ -17,9 +19,11 @@ long parse_cid(const char * address) {
return cid; return cid;
} }
long parse_port(const char * port_str) { long parse_port(const char *port_str)
{
char *end = NULL; char *end = NULL;
long port = strtol(port_str, &end, 10); long port = strtol(port_str, &end, 10);
if (port_str == end || *end != '\0') { if (port_str == end || *end != '\0') {
*end = 0; *end = 0;
die(2, NULL, NULL, "Invalid vsock port: %s", port_str); die(2, NULL, NULL, "Invalid vsock port: %s", port_str);
@ -27,12 +31,15 @@ long parse_port(const char * port_str) {
return port; return port;
} }
int find_colon(const char * address) { int find_colon(const char *address)
{
int colon = 0; int colon = 0;
while (address[colon] != '\0') while (address[colon] != '\0')
if (address[colon] == ':') break; if (address[colon] == ':')
else colon++; break;
else
colon++;
if (address[colon] == '\0') if (address[colon] == '\0')
die(2, NULL, NULL, "Missing port in vsock address %s", address); die(2, NULL, NULL, "Missing port in vsock address %s", address);
@ -40,20 +47,21 @@ int find_colon(const char * address) {
return colon; return colon;
} }
int bind_vsock(const char * address) { int bind_vsock(const char *address)
{
long cid, port; long cid, port;
int colon; int colon;
struct sockaddr_vm sa_listen = { struct sockaddr_vm sa_listen = {
.svm_family = AF_VSOCK, .svm_family = AF_VSOCK,
}; };
int sock_fd; int sock_fd;
colon = find_colon(address); colon = find_colon(address);
if (address[0] == '_' && colon == 1) cid = VMADDR_CID_ANY; if (address[0] == '_' && colon == 1)
else cid = parse_cid(address); cid = VMADDR_CID_ANY;
else
cid = parse_cid(address);
port = parse_port(address + colon + 1); port = parse_port(address + colon + 1);
@ -70,14 +78,13 @@ int bind_vsock(const char * address) {
return sock_fd; return sock_fd;
} }
int connect_vsock(const char * address) { int connect_vsock(const char *address)
{
long cid, port; long cid, port;
int colon; int colon;
struct sockaddr_vm sa_connect = { struct sockaddr_vm sa_connect = {
.svm_family = AF_VSOCK, .svm_family = AF_VSOCK,
}; };
int sock_fd; int sock_fd;
colon = find_colon(address); colon = find_colon(address);

View File

@ -1,2 +1,7 @@
#ifndef _TRANSFUSED_VSOCK_H_
#define _TRANSFUSED_VSOCK_H_
int bind_vsock(const char *address); int bind_vsock(const char *address);
int connect_vsock(const char *address); int connect_vsock(const char *address);
#endif /* _TRANSFUSED_VSOCK_H_ */