Browse Source

import patches/ and cmake support

Signed-off-by: John Crispin <blogic@openwrt.org>
John Crispin 8 years ago
parent
commit
827e9eb7ea
14 changed files with 92 additions and 68 deletions
  1. 13 0
      CMakeLists.txt
  2. 0 19
      Makefile
  3. 13 7
      autofs.c
  4. 20 16
      fs.c
  5. 1 0
      include/fs.h
  6. 1 0
      led.c
  7. 2 0
      log.c
  8. 4 1
      main.c
  9. 32 21
      mount.c
  10. 2 1
      signal.c
  11. 1 0
      sys.c
  12. 1 1
      timer.c
  13. 1 1
      uci.c
  14. 1 1
      ucix.c

+ 13 - 0
CMakeLists.txt

@@ -0,0 +1,13 @@
+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
+)

+ 0 - 19
Makefile

@@ -1,19 +0,0 @@
-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 $@

+ 13 - 7
autofs.c

@@ -19,7 +19,7 @@
 #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"
@@ -28,6 +28,7 @@
 #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 */
@@ -36,7 +37,7 @@ dev_t dev;
 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/");
 }
@@ -113,7 +114,7 @@ static int autofs_process_request(const struct autofs_v5_packet *pkt)
 	return 0;
 }
 
-void expire_proc(void)
+static void expire_proc(void)
 {
 	struct autofs_packet_expire pkt;
 	while(ioctl(fdin, AUTOFS_IOC_EXPIRE, &pkt) == 0)
@@ -140,6 +141,7 @@ static int fullread(void *ptr, size_t len)
 
 static int autofs_in(union autofs_v5_packet_union *pkt)
 {
+	int res;
 	struct pollfd fds[1];
 
 	fds[0].fd = fdout;
@@ -147,15 +149,19 @@ static int autofs_in(union autofs_v5_packet_union *pkt)
 
 	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));
+		}
 	}
 }
 
@@ -170,14 +176,14 @@ pid_t autofs_safe_fork(void)
 	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;

+ 20 - 16
fs.c

@@ -2,7 +2,6 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
-#include <error.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -11,7 +10,7 @@
 
 typedef int (*dfunc)(int);
 
-unsigned short
+static unsigned short
 get_le_short(void *from)
 {
 	unsigned char *p = from;
@@ -19,7 +18,7 @@ get_le_short(void *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) +
@@ -28,14 +27,14 @@ unsigned int get_le_long(void *from)
 		(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) +
@@ -44,7 +43,7 @@ unsigned int get_be_long(void *from)
 		(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;
@@ -54,7 +53,7 @@ int get_buffer(int fd, unsigned char *b, int offset, int len)
 }
 
 #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);
@@ -69,7 +68,7 @@ out:
 
 #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);
@@ -83,7 +82,7 @@ out:
 }
 
 #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);
@@ -91,9 +90,14 @@ int detect_ext23(int fd)
 		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;
 	}
@@ -103,7 +107,7 @@ out:
 }
 
 #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);
@@ -121,7 +125,7 @@ out:
 
 #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;
@@ -144,7 +148,7 @@ out:
 }
 
 #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);
@@ -158,7 +162,7 @@ out:
 }
 
 #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);

+ 1 - 0
include/fs.h

@@ -7,5 +7,6 @@
 #define EFI			7
 #define NTFS		8
 #define EXTENDED	9
+#define EXT4		10
 
 int detect_fs(char *device);

+ 1 - 0
led.c

@@ -25,6 +25,7 @@
 #include "include/ucix.h"
 #include "include/log.h"
 #include "include/timer.h"
+#include "include/led.h"
 
 char usbled[16];
 

+ 2 - 0
log.c

@@ -2,6 +2,8 @@
 #include <syslog.h>
 #include <stdarg.h>
 
+#include "include/log.h"
+
 extern int daemonize;
 
 void log_start(void)

+ 4 - 1
main.c

@@ -1,4 +1,5 @@
 #include <stdio.h>
+#include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
@@ -13,7 +14,9 @@ int daemonize = 0;
 
 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");

+ 32 - 21
mount.c

@@ -16,6 +16,7 @@
 #include <glob.h>
 #include <libgen.h>
 #include <poll.h>
+#include <dirent.h>
 
 #include "include/log.h"
 #include "include/list.h"
@@ -25,6 +26,7 @@
 #include "include/autofs.h"
 #include "include/ucix.h"
 #include "include/fs.h"
+#include "include/mount.h"
 
 int mount_new(char *path, char *dev);
 
@@ -53,7 +55,10 @@ char *fs_names[] = {
 	"EXT3",
 	"FAT",
 	"HFSPLUS",
-	"NTFS"
+	"",
+	"NTFS",
+	"",
+	"EXT4"
 };
 
 #define MAX_MOUNTED		32
@@ -63,7 +68,7 @@ char mounted[MAX_MOUNTED][3][MAX_MOUNT_NAME];
 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;
@@ -93,7 +98,7 @@ void mount_dump_uci_state(void)
 		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]);
@@ -110,7 +115,7 @@ void mount_dump_uci_state(void)
 	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)
@@ -126,12 +131,12 @@ struct mount* mount_find(char *name, char *dev)
 	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);
@@ -147,7 +152,7 @@ void mount_add_list(char *name, char *dev, char *serial,
 	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);
@@ -157,7 +162,7 @@ void mount_add_list(char *name, char *dev, char *serial,
 	}
 }
 
-int mount_check_disc(char *disc)
+static int mount_check_disc(char *disc)
 {
 	FILE *fp = fopen("/proc/mounts", "r");
 	char tmp[256];
@@ -187,7 +192,7 @@ int mount_check_disc(char *disc)
 	return avail;
 }
 
-int mount_wait_for_disc(char *disc)
+static int mount_wait_for_disc(char *disc)
 {
 	int i = 10;
 	while(i--)
@@ -226,6 +231,11 @@ int mount_new(char *path, char *dev)
 			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);
@@ -280,19 +290,19 @@ int mount_remove(char *path, char *dev)
 	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];
@@ -371,6 +381,7 @@ char* mount_get_serial(char *dev)
 	} 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];
@@ -380,14 +391,14 @@ char* mount_get_serial(char *dev)
 		{
 			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)
@@ -506,7 +517,7 @@ void mount_dev_add(char *dev)
 	}
 }
 
-void mount_dev_del(char *dev)
+static void mount_dev_del(char *dev)
 {
 	struct mount *mount = mount_find(0, dev);
 	char tmp[256];
@@ -551,7 +562,7 @@ char* is_mounted(char *block, char *path)
 	return 0;
 }
 
-void mount_check_mount_list(void)
+static void mount_check_mount_list(void)
 {
 	FILE *fp = fopen("/proc/mounts", "r");
 	char tmp[256];
@@ -599,8 +610,8 @@ void mount_check_mount_list(void)
 	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;
@@ -610,7 +621,7 @@ int dir_filter2(const struct dirent *a)
 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++)
@@ -621,7 +632,7 @@ int check_block(char *b)
 	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);
@@ -696,7 +707,7 @@ void mount_enum_drives(void)
 		mount_dev_add(block[i]);
 }
 
-void mount_check_enum(void)
+static void mount_check_enum(void)
 {
 	waitpid(-1, 0, WNOHANG);
 	mount_enum_drives();

+ 2 - 1
signal.c

@@ -5,10 +5,11 @@
 #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)

+ 1 - 0
sys.c

@@ -4,6 +4,7 @@
 #include <stdlib.h>
 
 #include "include/log.h"
+#include "include/sys.h"
 
 int system_printf(char *fmt, ...)
 {

+ 1 - 1
timer.c

@@ -34,7 +34,7 @@ void timer_add(timercb_t timercb, int timeout)
 	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)

+ 1 - 1
uci.c

@@ -28,7 +28,7 @@ struct uci_package *p = NULL;
 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);

+ 1 - 1
ucix.c

@@ -18,7 +18,7 @@ static inline int ucix_get_ptr(struct uci_context *ctx, const char *p, const cha
 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);