--- /dev/null
+cmake_minimum_required(VERSION 2.6)
+
+PROJECT(mountd C)
+ADD_DEFINITIONS(-Os -ggdb -Wall -Werror --std=gnu99 -Wmissing-declarations)
+
+SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
+
+ADD_EXECUTABLE(mountd main.c log.c sys.c autofs.c mount.c timer.c signal.c ucix.c led.c fs.c ucix.c)
+TARGET_LINK_LIBRARIES(mountd uci ubox)
+
+INSTALL(TARGETS mountd
+ RUNTIME DESTINATION sbin
+)
+++ /dev/null
-PROG=mountd
-OBJS=main.o lib/log.o lib/sys.o lib/autofs.o lib/mount.o lib/timer.o lib/signal.o lib/ucix.o lib/led.o lib/fs.o lib/ucix.o
-
-LDFLAGS?=
-LDFLAGS+=-ldl -luci
-
-CFLAGS?=
-CFLAGS+= -Wall
-
-all: mountd
-
-mountd: $(OBJS)
- $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@
-
-clean:
- rm -f lib/*.o *.o $(PROG)
-
-%.o: %.c
- $(CC) $(CFLAGS) -c $^ -o $@
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/time.h>
-#include <sys/poll.h>
+#include <poll.h>
#include <linux/auto_fs4.h>
#include "include/log.h"
#include "include/mount.h"
#include "include/signal.h"
#include "include/ucix.h"
+#include "include/autofs.h"
int fdin = 0; /* data coming out of the kernel */
int fdout = 0;/* data going into the kernel */
time_t uci_timeout;
char uci_path[32];
-void umount_autofs(void)
+static void umount_autofs(void)
{
system_printf("umount %s 2> /dev/null", "/tmp/run/mountd/");
}
return 0;
}
-void expire_proc(void)
+static void expire_proc(void)
{
struct autofs_packet_expire pkt;
while(ioctl(fdin, AUTOFS_IOC_EXPIRE, &pkt) == 0)
static int autofs_in(union autofs_v5_packet_union *pkt)
{
+ int res;
struct pollfd fds[1];
fds[0].fd = fdout;
while(1)
{
- if(poll(fds, 2, 1000) == -1)
+ res = poll(fds, 1, -1);
+
+ if (res == -1)
{
if (errno == EINTR)
continue;
log_printf("failed while trying to read packet from kernel\n");
return -1;
}
- if(fds[0].revents & POLLIN)
+ else if ((res > 0) && (fds[0].revents & POLLIN))
+ {
return fullread(pkt, sizeof(*pkt));
+ }
}
}
return pid;
}
-void autofs_cleanup_handler(void)
+static void autofs_cleanup_handler(void)
{
close(fdin);
close(fdout);
umount_autofs();
}
-void autofs_init(void)
+static void autofs_init(void)
{
int kproto_version;
char *p;
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
-#include <error.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
typedef int (*dfunc)(int);
-unsigned short
+static unsigned short
get_le_short(void *from)
{
unsigned char *p = from;
(unsigned short)p[0];
}
-unsigned int get_le_long(void *from)
+static unsigned int get_le_long(void *from)
{
unsigned char *p = from;
return ((unsigned int)(p[3]) << 24) +
(unsigned int)p[0];
}
-unsigned short get_be_short(void *from)
+static unsigned short get_be_short(void *from)
{
unsigned char *p = from;
return ((unsigned short)(p[0]) << 8) +
(unsigned short)p[1];
}
-unsigned int get_be_long(void *from)
+static unsigned int get_be_long(void *from)
{
unsigned char *p = from;
return ((unsigned int)(p[0]) << 24) +
(unsigned int)p[3];
}
-int get_buffer(int fd, unsigned char *b, int offset, int len)
+static int get_buffer(int fd, unsigned char *b, int offset, int len)
{
if(lseek(fd, offset, SEEK_SET) != offset)
return -1;
}
#define MBR_BUF_SIZE 512
-int detect_mbr(int fd)
+static int detect_mbr(int fd)
{
int ret = NONE;
unsigned char *buffer = (unsigned char*)malloc(MBR_BUF_SIZE);
#define EFI_BUF_OFFSET 512
#define EFI_BUF_SIZE 512
-int detect_efi(int fd)
+static int detect_efi(int fd)
{
int ret = NONE;
unsigned char *buffer = (unsigned char*)malloc(EFI_BUF_SIZE);
}
#define EXT2_BUF_SIZE 1024
-int detect_ext23(int fd)
+static int detect_ext23(int fd)
{
int ret = NONE;
unsigned char *buffer = (unsigned char*)malloc(EXT2_BUF_SIZE);
goto out;
if(get_le_short(buffer + 56) == 0xEF53)
{
- if((get_le_long(buffer + 96) & 0x0008)
- || (get_le_long(buffer + 92) & 0x0004))
- ret = EXT3;
+ if(get_le_long(buffer + 92) & 0x0004)
+ {
+ if ((get_le_long(buffer + 96) < 0x0000040)
+ && (get_le_long(buffer + 100) < 0x0000008))
+ ret = EXT3;
+ else
+ ret = EXT4;
+ }
else
ret = EXT2;
}
}
#define FAT_BUF_SIZE 512
-int detect_fat(int fd)
+static int detect_fat(int fd)
{
int ret = NONE;
unsigned char *buffer = (unsigned char*)malloc(FAT_BUF_SIZE);
#define HFSPLUS_VOL_JOURNALED (1 << 13)
#define HFSPLUS_BUF_SIZE 512
-int detect_hfsplus(int fd)
+static int detect_hfsplus(int fd)
{
int ret = NONE;
unsigned short magic;
}
#define NTFS_BUF_SIZE 512
-int detect_ntfs(int fd)
+static int detect_ntfs(int fd)
{
int ret = NONE;
unsigned char *buffer = (unsigned char*)malloc(NTFS_BUF_SIZE);
}
#define EXTENDED_BUF_SIZE 512
-int detect_extended(int fd)
+static int detect_extended(int fd)
{
int ret = NONE;
unsigned char *buffer = (unsigned char*)malloc(MBR_BUF_SIZE);
#define EFI 7
#define NTFS 8
#define EXTENDED 9
+#define EXT4 10
int detect_fs(char *device);
#include "include/ucix.h"
#include "include/log.h"
#include "include/timer.h"
+#include "include/led.h"
char usbled[16];
#include <syslog.h>
#include <stdarg.h>
+#include "include/log.h"
+
extern int daemonize;
void log_start(void)
#include <stdio.h>
+#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc, char *argv[])
{
- daemon(0,0);
+ if ((argc < 2) || strcmp(argv[1], "-f"))
+ daemon(0,0);
+
daemonize = 1;
log_start();
log_printf("Starting OpenWrt (auto)mountd V1\n");
#include <glob.h>
#include <libgen.h>
#include <poll.h>
+#include <dirent.h>
#include "include/log.h"
#include "include/list.h"
#include "include/autofs.h"
#include "include/ucix.h"
#include "include/fs.h"
+#include "include/mount.h"
int mount_new(char *path, char *dev);
"EXT3",
"FAT",
"HFSPLUS",
- "NTFS"
+ "",
+ "NTFS",
+ "",
+ "EXT4"
};
#define MAX_MOUNTED 32
int mounted_count = 0;
extern char uci_path[32];
-void mount_dump_uci_state(void)
+static void mount_dump_uci_state(void)
{
struct uci_context *ctx;
struct list_head *p;
ucix_add_option(ctx, mountd, q->serial, "rev", q->rev);
snprintf(t, 64, "size%d", atoi(&q->dev[3]));
ucix_add_option(ctx, mountd, q->serial, t, q->size);
- if(q->fs > MBR && q->fs <= NTFS)
+ if(q->fs > MBR && q->fs <= EXT4)
{
snprintf(t, 64, "fs%d", atoi(&q->dev[3]));
ucix_add_option(ctx, mountd, q->serial, t, fs_names[q->fs]);
ucix_cleanup(ctx);
}
-struct mount* mount_find(char *name, char *dev)
+static struct mount* mount_find(char *name, char *dev)
{
struct list_head *p;
list_for_each(p, &mounts)
return 0;
}
-void mount_add_list(char *name, char *dev, char *serial,
+static void mount_add_list(char *name, char *dev, char *serial,
char *vendor, char *model, char *rev, int ignore, char *size, char *sector_size, int fs)
{
struct mount *mount;
char tmp[64], tmp2[64];
- if(fs <= MBR || fs > NTFS)
+ if(fs <= MBR || fs > EXT4)
return;
mount = malloc(sizeof(struct mount));
INIT_LIST_HEAD(&mount->list);
mount->mounted = 0;
mount->fs = fs;
list_add(&mount->list, &mounts);
- if((!mount->ignore) && (mount->fs > MBR) && (mount->fs <= NTFS))
+ if((!mount->ignore) && (mount->fs > MBR) && (mount->fs <= EXT4))
{
log_printf("new mount : %s -> %s (%s)\n", name, dev, fs_names[mount->fs]);
snprintf(tmp, 64, "%s%s", uci_path, name);
}
}
-int mount_check_disc(char *disc)
+static int mount_check_disc(char *disc)
{
FILE *fp = fopen("/proc/mounts", "r");
char tmp[256];
return avail;
}
-int mount_wait_for_disc(char *disc)
+static int mount_wait_for_disc(char *disc)
{
int i = 10;
while(i--)
log_printf("mount -t vfat -o rw,uid=1000,gid=1000 /dev/%s %s", mount->dev, tmp);
ret = system_printf("mount -t vfat -o rw,uid=1000,gid=1000 /dev/%s %s", mount->dev, tmp);
}
+ if(mount->fs == EXT4)
+ {
+ log_printf("mount -t ext4 -o rw,defaults /dev/%s %s", mount->dev, tmp);
+ ret = system_printf("mount -t ext4 -o rw,defaults /dev/%s %s", mount->dev, tmp);
+ }
if(mount->fs == EXT3)
{
log_printf("mount -t ext3 -o rw,defaults /dev/%s %s", mount->dev, tmp);
return 0;
}
-int dir_sort(const void *a, const void *b)
+static int dir_sort(const struct dirent **a, const struct dirent **b)
{
return 0;
}
-int dir_filter(const struct dirent *a)
+static int dir_filter(const struct dirent *a)
{
if(strstr(a->d_name, ":"))
return 1;
return 0;
}
-char* mount_get_serial(char *dev)
+static char* mount_get_serial(char *dev)
{
static char tmp[64];
static char tmp2[64];
} else {
/* serial string id is cheap, but makes the discs anonymous */
unsigned char uniq[6];
+ unsigned int *u = (unsigned int*) uniq;
int l = strlen(serial);
int i;
static char disc_id[13];
{
uniq[i%6] += serial[i];
}
- sprintf(disc_id, "%08X%02X%02X", *((unsigned int*)&uniq[0]), uniq[4], uniq[5]);
+ sprintf(disc_id, "%08X%02X%02X", *u, uniq[4], uniq[5]);
//log_printf("Serial number - %s %s\n", serial, disc_id);
return disc_id;
}
return 0;
}
-void mount_dev_add(char *dev)
+static void mount_dev_add(char *dev)
{
struct mount *mount = mount_find(0, dev);
if(!mount)
}
}
-void mount_dev_del(char *dev)
+static void mount_dev_del(char *dev)
{
struct mount *mount = mount_find(0, dev);
char tmp[256];
return 0;
}
-void mount_check_mount_list(void)
+static void mount_check_mount_list(void)
{
FILE *fp = fopen("/proc/mounts", "r");
char tmp[256];
fclose(fp);
}
-/* FIXME: we need ore intelligence here */
-int dir_filter2(const struct dirent *a)
+/* FIXME: we need more intelligence here */
+static int dir_filter2(const struct dirent *a)
{
if(/*strcmp(a->d_name, "sda") &&*/(!strncmp(a->d_name, "sd", 2)))
return 1;
char block[MAX_BLOCK][MAX_BLOCK];
int blk_cnt = 0;
-int check_block(char *b)
+static int check_block(char *b)
{
int i;
for(i = 0; i < blk_cnt; i++)
return 0;
}
-void mount_enum_drives(void)
+static void mount_enum_drives(void)
{
struct dirent **namelist, **namelist2;
int i, n = scandir("/sys/block/", &namelist, dir_filter2, dir_sort);
mount_dev_add(block[i]);
}
-void mount_check_enum(void)
+static void mount_check_enum(void)
{
waitpid(-1, 0, WNOHANG);
mount_enum_drives();
#include "include/log.h"
#include "include/list.h"
#include "include/led.h"
+#include "include/signal.h"
void (*crtlc_cb)(void) = 0;
-void handlerINT(int s)
+static void handlerINT(int s)
{
log_printf("caught sig int ... cleaning up\n");
if(crtlc_cb)
#include <stdlib.h>
#include "include/log.h"
+#include "include/sys.h"
int system_printf(char *fmt, ...)
{
list_add(&timer->list, &timers);
}
-void timer_proc(int signo)
+static void timer_proc(int signo)
{
struct list_head *p;
list_for_each(p, &timers)
struct uci_context* uci_init(char *config_file)
{
struct uci_context *ctx = uci_alloc_context();
- uci_add_history_path(ctx, "/var/state");
+ uci_add_delta_path(ctx, "/var/state");
if(uci_load(ctx, config_file, &p) != UCI_OK)
{
log_printf("/etc/config/%s is missing or corrupt\n", config_file);
struct uci_context* ucix_init(const char *config_file)
{
struct uci_context *ctx = uci_alloc_context();
- uci_add_history_path(ctx, "/var/state");
+ uci_add_delta_path(ctx, "/var/state");
if(uci_load(ctx, config_file, NULL) != UCI_OK)
{
printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file);