Skip to content

Commit

Permalink
Introduce an IPC mechanism and clean a bit the source
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonBoy committed Apr 10, 2015
1 parent 50fce3b commit 27d5efb
Show file tree
Hide file tree
Showing 8 changed files with 1,007 additions and 580 deletions.
53 changes: 34 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,40 +1,53 @@
VERSION=v0.6
VERSION_GIT=$(shell test -d .git && git describe 2> /dev/null)

ifneq "$(VERSION_GIT)" ""
VERSION=$(VERSION_GIT)
endif

CC ?= gcc
CFLAGS := -O2 $(CFLAGS)
LDFLAGS := -ludev -lmount $(LDFLAGS)
CFDEBUG = -g3 -pedantic -Wall -Wunused-parameter -Wlong-long
CFLAGS += -std=c99 \
-D_GNU_SOURCE \
-Wall -Wunused-parameter -O2 \
-DVERSION_STR="\"$(VERSION)\""
CFDEBUG = -g3 -pedantic -Wlong-long
CFDEBUG += -Wsign-conversion -Wconversion -Wimplicit-function-declaration

PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
SYSTEMDDIR ?= $(PREFIX)/lib/systemd
LIBS = libudev mount glib-2.0
LDFLAGS := `pkg-config --libs $(LIBS)` $(LDFLAGS)

EXEC = ldm
SRCS = ldm.c
OBJS = $(SRCS:.c=.o)
BINDIR ?= /usr/bin
SYSTEMDDIR ?= /usr/lib/systemd

all: $(EXEC) doc
all: ldm ldmc doc

.c.o:
$(CC) $(CFLAGS) -o $@ -c $<
$(CC) $(CFLAGS) `pkg-config --cflags $(LIBS)` -o $@ -c $<

ldm: ipc.o ldm.o
$(CC) $(LDFLAGS) -o ldm ipc.o ldm.o

$(EXEC): $(OBJS)
$(CC) $(LDFLAGS) -o $(EXEC) $(OBJS)
ldmc: ipc.o ldmc.o
$(CC) $(LDFLAGS) -o ldmc ipc.o ldmc.o

debug: $(EXEC)
debug: ldm ldmc
debug: CC += $(CFDEBUG)

doc: README.pod
@pod2man --section=1 --center="ldm Manual" --name "ldm" --release="ldm $(shell git describe)" README.pod > ldm.1
@pod2man --section=1 --center="ldm Manual" --name "ldm" --release="$(VERSION)" README.pod > ldm.1
@pod2man --section=1 --center="ldmc Manual" --name "ldmc" --release="$(VERSION)" ldmc.pod > ldmc.1

clean:
$(RM) *.o *.1 ldm
$(RM) *.o *.1 ldm ldmc

mrproper: clean
$(RM) $(EXEC)
$(RM) ldm ldmc

install-main: ldm doc
install -D -m 755 ldm $(DESTDIR)$(BINDIR)/ldm
install -D -m 644 ldm.1 $(DESTDIR)$(PREFIX)/share/man/man1/ldm.1
install -D -m 755 ldmc $(DESTDIR)$(BINDIR)/ldmc
install -D -m 644 ldm.1 $(DESTDIR)/usr/share/man/man1/ldm.1
install -D -m 644 ldmc.1 $(DESTDIR)/usr/share/man/man1/ldmc.1

install-systemd: ldm.service
install -D -m 644 ldm.service $(DESTDIR)$(SYSTEMDDIR)/system/ldm.service
Expand All @@ -43,7 +56,9 @@ install: all install-main install-systemd

uninstall:
$(RM) $(DESTDIR)$(BINDIR)/ldm
$(RM) $(DESTDIR)$(PREFIX)/share/man/man1/ldm.1
$(RM) $(DESTDIR)$(BINDIR)/ldmc
$(RM) $(DESTDIR)/usr/share/man/man1/ldm.1
$(RM) $(DESTDIR)/usr/share/man/man1/ldmc.1
$(RM) $(DESTDIR)$(SYSTEMDDIR)/system/ldm.service

.PHONY: all debug clean mrproper install install-main install-systemd uninstall
19 changes: 12 additions & 7 deletions README.pod
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ ldm - Lightweight Device Mounter

=head1 SYNOPSIS

I<ldm> [-d] [-r I<device>] [-g I<gid>] [-u I<uid>] [-p I<path>] [-c I<command>] [-h]
I<ldm> [-d] [-g I<gid>] [-u I<uid>] [-p I<path>] [-c I<command>] [-h]

=head1 DESCRIPTION

ldm is a lightweight device mounter following the UNIX philosophy written in C and based on udev and libmount.
The daemon can be controlled with B<ldmc>.

=head1 OPTIONS

=over

=item B<-d>
=item B<-d>

Run ldm as a daemon.

=item B<-r> I<device>

Ask the ldm daemon to unmount the selected device.

=item B<-g> I<gid>

Specify the gid of the user owning the mount points.
Expand All @@ -48,6 +45,10 @@ The complete path to the mountpoint.

The path pointing to the device node in /dev

=item B<LDM_FS>

The filesystem on the mounted device.

=item B<LDM_ACTION>

The action ldm has just performed, it can either be I<mount> or I<unmount>
Expand Down Expand Up @@ -92,10 +93,14 @@ USER_UID = <i>uid</i></br>

=end html

=head1 SEE ALSO

ldmc(1)

=head1 WWW

L<git repository|https://github.com/LemonBoy/ldm>

=head1 AUTHOR

2011-2014 (C) The Lemon Man <[email protected]>
2011-2015 (C) The Lemon Man <[email protected]>
150 changes: 150 additions & 0 deletions ipc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>

#define IPC_SOCKET "/run/ldm.fifo"

void
ipc_deinit (int fd)
{
if (fd >= 0)
close (fd);
unlink (IPC_SOCKET);
}

int
ipc_init (int as_master)
{
int sock;
int flags;
struct sockaddr_un so;

sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
perror("socket");
return -1;
}

// Make sure that there are no leftovers
unlink (IPC_SOCKET);

memset(&so, 0, sizeof(struct sockaddr_un));
so.sun_family = AF_UNIX;
strncpy(so.sun_path, IPC_SOCKET, sizeof(so.sun_path));

if (as_master) {
// The master waits for the slaves to connect
if (bind(sock, (struct sockaddr *)&so, sizeof(struct sockaddr_un)) < 0) {
perror("bind");
return -1;
}

// Make the sock non-blocking
flags = fcntl(sock, F_GETFL, 0);
if (flags < 0) {
perror("fcntl");
return -1;
}

if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) {
perror("fcntl");
return -1;
}
}
else {
// The slave connects to the master
if (connect(sock, (struct sockaddr *)&so, sizeof(struct sockaddr_un)) < 0) {
perror("connect");
return -1;
}
}

return sock;
}

char
ipc_read_one (int fd)
{
char resp;

if (fd < 0)
return 0;

if (read(fd, &resp, 1) != 1) {
perror("read");
return 0;
}

return resp;
}

int
ipc_read_line (int fd, char *line, int max_line_length)
{
int p;

if (fd < 0 || !line)
return 0;

for (p = 0; p < max_line_length - 1; p++) {
if (read(fd, &line[p], 1) != 1) {
perror("read");
break;
}

if (line[p] == '\n')
break;
}

line[p] = '\0';

// Don't take into account the \n
return p? p - 1: 0;
}

int
ipc_sendf (int fd, const char *fmt, ...)
{
va_list args, args_;
int fmt_length;
char *buf;

va_start(args, fmt);

// Make a copy since the first vsnprintf call modifies it
va_copy(args_, args);

// Obtain the final string length first
fmt_length = vsnprintf(NULL, 0, fmt, args_);
if (fmt_length < 0) {
perror("vsprintf");
va_end(args);
return 0;
}

buf = malloc(fmt_length + 1);
if (!buf) {
perror("errno");
va_end(args);
return 0;
}

vsnprintf(buf, fmt_length + 1, fmt, args);

// Don't send the trailing NULL
if (write(fd, buf, fmt_length) != fmt_length) {
perror("write");
free(buf);
va_end(args);
return 0;
}

free(buf);
va_end(args);

return fmt_length;
}
7 changes: 7 additions & 0 deletions ipc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

void ipc_deinit (int fd);
int ipc_init (int as_master);
char ipc_read_one (int fd);
int ipc_read_line (int fd, char *line, int max_line_length);
int ipc_sendf (int fd, const char *fmt, ...);
Loading

0 comments on commit 27d5efb

Please sign in to comment.