From 02013228914a1d17e8df15d4e2b7950469395a5c Mon Sep 17 00:00:00 2001 From: Bjørn Mork Date: Fri, 15 May 2015 10:23:51 +0200 Subject: ripe-atlas-fw: imported version 4520 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bjørn Mork --- coreutils/Config.in | 20 ++++++++ coreutils/Kbuild | 2 + coreutils/buddyinfo.c | 72 +++++++++++++++++++++++++++++ coreutils/condmv.c | 100 ++++++++++++++++++++++++++++++++++++++++ coreutils/date.c | 40 ++++++++++++---- coreutils/dfrm.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++ coreutils/findpid.c | 33 ++++++++++++++ coreutils/sleepkick.c | 74 ++++++++++++++++++++++++++++++ coreutils/test.c | 7 ++- 9 files changed, 460 insertions(+), 11 deletions(-) create mode 100644 coreutils/buddyinfo.c create mode 100644 coreutils/condmv.c create mode 100644 coreutils/dfrm.c create mode 100644 coreutils/findpid.c create mode 100644 coreutils/sleepkick.c (limited to 'coreutils') diff --git a/coreutils/Config.in b/coreutils/Config.in index 8cbc92f..bb81994 100644 --- a/coreutils/Config.in +++ b/coreutils/Config.in @@ -72,6 +72,12 @@ config COMM comm is used to compare two files line by line and return a three-column output. +config CONDMV + bool "condmv" + default n + help + condmv is used to rename a file if the destination does not exist + config CP bool "cp" default n @@ -136,6 +142,13 @@ config DF df reports the amount of disk space used and available on filesystems. +config DFRM + bool "dfrm" + default n + help + dfrm deletes files from directories when the amount of free space is + too low + config FEATURE_DF_FANCY bool "Enable -a, -i, -B" default n @@ -537,6 +550,13 @@ config FEATURE_FLOAT_SLEEP help Allow for fractional numeric parameters. +config SLEEPKICK + bool "sleepkick" + default n + help + sleep is used to pause for a specified number of seconds. + And kick the /dev/watchdog every x seconds. RIPE ATLAS. Antony + config SORT bool "sort" default n diff --git a/coreutils/Kbuild b/coreutils/Kbuild index a5a2d4c..4cb1736 100644 --- a/coreutils/Kbuild +++ b/coreutils/Kbuild @@ -20,11 +20,13 @@ lib-$(CONFIG_CHOWN) += chown.o lib-$(CONFIG_CHROOT) += chroot.o lib-$(CONFIG_CKSUM) += cksum.o lib-$(CONFIG_COMM) += comm.o +lib-$(CONFIG_CONDMV) += condmv.o lib-$(CONFIG_CP) += cp.o lib-$(CONFIG_CUT) += cut.o lib-$(CONFIG_DATE) += date.o lib-$(CONFIG_DD) += dd.o lib-$(CONFIG_DF) += df.o +lib-$(CONFIG_DFRM) += dfrm.o lib-$(CONFIG_DIRNAME) += dirname.o lib-$(CONFIG_DOS2UNIX) += dos2unix.o lib-$(CONFIG_DU) += du.o diff --git a/coreutils/buddyinfo.c b/coreutils/buddyinfo.c new file mode 100644 index 0000000..6f41eb1 --- /dev/null +++ b/coreutils/buddyinfo.c @@ -0,0 +1,72 @@ +/* vi: set sw=2 ts=2: + * + * 2010-2013 Copyright (c) 2013 RIPE NCC + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. + * read /cat/proc/buddyinfo and print out. + * if env variable LOWMEM_REBOOT is set KBytes same as buddyinfo reboot + * + */ + +#include "libbb.h" + +/* This is a NOFORK applet. Be very careful! */ + +int buddyinfo_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int buddyinfo_main(int argc UNUSED_PARAM, char **argv) +{ + char *lowmemChar; + unsigned lowmem = 0; + lowmemChar = getenv("LOW_MEM_T"); + if(lowmemChar) + lowmem = xatou(lowmemChar); + + FILE *fp = xfopen_for_read("/proc/buddyinfo"); + char aa[10]; + fscanf(fp, "%s", aa); + fscanf(fp, "%s", aa); + fscanf(fp, "%s", aa); + fscanf(fp, "%s", aa); + + char *my_mac ; + my_mac = getenv("ETHER_SCANNED"); + + int i = 0; + int j = 0; + int memBlock = 4; + int fReboot = 1; // don't reboot + if (lowmem >= 4 ) + { + fReboot = 0; // env variable is set sow we check for low thershhold + } + printf ("RESULT 9001.0 ongoing %d ", (int)time(0)); + if (my_mac != NULL) + printf("%s ", my_mac); + for (j=0; j< 11; j++) + { + fscanf(fp, "%d", &i); + printf("%-3d ", i); + if ( lowmem >= 4) + { + if( memBlock >= lowmem) + { + if(fReboot == 0) + { + if (i > 0 ) + { + fReboot = 1; + + } + } + } + } + memBlock *= 2; + } + printf ("\n"); + fclose(fp); + if(fReboot == 0 ) + { + fprintf(stderr, "buddy info returned 1 for block %d\n", lowmem); + return (EXIT_FAILURE); + } + return EXIT_SUCCESS; +} diff --git a/coreutils/condmv.c b/coreutils/condmv.c new file mode 100644 index 0000000..0139779 --- /dev/null +++ b/coreutils/condmv.c @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2013 RIPE NCC + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. + * condmv.c -- move a file only if the destination doesn't exist + */ + +#include "libbb.h" + +#define SAFE_PREFIX_FROM ATLAS_DATA_NEW +#define SAFE_PREFIX_TO ATLAS_DATA_OUT + +#define A_FLAG (1 << 0) +#define F_FLAG (1 << 1) + +int condmv_main(int argc, char *argv[]) +{ + char *opt_add, *from, *to; + uint32_t opt; + struct stat sb; + FILE *file; + time_t mytime; + + opt_add= NULL; + opt_complementary= NULL; /* For when we are called by crond */ + opt= getopt32(argv, "!A:f", &opt_add); + + if (opt == (uint32_t)-1) + { + fprintf(stderr, "condmv: bad options\n"); + return 1; + } + + if (argc != optind + 2) + { + fprintf(stderr, "condmv: two arguments expected\n"); + return 1; + } + + from= argv[optind]; + to= argv[optind+1]; + + if (!validate_filename(from, SAFE_PREFIX_FROM)) + { + fprintf(stderr, "insecure from file '%s'\n", from); + return 1; + } + if (!validate_filename(to, SAFE_PREFIX_TO) && + !validate_filename(to, SAFE_PREFIX_FROM)) + { + fprintf(stderr, "insecure to file '%s'\n", to); + return 1; + } + + if (stat(to, &sb) == 0 && !(opt & F_FLAG)) + { + /* Destination exists */ + fprintf(stderr, "condmv: not moving, destination '%s' exists\n", + to); + return 1; + } + + if (opt_add) + { + mytime = time(NULL); + /* We have to add something to the existing file before moving + * to. + */ + file= fopen(from, "a"); + if (file == NULL) + { + fprintf(stderr, + "condmv: unable to append to '%s': %s\n", + from, strerror(errno)); + return 1; + } + if (fprintf(file, "%s %lu %s\n", opt_add, mytime, from) < 0) + { + fprintf(stderr, + "condmv: unable to append to '%s': %s\n", + from, strerror(errno)); + fclose(file); + return 1; + } + if (fclose(file) != 0) + { + fprintf(stderr, + "condmv: unable to close '%s': %s\n", + from, strerror(errno)); + return 1; + } + } + if (rename(from, to) != 0) + { + fprintf(stderr, "condmv: unable to rename '%s' to '%s': %s\n", + from, to, strerror(errno)); + return 1; + } + + return 0; +} diff --git a/coreutils/date.c b/coreutils/date.c index 177b7d0..57ec4e7 100644 --- a/coreutils/date.c +++ b/coreutils/date.c @@ -29,8 +29,9 @@ #define DATE_OPT_UTC 0x04 #define DATE_OPT_DATE 0x08 #define DATE_OPT_REFERENCE 0x10 -#define DATE_OPT_TIMESPEC 0x20 -#define DATE_OPT_HINT 0x40 +#define DATE_OPT_UNIXSECS 0x20 +#define DATE_OPT_TIMESPEC 0x40 +#define DATE_OPT_HINT 0x80 static void maybe_set_utc(int opt) { @@ -50,10 +51,11 @@ int date_main(int argc UNUSED_PARAM, char **argv) char *fmt_str2dt; char *filename; char *isofmt_arg = NULL; + char *check; opt_complementary = "d--s:s--d" USE_FEATURE_DATE_ISOFMT(":R--I:I--R"); - opt = getopt32(argv, "Rs:ud:r:" + opt = getopt32(argv, "Rs:ud:r:S" USE_FEATURE_DATE_ISOFMT("I::D:"), &date_str, &date_str, &filename USE_FEATURE_DATE_ISOFMT(, &isofmt_arg, &fmt_str2dt)); @@ -104,7 +106,19 @@ int date_main(int argc UNUSED_PARAM, char **argv) tm_time.tm_hour = 0; /* Process any date input to UNIX time since 1 Jan 1970 */ - if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) { + if (opt & DATE_OPT_UNIXSECS) + { + tm= strtoul(date_str, &check, 10); + if (check[0] != '\0') + { + bb_error_msg_and_die(bb_msg_invalid_date, + date_str); + } + + /* Fill in tm_time */ + tm_time= *localtime(&tm); + } + else if (ENABLE_FEATURE_DATE_ISOFMT && (opt & DATE_OPT_HINT)) { if (strptime(date_str, fmt_str2dt, &tm_time) == NULL) bb_error_msg_and_die(bb_msg_invalid_date, date_str); } else { @@ -168,13 +182,19 @@ int date_main(int argc UNUSED_PARAM, char **argv) bb_error_msg_and_die(bb_msg_invalid_date, date_str); } } - /* Correct any day of week and day of year etc. fields */ - tm_time.tm_isdst = -1; /* Be sure to recheck dst. */ - tm = mktime(&tm_time); - if (tm < 0) { - bb_error_msg_and_die(bb_msg_invalid_date, date_str); + if (!(opt & DATE_OPT_UNIXSECS)) + { + /* Correct any day of week and day of year etc. + * fields + */ + tm_time.tm_isdst = -1; /* Be sure to recheck dst. */ + tm = mktime(&tm_time); + if (tm < 0) { + bb_error_msg_and_die(bb_msg_invalid_date, + date_str); + } + maybe_set_utc(opt); } - maybe_set_utc(opt); /* if setting time, set it */ if ((opt & DATE_OPT_SET) && stime(&tm) < 0) { diff --git a/coreutils/dfrm.c b/coreutils/dfrm.c new file mode 100644 index 0000000..75e3a5f --- /dev/null +++ b/coreutils/dfrm.c @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2013 RIPE NCC + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. + * dfrm.c + * Remove the contents of directories if the amount of free space gets too low + */ + +#include +#include +#include +#include +#include +#include + +#include "libbb.h" + +#define DBQ(str) "\"" #str "\"" + +int dfrm_main(int argc, char *argv[]) +{ + int i; + size_t len; + uint32_t opt; + unsigned long limit, avail; + char *opt_atlas; + char *dev, *limit_str, *dir_str, *check, *path; + DIR *dir; + struct dirent *de; + struct statfs sb; + + opt_atlas= NULL; + opt_complementary= NULL; /* Just in case */ + opt= getopt32(argv, "A:", &opt_atlas); + + if (argc < optind+3) + { + printf("not enough arguments\n"); + return 1; + } + dev= argv[optind]; + limit_str= argv[optind+1]; + + if (statfs(dev, &sb) != 0) + { + fprintf(stderr, "statfs on %s failed: %s\n", + dev, strerror(errno)); + return 1; + } + + printf("RESULT { "); + if (opt_atlas) + { + printf( + DBQ(id) ":" DBQ(%s) ", " DBQ(fw) ": %d, " DBQ(time) ": %ld, ", + opt_atlas, get_atlas_fw_version(), (long)time(NULL)); + } + printf(DBQ(bsize) ": %ld, " DBQ(blocks) ": %ld, " + DBQ(bfree) ": %ld, " DBQ(free) ": %ld", + (long)sb.f_bsize, (long)sb.f_blocks, (long)sb.f_bfree, + (long)sb.f_bfree*(sb.f_bsize/1024)); + printf(" }\n"); + + avail= sb.f_bavail*(sb.f_bsize/1024); + + limit= strtoul(limit_str, &check, 10); + if (check[0] != '\0') + { + fprintf(stderr, "unable to parse limit '%s'\n", limit_str); + return 1; + } + if (avail > limit) + { + fprintf(stderr, "enough space free, no need to do anything\n"); + return 1; + } + + for (i= optind+2; i < argc; i++) + { + dir_str= argv[i]; + + dir= opendir(dir_str); + if (!dir) + { + fprintf(stderr, "opendir failed for '%s'\n", dir_str); + continue; + } + + path= NULL; + while (de= readdir(dir), de != NULL) + { + if (strcmp(de->d_name, ".") == 0 || + strcmp(de->d_name, "..") == 0) + { + continue; + } + len= strlen(dir_str) + 1 + strlen(de->d_name) + 1; + path= realloc(path, len); /* Avoid leaks */ + if (path == NULL) + { + fprintf(stderr, + "unable to allocate %ld bytes\n", + (long)len); + continue; + } + strlcpy(path, dir_str, len); + strlcat(path, "/", len); + strlcat(path, de->d_name, len); + + if (unlink(path) != 0) + { + fprintf(stderr, "unable to unlink '%s': %s\n", + path, strerror(errno)); + continue; + } + fprintf(stderr, "rm %s\n", path); + } + closedir(dir); + free(path); path= NULL; + + } + + return 0; +} diff --git a/coreutils/findpid.c b/coreutils/findpid.c new file mode 100644 index 0000000..fa1d572 --- /dev/null +++ b/coreutils/findpid.c @@ -0,0 +1,33 @@ +/* vi: set sw=4 ts=4: + * + * Copyright (c) 2010-2013 RIPE NCC + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. + * simple find_pid_name. return 0 if a name is found + */ + +#include "libbb.h" + +/* This is a NOFORK applet. Be very careful! */ + +int findpid_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int findpid_main(int argc UNUSED_PARAM, char **argv) +{ + pid_t* pidList; + procps_status_t* p = NULL; + + if (argc > 1) + { + while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN))) + { + if (comm_match(p, argv[1]) + /* or we require argv0 to match (essential for matching reexeced + /proc/self/exe)*/ + || (p->argv0 && strcmp(bb_basename(p->argv0), argv) == 0) + /* TODO: we can also try /proc/NUM/exe link, do we want that? */ + ) { + return EXIT_SUCCESS; + } + } + } + return EXIT_FAILURE; +} diff --git a/coreutils/sleepkick.c b/coreutils/sleepkick.c new file mode 100644 index 0000000..cd06867 --- /dev/null +++ b/coreutils/sleepkick.c @@ -0,0 +1,74 @@ +/* vi: set sw=4 ts=4: + * Copyright (C) 2003 Manuel Novoa III + * Copyright (c) 2010-2013 RIPE NCC, Antony + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. + * + * sleep implementation for busybox with watchdog petting. + */ + +/* BB_AUDIT SUSv3 compliant */ +/* BB_AUDIT GNU issues -- fancy version matches except args must be ints. */ +/* http://www.opengroup.org/onlinepubs/007904975/utilities/sleep.html */ + +/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) + * + * Rewritten to do proper arg and error checking. + * Also, added a 'fancy' configuration to accept multiple args with + * time suffixes for seconds, minutes, hours, and days. + */ + +#include "libbb.h" +#define WATCHDOGDEV "/dev/watchdog" + +/* This is a NOFORK applet. Be very careful! */ + +int sleepkick_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +int sleepkick_main(int argc UNUSED_PARAM, char **argv) +{ + unsigned duration; + unsigned watchdog; + ++argv; + if (!*argv) + bb_show_usage(); + duration = xatou(*argv); + ++argv; + + if(*argv) + { + int fd; /* File handler for watchdog */ + int i = 0; + int iMax = 0; + + watchdog = xatou(*argv); + iMax = (int) (duration / watchdog); + + int modDuration = 0; + int wReminder = 0; + if( duration >= watchdog) + { + modDuration = duration % watchdog; + } + else { + modDuration = duration; + } + + fd = open(WATCHDOGDEV, O_RDWR); + for( i = 0; i < iMax; i++) + { + write(fd, "1", 1); + sleep(watchdog); + } + if(modDuration) + { + write(fd, "1", 1); + sleep(modDuration); + write(fd, "1", 1); + } + close(fd); + } + else + { + sleep(duration); + } + return EXIT_SUCCESS; +} diff --git a/coreutils/test.c b/coreutils/test.c index ae40192..1b7a51b 100644 --- a/coreutils/test.c +++ b/coreutils/test.c @@ -42,7 +42,7 @@ unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"| "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S"; - binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"| + binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-ad"|"-le"|"-lt"| "-nt"|"-ot"|"-ef"; operand ::= */ @@ -82,6 +82,7 @@ enum token { INTNE, INTGE, INTGT, + INTAD, INTLE, INTLT, UNOT, @@ -154,6 +155,7 @@ static const char *const TOKSTR[] = { "INTNE", "INTGE", "INTGT", + "INTAD' "INTLE", "INTLT", "UNOT", @@ -216,6 +218,7 @@ static const struct operator_t ops[] = { { "-ge", INTGE , BINOP }, { "-gt", INTGT , BINOP }, { "-le", INTLE , BINOP }, + { "-ad", INTAD , BINOP }, { "-lt", INTLT , BINOP }, { "-nt", FILNT , BINOP }, { "-ot", FILOT , BINOP }, @@ -383,6 +386,8 @@ static int binop(void) return val1 >= val2; if (op->op_num == INTGT) return val1 > val2; + if (op->op_num == INTAD) + return 7; if (op->op_num == INTLE) return val1 <= val2; if (op->op_num == INTLT) -- cgit v1.2.3