Lines 771-776
Link Here
|
771 |
return 0; |
771 |
return 0; |
772 |
} |
772 |
} |
773 |
|
773 |
|
|
|
774 |
int doRm(const char *path, int recursive); |
775 |
|
774 |
#define MAX_INIT_ARGS 32 |
776 |
#define MAX_INIT_ARGS 32 |
775 |
/* 2.6 magic not-pivot-root but kind of similar stuff. |
777 |
/* 2.6 magic not-pivot-root but kind of similar stuff. |
776 |
* This is based on code from klibc/utils/run_init.c |
778 |
* This is based on code from klibc/utils/run_init.c |
Lines 807-812
Link Here
|
807 |
if (init == NULL) |
809 |
if (init == NULL) |
808 |
cmdline = getKernelCmdLine(); |
810 |
cmdline = getKernelCmdLine(); |
809 |
|
811 |
|
|
|
812 |
doRm("/dev", 1); |
813 |
umount("/proc"); |
814 |
rmdir("/proc"); |
815 |
umount("/sys"); |
816 |
rmdir("/sys"); |
817 |
|
810 |
if (mount(".", "/", NULL, MS_MOVE, NULL)) { |
818 |
if (mount(".", "/", NULL, MS_MOVE, NULL)) { |
811 |
printf("switchroot: mount failed: %d\n", errno); |
819 |
printf("switchroot: mount failed: %d\n", errno); |
812 |
return 1; |
820 |
return 1; |
Lines 1556-1561
Link Here
|
1556 |
return 0; |
1564 |
return 0; |
1557 |
} |
1565 |
} |
1558 |
|
1566 |
|
|
|
1567 |
int doRm(const char *path, int recursive) { |
1568 |
struct stat st; |
1569 |
int result = 0; |
1570 |
stat(path, &st); |
1571 |
if ( S_ISDIR(st.st_mode) ) { |
1572 |
if (recursive) { |
1573 |
DIR *dir = opendir(path); |
1574 |
if (dir) { |
1575 |
size_t np_size = 0; |
1576 |
char * new_path = NULL; |
1577 |
struct dirent *de; |
1578 |
while( (de = readdir(dir)) ) { |
1579 |
if ( strcmp(de->d_name, "..") && strcmp(de->d_name, ".") ) { // do not delete ".." and "." entries |
1580 |
const size_t new_size = strlen(de->d_name) + strlen(path) + 2; |
1581 |
if (new_size > np_size) { |
1582 |
np_size = new_size; |
1583 |
new_path = (char*)realloc(new_path, np_size); |
1584 |
} |
1585 |
strncpy(new_path, path, np_size); |
1586 |
strcat(new_path, "/"); |
1587 |
strcat(new_path, de->d_name); |
1588 |
doRm(new_path, recursive); |
1589 |
} |
1590 |
} |
1591 |
free(new_path); |
1592 |
closedir(dir); |
1593 |
} |
1594 |
} |
1595 |
result = rmdir(path); |
1596 |
} else |
1597 |
result = unlink(path); |
1598 |
|
1599 |
if (result != 0) |
1600 |
printf("rm: unable to remove \"%s\": %d\n", path, errno); |
1601 |
return result; |
1602 |
} |
1603 |
|
1604 |
int rmCommand(char * cmd, char * end) { |
1605 |
int recursive = 0; |
1606 |
char *arg; |
1607 |
while( (cmd = getArg(cmd, end, &arg)) ) { |
1608 |
if ( !strcmp("-r", arg) ) |
1609 |
recursive = 1; |
1610 |
else if ( !strcmp("-n", arg) ) |
1611 |
recursive = 0; |
1612 |
else |
1613 |
doRm(arg, recursive); |
1614 |
} |
1615 |
return 0; |
1616 |
} |
1617 |
|
1618 |
int rmdirCommand(char * cmd, char * end) { |
1619 |
char *path; |
1620 |
int result = -1; |
1621 |
while( (cmd = getArg(cmd, end, &path)) ) { |
1622 |
if (result < 0) |
1623 |
result = 1; |
1624 |
if ( rmdir(path) ) |
1625 |
printf("rmdir: unable to delete \"%s\": %d\n", path, errno); |
1626 |
else |
1627 |
result = 0; |
1628 |
} |
1629 |
return result; |
1630 |
} |
1631 |
|
1632 |
int unlinkCommand(char * cmd, char * end) { |
1633 |
char *path; |
1634 |
int result = -1; |
1635 |
while( (cmd = getArg(cmd, end, &path)) ) { |
1636 |
if (result < 0) |
1637 |
result = 1; |
1638 |
if ( unlink(path) ) |
1639 |
printf("unlink: unable to delete \"%s\": %d\n", path, errno); |
1640 |
else |
1641 |
result = 0; |
1642 |
} |
1643 |
return result; |
1644 |
} |
1645 |
|
1646 |
extern long init_module(void*, unsigned long, const char *); |
1647 |
|
1648 |
int insmodCommand(char * cmd, char * end) { |
1649 |
char *modname; |
1650 |
int fd; |
1651 |
int result = 1; |
1652 |
if ( !(cmd = getArg(cmd, end, &modname)) ) { |
1653 |
printf("insmod: path expected\n"); |
1654 |
return 1; |
1655 |
} |
1656 |
if ( (fd = open(modname, O_RDONLY) ) < 0) { |
1657 |
printf("insmod: unable to open \"%s\": %d\n", modname, errno); |
1658 |
return 1; |
1659 |
} |
1660 |
do { |
1661 |
void *buffer; |
1662 |
off_t modsize = lseek(fd, 0, SEEK_END); |
1663 |
if (modsize == (off_t)-1) { |
1664 |
printf("insmod: unable to determine module \"%s\" size: %d\n", modname, errno); |
1665 |
break; |
1666 |
} |
1667 |
|
1668 |
buffer = malloc(modsize); |
1669 |
if (buffer == NULL) { |
1670 |
printf("insmod: unable to allocate memory for \"%s\": %d\n", modname, errno); |
1671 |
break; |
1672 |
} |
1673 |
|
1674 |
lseek(fd, 0, SEEK_SET); |
1675 |
if ( read(fd, buffer, modsize) != modsize) { |
1676 |
printf("insmod: unable to read \"%s\": %d\n", modname, errno); |
1677 |
free(buffer); |
1678 |
break; |
1679 |
} |
1680 |
|
1681 |
*end = '\0'; |
1682 |
if (init_module(buffer, modsize, (cmd < end) ? cmd : "") ) { |
1683 |
printf("insmod: unable to insert module \"%s %s\": %d\n", modname, cmd, errno); |
1684 |
free(buffer); |
1685 |
break; |
1686 |
} |
1687 |
|
1688 |
result = 0; |
1689 |
} while(0); |
1690 |
|
1691 |
close(fd); |
1692 |
return result; |
1693 |
} |
1694 |
|
1559 |
int runStartup(int fd) { |
1695 |
int runStartup(int fd) { |
1560 |
char contents[32768]; |
1696 |
char contents[32768]; |
1561 |
int i; |
1697 |
int i; |
Lines 1645-1650
Link Here
|
1645 |
rc = readlinkCommand(chptr, end); |
1781 |
rc = readlinkCommand(chptr, end); |
1646 |
else if (!strncmp(start, "setquiet", MAX(8, chptr-start))) |
1782 |
else if (!strncmp(start, "setquiet", MAX(8, chptr-start))) |
1647 |
rc = setQuietCommand(chptr, end); |
1783 |
rc = setQuietCommand(chptr, end); |
|
|
1784 |
else if (!strncmp(start, "rm", MAX(2, chptr-start))) |
1785 |
rc = rmCommand(chptr, end); |
1786 |
else if (!strncmp(start, "insmod", MAX(6, chptr - start))) |
1787 |
rc = insmodCommand(chptr, end); |
1788 |
else if (!strncmp(start, "unlink", MAX(6, chptr - start))) |
1789 |
rc = unlinkCommand(chptr, end); |
1790 |
else if (!strncmp(start, "rmdir", MAX(5, chptr - start))) |
1791 |
rc = rmdirCommand(chptr, end); |
1648 |
#ifdef DEBUG |
1792 |
#ifdef DEBUG |
1649 |
else if (!strncmp(start, "cat", MAX(3, chptr-start))) |
1793 |
else if (!strncmp(start, "cat", MAX(3, chptr-start))) |
1650 |
rc = catCommand(chptr, end); |
1794 |
rc = catCommand(chptr, end); |