From dbb31994a08788db438284b10907214301534ac2 Mon Sep 17 00:00:00 2001 From: Claudio Matsuoka Date: Sat, 16 Feb 2008 22:03:16 -0200 Subject: [PATCH] Moved helpers to a separate file Signed-off-by: Claudio Matsuoka --- Changelog | 1 + Makefile | 13 ++++++-- finit-alt.c | 63 ++--------------------------------- finit-mod.c | 70 +++------------------------------------ helpers.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++ helpers.h | 8 +++++ 6 files changed, 123 insertions(+), 127 deletions(-) create mode 100644 helpers.c create mode 100644 helpers.h diff --git a/Changelog b/Changelog index 334ac494..3273e439 100644 --- a/Changelog +++ b/Changelog @@ -7,6 +7,7 @@ /proc/acpi/sleep (Metalshark) - use direct calls to set loopback instead of system("ifconfig") - replace system("cat") and system("dd") with C implementation + - moved finit-mod and finit-alt helpers to helpers.c 0.1 (20081214) - initial release diff --git a/Makefile b/Makefile index afa22aa1..6c459203 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,7 @@ LDFLAGS = LIBS = VERSION = 0.2 PKG = finit-$(VERSION) +DFILES = Makefile README finit.c finit-mod.c finit-alt.c helpers.c helpers.h #### Configurable parameters @@ -32,7 +33,15 @@ CFLAGS += -march=pentium-m all: finit finit-mod finit-mdv -%: %.o +finit: finit.o + $(LD) $(LDFLAGS) -o $@ $+ $(LIBS) + strip $@ + +finit-mod: finit-mod.o helpers.o + $(LD) $(LDFLAGS) -o $@ $+ $(LIBS) + strip $@ + +finit-mdv: finit-mdv.o helpers.o $(LD) $(LDFLAGS) -o $@ $+ $(LIBS) strip $@ @@ -49,7 +58,7 @@ finit-mod.o: finit-mod.c Makefile dist: rm -Rf $(PKG) mkdir $(PKG) - cp Makefile README finit.c finit-mod.c finit-alt.c $(PKG) + cp Makefile $(DFILES) $(PKG) tar cf - $(PKG) | gzip -c > $(PKG).tar.gz rm -Rf $(PKG) ls -l $(PKG).tar.gz diff --git a/finit-alt.c b/finit-alt.c index 66508206..dc17ee00 100644 --- a/finit-alt.c +++ b/finit-alt.c @@ -42,6 +42,7 @@ THE SOFTWARE. #include #include +#include "helpers.h" #ifdef DEBUG #define debug(x...) do { \ @@ -93,64 +94,6 @@ THE SOFTWARE. #define chardev(x,m,maj,min) mknod((x), S_IFCHR|(m), makedev((maj),(min))) -/* - * Helpers to replace system() calls - */ - -int makepath(char *p) -{ - char *x, path[PATH_MAX]; - int ret; - - x = path; - - do { - do { *x++ = *p++; } while (*p && *p != '/'); - ret = mkdir(path, 0777); - } while (*p && (*p != '/' || *(p + 1))); /* ignore trailing slash */ - - return ret; -} - -void ifconfig(char *name, char *inet, char *mask, int flags) -{ - struct ifreq ifr; - struct sockaddr_in *a = (struct sockaddr_in *)&(ifr.ifr_addr); - int sock; - - if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) - return; - - memset(&ifr, 0, sizeof (ifr)); - strncpy(ifr.ifr_name, name, IFNAMSIZ); - a->sin_family = AF_INET; - inet_aton(inet, &a->sin_addr); - ioctl(sock, SIOCSIFADDR, &ifr); - inet_aton(mask, &a->sin_addr); - ioctl(sock, SIOCSIFNETMASK, &ifr); - ioctl(sock, SIOCGIFFLAGS, &ifr); - ifr.ifr_flags |= flags; - ioctl(sock, SIOCSIFFLAGS, &ifr); - close(sock); -} - -void copy4k(char *src, char *dst) -{ - char buffer[4096]; - int s, d, n; - - if ((s = open(src, O_RDONLY)) >= 0) { - if ((d = open(dst, O_WRONLY | O_CREAT, 0644)) >= 0) { - if ((n = read(s, buffer, 4096)) > 0) - write(d, buffer, n); - close(d); - } - close(s); - } -} - - - void do_shutdown(int); void signal_handler(int); void chld_handler(int); @@ -308,10 +251,10 @@ int main() /* * Set random seed */ - copy4k(RANDOMSEED, "/dev/urandom"); + copyfile(RANDOMSEED, "/dev/urandom", 0); unlink(RANDOMSEED); umask(077); - copy4k("/dev/urandom", RANDOMSEED); + copyfile("/dev/urandom", RANDOMSEED, 4096); /* * Misc setup diff --git a/finit-mod.c b/finit-mod.c index e7ec25bb..c95b244e 100644 --- a/finit-mod.c +++ b/finit-mod.c @@ -55,10 +55,8 @@ Changelog from the original Eeepc fastinit: #include #include #include -#include -#include -#include -#include + +#include "helpers.h" #ifdef DIRECTISA #define HWCLOCK_DIRECTISA " --directisa" @@ -80,64 +78,6 @@ Changelog from the original Eeepc fastinit: #define touch(x) mknod((x), S_IFREG|0644, 0) -/* - * Helpers to replace system() calls - */ - -int makepath(char *p) -{ - char *x, path[PATH_MAX]; - int ret; - - x = path; - - do { - do { *x++ = *p++; } while (*p && *p != '/'); - ret = mkdir(path, 0777); - } while (*p && (*p != '/' || *(p + 1))); /* ignore trailing slash */ - - return ret; -} - -void ifconfig(char *name, char *inet, char *mask, int flags) -{ - struct ifreq ifr; - struct sockaddr_in *a = (struct sockaddr_in *)&(ifr.ifr_addr); - int sock; - - if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) - return; - - memset(&ifr, 0, sizeof (ifr)); - strncpy(ifr.ifr_name, name, IFNAMSIZ); - a->sin_family = AF_INET; - inet_aton(inet, &a->sin_addr); - ioctl(sock, SIOCSIFADDR, &ifr); - inet_aton(mask, &a->sin_addr); - ioctl(sock, SIOCSIFNETMASK, &ifr); - ioctl(sock, SIOCGIFFLAGS, &ifr); - ifr.ifr_flags |= flags; - ioctl(sock, SIOCSIFFLAGS, &ifr); - close(sock); -} - -void copy4k(char *src, char *dst) -{ - char buffer[4096]; - int s, d, n; - - if ((s = open(src, O_RDONLY)) >= 0) { - if ((d = open(dst, O_WRONLY | O_CREAT, 0644)) >= 0) { - if ((n = read(s, buffer, 4096)) > 0) - write(d, buffer, n); - close(d); - } - close(s); - } -} - - - void do_shutdown(int); void signal_handler(int); void chld_handler(int); @@ -258,15 +198,15 @@ int main() } - ifconfig("lo", "127.0.0.1", "255.0.0.0", IFF_UP); + ifconfig("lo", "127.0.0.1", "255.0.0.0", 1); /* * Set random seed */ - copy4k("/var/lib/urandom/random-seed", "/dev/urandom"); + copyfile("/var/lib/urandom/random-seed", "/dev/urandom", 0); unlink("/var/lib/urandom/random-seed"); umask(077); - copy4k("/dev/urandom", "/var/lib/urandom/random-seed"); + copyfile("/dev/urandom", "/var/lib/urandom/random-seed", 4096); /* * Misc setup diff --git a/helpers.c b/helpers.c new file mode 100644 index 00000000..81b4d56d --- /dev/null +++ b/helpers.c @@ -0,0 +1,95 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "helpers.h" + +/* + * Helpers to replace system() calls + */ + +int makepath(char *p) +{ + char *x, path[PATH_MAX]; + int ret; + + x = path; + + do { + do { *x++ = *p++; } while (*p && *p != '/'); + ret = mkdir(path, 0777); + } while (*p && (*p != '/' || *(p + 1))); /* ignore trailing slash */ + + return ret; +} + +void ifconfig(char *name, char *inet, char *mask, int up) +{ + struct ifreq ifr; + struct sockaddr_in *a = (struct sockaddr_in *)&(ifr.ifr_addr); + int sock; + + if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) + return; + + memset(&ifr, 0, sizeof (ifr)); + strncpy(ifr.ifr_name, name, IFNAMSIZ); + a->sin_family = AF_INET; + inet_aton(inet, &a->sin_addr); + + if (up) { + ioctl(sock, SIOCSIFADDR, &ifr); + inet_aton(mask, &a->sin_addr); + ioctl(sock, SIOCSIFNETMASK, &ifr); + ioctl(sock, SIOCGIFFLAGS, &ifr); + ifr.ifr_flags |= IFF_UP; + ioctl(sock, SIOCSIFFLAGS, &ifr); + } else { + ioctl(sock, SIOCGIFFLAGS, &ifr); + ifr.ifr_flags &= ~IFF_UP; + ioctl(sock, SIOCSIFFLAGS, &ifr); + } + + close(sock); +} + +#define BUF_SIZE 4096 + +void copyfile(char *src, char *dst, int size) +{ + char buffer[BUF_SIZE]; + int s, d, n; + + if ((s = open(src, O_RDONLY)) >= 0) { + if ((d = open(dst, O_WRONLY | O_CREAT, 0644)) >= 0) { + + /* Size <= 0 means copy entire file */ + if (size <= 0) { + do { + if ((n = read(s, buffer, BUF_SIZE)) > 0) + write(d, buffer, n); + } while (n == BUF_SIZE); + } else { + do { + int csize = size > BUF_SIZE ? + BUF_SIZE : size; + + if ((n = read(s, buffer, csize)) > 0) + write(d, buffer, n); + size -= csize; + } while (size > 0 && n == BUF_SIZE); + } + close(d); + } + close(s); + } +} + diff --git a/helpers.h b/helpers.h new file mode 100644 index 00000000..cee06ae0 --- /dev/null +++ b/helpers.h @@ -0,0 +1,8 @@ +#ifndef __FINIT_HELPERS_H +#define __FINIT_HELPERS_H + +int makepath (char *); +void ifconfig (char *, char *, char *, int); +void copyfile (char *, char *, int); + +#endif