Moved helpers to a separate file

Signed-off-by: Claudio Matsuoka <cmatsuoka@gmail.com>
This commit is contained in:
Claudio Matsuoka
2008-02-16 22:03:16 -02:00
parent a31bd547d6
commit dbb31994a0
6 changed files with 123 additions and 127 deletions
+1
View File
@@ -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
+11 -2
View File
@@ -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
+3 -60
View File
@@ -42,6 +42,7 @@ THE SOFTWARE.
#include <netinet/in.h>
#include <arpa/inet.h>
#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
+5 -65
View File
@@ -55,10 +55,8 @@ Changelog from the original Eeepc fastinit:
#include <sys/reboot.h>
#include <sys/wait.h>
#include <linux/fs.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#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
+95
View File
@@ -0,0 +1,95 @@
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#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);
}
}
+8
View File
@@ -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