-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce an IPC mechanism and clean a bit the source
- Loading branch information
Showing
8 changed files
with
1,007 additions
and
580 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 |
---|---|---|
|
@@ -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. | ||
|
@@ -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> | ||
|
@@ -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]> |
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 |
---|---|---|
@@ -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; | ||
} |
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 |
---|---|---|
@@ -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, ...); |
Oops, something went wrong.