mirror of
https://github.com/troglobit/finit.git
synced 2026-10-12 10:12:32 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3e29872db7 | ||
|
|
944ba857e6 | ||
|
|
0c72f960f1 | ||
|
|
1fc2d9596a | ||
|
|
cdbc2c536d | ||
|
|
d17c3160bd | ||
|
|
dbb31994a0 | ||
|
|
a31bd547d6 | ||
|
|
57d982699b | ||
|
|
e1f57e78c9 | ||
|
|
3657d539c1 | ||
|
|
9a2b6890e5 | ||
|
|
bbe281db55 | ||
|
|
ef3071b957 | ||
|
|
1d4d7df3cc | ||
|
|
16ef455fb1 | ||
|
|
8c3580d352 | ||
|
|
412de97ad8 | ||
|
|
7af37f1fb7 |
@@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
0.2 ()
|
||||||
|
- replace system("touch") with touch() in finit-mod (Metalshark)
|
||||||
|
- disable NO_HCTOSYS by default to match stock Eeepc kernel
|
||||||
|
- drop system("rm -f") to clean /tmp, its a fresh mounted tmpfs
|
||||||
|
- write ACPI sleep state to /sys/power/state instead of
|
||||||
|
/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
|
||||||
|
- replace system("echo;cat") to draw shutdown splash with C calls
|
||||||
|
|
||||||
|
0.1 (20081214)
|
||||||
|
- initial release
|
||||||
@@ -3,8 +3,10 @@ CFLAGS = -Os -Wall -DVERSION=\"$(VERSION)\" -DWHOAMI=\"`whoami`@`hostname`\"
|
|||||||
LD = gcc
|
LD = gcc
|
||||||
LDFLAGS =
|
LDFLAGS =
|
||||||
LIBS =
|
LIBS =
|
||||||
VERSION = 0.1
|
VERSION = 0.2
|
||||||
PKG = finit-$(VERSION)
|
PKG = finit-$(VERSION)
|
||||||
|
DFILES = Makefile README finit.c finit-mod.c finit-alt.c helpers.c helpers.h
|
||||||
|
|
||||||
|
|
||||||
#### Configurable parameters
|
#### Configurable parameters
|
||||||
|
|
||||||
@@ -17,7 +19,7 @@ PKG = finit-$(VERSION)
|
|||||||
CFLAGS += -march=pentium-m
|
CFLAGS += -march=pentium-m
|
||||||
|
|
||||||
# Omit the first /sbin/hwclock if CONFIG_RTC_HCTOSYS is enabled in the kernel
|
# Omit the first /sbin/hwclock if CONFIG_RTC_HCTOSYS is enabled in the kernel
|
||||||
CFLAGS += -DNO_HCTOSYS
|
#CFLAGS += -DNO_HCTOSYS
|
||||||
|
|
||||||
# Append –directisa to /sbin/hwclock lines if the user has disabled
|
# Append –directisa to /sbin/hwclock lines if the user has disabled
|
||||||
# CONFIG_GENRTC and enabled CONFIG_RTC but not enabled CONFIG_HPET_TIMER
|
# CONFIG_GENRTC and enabled CONFIG_RTC but not enabled CONFIG_HPET_TIMER
|
||||||
@@ -31,7 +33,15 @@ CFLAGS += -DNO_HCTOSYS
|
|||||||
|
|
||||||
all: finit finit-mod finit-mdv
|
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)
|
$(LD) $(LDFLAGS) -o $@ $+ $(LIBS)
|
||||||
strip $@
|
strip $@
|
||||||
|
|
||||||
@@ -48,7 +58,7 @@ finit-mod.o: finit-mod.c Makefile
|
|||||||
dist:
|
dist:
|
||||||
rm -Rf $(PKG)
|
rm -Rf $(PKG)
|
||||||
mkdir $(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
|
tar cf - $(PKG) | gzip -c > $(PKG).tar.gz
|
||||||
rm -Rf $(PKG)
|
rm -Rf $(PKG)
|
||||||
ls -l $(PKG).tar.gz
|
ls -l $(PKG).tar.gz
|
||||||
|
|||||||
@@ -48,3 +48,28 @@ umount vi
|
|||||||
|
|
||||||
console fb null ram0 sda1 sda2 sda3 sr0 tty0
|
console fb null ram0 sda1 sda2 sda3 sr0 tty0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Using finit with alternative systems
|
||||||
|
------------------------------------
|
||||||
|
|
||||||
|
Currently tested only with Mandriva 2008. Proceed with caution, there are
|
||||||
|
problems related to udev and pam.
|
||||||
|
|
||||||
|
1 - Add init=/sbin/finit-mdv to the kernel boot line
|
||||||
|
|
||||||
|
2 - Provide a suitable /usr/sbin/services.sh, this is the one I use in my
|
||||||
|
test system:
|
||||||
|
|
||||||
|
mkdir /var/lock/subsys
|
||||||
|
chmod 1777 /var/lock/subsys
|
||||||
|
/etc/init.d/network start
|
||||||
|
dhclient eth0
|
||||||
|
/sbin/start_udev&
|
||||||
|
/etc/init.d/portmap start
|
||||||
|
/etc/init.d/nfs-common start
|
||||||
|
|
||||||
|
3 - Provide /etc/hostname if you don't have it already
|
||||||
|
|
||||||
|
4 - Set the pointer device in the X server configuration to /dev/input/mice
|
||||||
|
|
||||||
|
|||||||
+14
-34
@@ -37,8 +37,8 @@ THE SOFTWARE.
|
|||||||
#include <sys/reboot.h>
|
#include <sys/reboot.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <linux/fs.h>
|
#include <linux/fs.h>
|
||||||
#include <linux/limits.h>
|
|
||||||
|
|
||||||
|
#include "helpers.h"
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
#define debug(x...) do { \
|
#define debug(x...) do { \
|
||||||
@@ -90,23 +90,6 @@ THE SOFTWARE.
|
|||||||
#define chardev(x,m,maj,min) mknod((x), S_IFCHR|(m), makedev((maj),(min)))
|
#define chardev(x,m,maj,min) mknod((x), S_IFCHR|(m), makedev((maj),(min)))
|
||||||
|
|
||||||
|
|
||||||
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 shutdown(int);
|
void shutdown(int);
|
||||||
void signal_handler(int);
|
void signal_handler(int);
|
||||||
void chld_handler(int);
|
void chld_handler(int);
|
||||||
@@ -137,10 +120,10 @@ int main()
|
|||||||
for (i = 1; i < NSIG; i++)
|
for (i = 1; i < NSIG; i++)
|
||||||
SETSIG(sa, i, SIG_IGN, SA_RESTART);
|
SETSIG(sa, i, SIG_IGN, SA_RESTART);
|
||||||
|
|
||||||
SETSIG(sa, SIGINT, shutdown, 0);
|
SETSIG(sa, SIGINT, shutdown, 0);
|
||||||
SETSIG(sa, SIGPWR, signal_handler, 0);
|
SETSIG(sa, SIGPWR, signal_handler, 0);
|
||||||
SETSIG(sa, SIGUSR1, shutdown, 0);
|
SETSIG(sa, SIGUSR1, shutdown, 0);
|
||||||
SETSIG(sa, SIGUSR2, shutdown, 0);
|
SETSIG(sa, SIGUSR2, shutdown, 0);
|
||||||
SETSIG(sa, SIGTERM, signal_handler, 0);
|
SETSIG(sa, SIGTERM, signal_handler, 0);
|
||||||
SETSIG(sa, SIGALRM, signal_handler, 0);
|
SETSIG(sa, SIGALRM, signal_handler, 0);
|
||||||
SETSIG(sa, SIGHUP, signal_handler, 0);
|
SETSIG(sa, SIGHUP, signal_handler, 0);
|
||||||
@@ -259,22 +242,19 @@ int main()
|
|||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
system("/sbin/ifconfig lo 127.0.0.1 netmask 255.0.0.0 up > /dev/null");
|
ifconfig("lo", "127.0.0.1", "255.0.0.0", 1);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set random seed
|
* Set random seed
|
||||||
*/
|
*/
|
||||||
system("/bin/cat " RANDOMSEED " >/dev/urandom 2> /dev/null");
|
copyfile(RANDOMSEED, "/dev/urandom", 0);
|
||||||
unlink(RANDOMSEED);
|
unlink(RANDOMSEED);
|
||||||
|
|
||||||
umask(077);
|
umask(077);
|
||||||
system("/bin/dd if=/dev/urandom of=" RANDOMSEED "bs=4096 count=1 "
|
copyfile("/dev/urandom", RANDOMSEED, 4096);
|
||||||
">/dev/null 2>&1");
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Misc setup
|
* Misc setup
|
||||||
*/
|
*/
|
||||||
system("/bin/rm -rf /tmp/* /tmp/.* 2>/dev/null");
|
|
||||||
unlink("/var/run/.clean");
|
unlink("/var/run/.clean");
|
||||||
unlink("/var/lock/.clean");
|
unlink("/var/lock/.clean");
|
||||||
umask(0);
|
umask(0);
|
||||||
@@ -332,9 +312,9 @@ int main()
|
|||||||
|
|
||||||
/* parent process */
|
/* parent process */
|
||||||
|
|
||||||
system(GETTY " &");
|
system(GETTY "&");
|
||||||
sleep(1);
|
sleep(1);
|
||||||
system("/usr/sbin/services.sh &> /dev/null &");
|
system("/usr/sbin/services.sh &>/dev/null&");
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
sigemptyset(&nmask);
|
sigemptyset(&nmask);
|
||||||
@@ -355,9 +335,9 @@ void shutdown(int sig)
|
|||||||
|
|
||||||
kill(-1, SIGTERM);
|
kill(-1, SIGTERM);
|
||||||
|
|
||||||
system("/bin/echo -e \"\033[?25l\033[30;40m\"; "
|
write(1, "\033[?25l\033[30;40m", 14);
|
||||||
"/bin/cp /boot/shutdown.fb /dev/fb/0");
|
copyfile("/boot/shutdown.fb", "/dev/fb/0", 0);
|
||||||
sleep(1);
|
sleep(2);
|
||||||
|
|
||||||
system("/usr/sbin/alsactl store > /dev/null 2>&1");
|
system("/usr/sbin/alsactl store > /dev/null 2>&1");
|
||||||
system("/sbin/hwclock --systohc --localtime" HWCLOCK_DIRECTISA);
|
system("/sbin/hwclock --systohc --localtime" HWCLOCK_DIRECTISA);
|
||||||
@@ -374,7 +354,7 @@ void shutdown(int sig)
|
|||||||
if (sig == SIGINT || sig == SIGUSR1)
|
if (sig == SIGINT || sig == SIGUSR1)
|
||||||
reboot(RB_AUTOBOOT);
|
reboot(RB_AUTOBOOT);
|
||||||
|
|
||||||
if ((fd = open("/proc/acpi/sleep", O_WRONLY)) >= 0) {
|
if ((fd = open("/sys/power/state", O_WRONLY)) >= 0) {
|
||||||
write(fd, "5", 1);
|
write(fd, "5", 1);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
@@ -396,7 +376,7 @@ void chld_handler(int sig)
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We got a signal (PWR TERM ALRM HUP STOP CONT)
|
* We got a signal (PWR TERM ALRM HUP CONT)
|
||||||
*/
|
*/
|
||||||
void signal_handler(int sig)
|
void signal_handler(int sig)
|
||||||
{
|
{
|
||||||
|
|||||||
+22
-37
@@ -26,13 +26,18 @@ THE SOFTWARE.
|
|||||||
|
|
||||||
Changelog from the original Eeepc fastinit:
|
Changelog from the original Eeepc fastinit:
|
||||||
|
|
||||||
- Use mknod() instead of close(creat()) call to create empty file
|
- Use mknod() instead of close(open()) call to create empty file
|
||||||
- Fix random seed initialization
|
- Fix random seed initialization
|
||||||
- Don't try to handle signal 0, SIGKILL or SIGSTOP
|
- Don't try to handle signal 0, SIGKILL or SIGSTOP
|
||||||
- Implement makepath() to avoid system("mkdir -p")
|
- Implement makepath() to avoid system("mkdir -p")
|
||||||
- Omit first hwclock if user has CONFIG_RTC_HCTOSYS in kernel (by Metalshark)
|
- Omit first hwclock if user has CONFIG_RTC_HCTOSYS in kernel (by Metalshark)
|
||||||
- Add –directisa to hwclock if user has disabled CONFIG_GENRTC and enabled
|
- Add –directisa to hwclock if user has disabled CONFIG_GENRTC and enabled
|
||||||
CONFIG_RTC but not CONFIG_HPET_TIMER and CONFIG_HPET_RTC_IRQ (by Metalshark)
|
CONFIG_RTC but not CONFIG_HPET_TIMER and CONFIG_HPET_RTC_IRQ (by Metalshark)
|
||||||
|
- Drop system() call to clean /tmp, it's a fresh mounted tmpfs
|
||||||
|
- Change /proc/acpi/sleep to /sys/power/state (by Metalshark)
|
||||||
|
- Set loopback interface using direct calls instead of system("ifconfig")
|
||||||
|
- Copy 4096 data block in C instead of system("cat") or system("dd")
|
||||||
|
- Draw shutdown splash screen using C calls instead of system("echo;cat")
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -52,6 +57,8 @@ Changelog from the original Eeepc fastinit:
|
|||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <linux/fs.h>
|
#include <linux/fs.h>
|
||||||
|
|
||||||
|
#include "helpers.h"
|
||||||
|
|
||||||
#ifdef DIRECTISA
|
#ifdef DIRECTISA
|
||||||
#define HWCLOCK_DIRECTISA " --directisa"
|
#define HWCLOCK_DIRECTISA " --directisa"
|
||||||
#else
|
#else
|
||||||
@@ -72,23 +79,6 @@ Changelog from the original Eeepc fastinit:
|
|||||||
#define touch(x) mknod((x), S_IFREG|0644, 0)
|
#define touch(x) mknod((x), S_IFREG|0644, 0)
|
||||||
|
|
||||||
|
|
||||||
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 shutdown(int);
|
void shutdown(int);
|
||||||
void signal_handler(int);
|
void signal_handler(int);
|
||||||
void chld_handler(int);
|
void chld_handler(int);
|
||||||
@@ -119,10 +109,10 @@ int main()
|
|||||||
for (i = 1; i < NSIG; i++)
|
for (i = 1; i < NSIG; i++)
|
||||||
SETSIG(sa, i, SIG_IGN, SA_RESTART);
|
SETSIG(sa, i, SIG_IGN, SA_RESTART);
|
||||||
|
|
||||||
SETSIG(sa, SIGINT, shutdown, 0);
|
SETSIG(sa, SIGINT, shutdown, 0);
|
||||||
SETSIG(sa, SIGPWR, signal_handler, 0);
|
SETSIG(sa, SIGPWR, signal_handler, 0);
|
||||||
SETSIG(sa, SIGUSR1, shutdown, 0);
|
SETSIG(sa, SIGUSR1, shutdown, 0);
|
||||||
SETSIG(sa, SIGUSR2, shutdown, 0);
|
SETSIG(sa, SIGUSR2, shutdown, 0);
|
||||||
SETSIG(sa, SIGTERM, signal_handler, 0);
|
SETSIG(sa, SIGTERM, signal_handler, 0);
|
||||||
SETSIG(sa, SIGALRM, signal_handler, 0);
|
SETSIG(sa, SIGALRM, signal_handler, 0);
|
||||||
SETSIG(sa, SIGHUP, signal_handler, 0);
|
SETSIG(sa, SIGHUP, signal_handler, 0);
|
||||||
@@ -209,23 +199,19 @@ int main()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
system("/sbin/ifconfig lo 127.0.0.1 netmask 255.0.0.0 up > /dev/null");
|
ifconfig("lo", "127.0.0.1", "255.0.0.0", 1);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Set random seed
|
* Set random seed
|
||||||
*/
|
*/
|
||||||
system("/bin/cat /var/lib/urandom/random-seed >/dev/urandom "
|
copyfile("/var/lib/urandom/random-seed", "/dev/urandom", 0);
|
||||||
"2> /dev/null");
|
|
||||||
unlink("/var/lib/urandom/random-seed");
|
unlink("/var/lib/urandom/random-seed");
|
||||||
|
|
||||||
umask(077);
|
umask(077);
|
||||||
system("/bin/dd if=/dev/urandom of=/var/lib/urandom/random-seed "
|
copyfile("/dev/urandom", "/var/lib/urandom/random-seed", 4096);
|
||||||
"bs=4096 count=1 >/dev/null 2>&1");
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Misc setup
|
* Misc setup
|
||||||
*/
|
*/
|
||||||
system("/bin/rm -rf /tmp/* /tmp/.* 2>/dev/null");
|
|
||||||
unlink("/var/run/.clean");
|
unlink("/var/run/.clean");
|
||||||
unlink("/var/lock/.clean");
|
unlink("/var/lock/.clean");
|
||||||
umask(0);
|
umask(0);
|
||||||
@@ -269,9 +255,9 @@ int main()
|
|||||||
|
|
||||||
/* parent process */
|
/* parent process */
|
||||||
|
|
||||||
system("/sbin/getty 38400 tty3 &");
|
system("/sbin/getty 38400 tty3&");
|
||||||
sleep(1);
|
sleep(1);
|
||||||
system("/usr/sbin/services.sh &> /dev/null &");
|
system("/usr/sbin/services.sh &>/dev/null&");
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
sigemptyset(&nmask);
|
sigemptyset(&nmask);
|
||||||
@@ -287,14 +273,13 @@ void shutdown(int sig)
|
|||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
system("/usr/bin/touch /tmp/shutdown");
|
touch("/tmp/shutdown");
|
||||||
|
|
||||||
kill(-1, SIGTERM);
|
kill(-1, SIGTERM);
|
||||||
|
|
||||||
system("/bin/echo -e \"\033[?25l\033[30;40m\"; "
|
write(1, "\033[?25l\033[30;40m", 14);
|
||||||
"/bin/cp /boot/shutdown.fb /dev/fb/0");
|
copyfile("/boot/shutdown.fb", "/dev/fb/0", 0);
|
||||||
sleep(1);
|
sleep(2);
|
||||||
sleep(1);
|
|
||||||
|
|
||||||
system("/usr/sbin/alsactl store > /dev/null 2>&1");
|
system("/usr/sbin/alsactl store > /dev/null 2>&1");
|
||||||
system("/sbin/hwclock --systohc --localtime" HWCLOCK_DIRECTISA);
|
system("/sbin/hwclock --systohc --localtime" HWCLOCK_DIRECTISA);
|
||||||
@@ -311,7 +296,7 @@ void shutdown(int sig)
|
|||||||
if (sig == SIGINT || sig == SIGUSR1)
|
if (sig == SIGINT || sig == SIGUSR1)
|
||||||
reboot(RB_AUTOBOOT);
|
reboot(RB_AUTOBOOT);
|
||||||
|
|
||||||
if ((fd = open("/proc/acpi/sleep", O_WRONLY)) >= 0) {
|
if ((fd = open("/sys/power/state", O_WRONLY)) >= 0) {
|
||||||
write(fd, "5", 1);
|
write(fd, "5", 1);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
@@ -333,7 +318,7 @@ void chld_handler(int sig)
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We got a signal (PWR TERM ALRM HUP STOP CONT)
|
* We got a signal (PWR TERM ALRM HUP CONT)
|
||||||
*/
|
*/
|
||||||
void signal_handler(int sig)
|
void signal_handler(int sig)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -164,7 +164,9 @@ int main()
|
|||||||
/*
|
/*
|
||||||
* Misc setup
|
* Misc setup
|
||||||
*/
|
*/
|
||||||
|
/* not really needed, /tmp is a fresh mounted tmpfs */
|
||||||
system("/bin/rm -rf /tmp/* /tmp/.* 2>/dev/null");
|
system("/bin/rm -rf /tmp/* /tmp/.* 2>/dev/null");
|
||||||
|
|
||||||
unlink("/var/run/.clean");
|
unlink("/var/run/.clean");
|
||||||
unlink("/var/lock/.clean");
|
unlink("/var/lock/.clean");
|
||||||
umask(0);
|
umask(0);
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
|
||||||
|
#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;
|
||||||
|
|
||||||
|
if (up) {
|
||||||
|
inet_aton(inet, &a->sin_addr);
|
||||||
|
ioctl(sock, SIOCSIFADDR, &ifr);
|
||||||
|
inet_aton(mask, &a->sin_addr);
|
||||||
|
ioctl(sock, SIOCSIFNETMASK, &ifr);
|
||||||
|
}
|
||||||
|
|
||||||
|
ioctl(sock, SIOCGIFFLAGS, &ifr);
|
||||||
|
|
||||||
|
if (up)
|
||||||
|
ifr.ifr_flags |= IFF_UP;
|
||||||
|
else
|
||||||
|
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;
|
||||||
|
|
||||||
|
/* Size == 0 means copy entire file */
|
||||||
|
if (size == 0)
|
||||||
|
size = INT_MAX;
|
||||||
|
|
||||||
|
if ((s = open(src, O_RDONLY)) >= 0) {
|
||||||
|
if ((d = open(dst, O_WRONLY | O_CREAT, 0644)) >= 0) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user