[Toybox] [PATCH] modprobe: use finit_module when possible

2017-01-25 Thread Steve Muckle
The finit_module() system call, introduced in Linux 3.8, reads the
module from a supplied file descriptor. This allows the kernel to do
security checks based on the file's location.
From fbb90c1db3c3ec4cffce32e4cdd67a880ed8e9f2 Mon Sep 17 00:00:00 2001
From: Steve Muckle 
Date: Wed, 25 Jan 2017 17:51:40 -0800
Subject: [PATCH] modprobe: use finit_module when possible

The finit_module() system call, introduced in Linux 3.8, reads the
module from a supplied file descriptor. This allows the kernel to do
security checks based on the file's location.

---
 toys/pending/modprobe.c | 17 +
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/toys/pending/modprobe.c b/toys/pending/modprobe.c
index 50ec60f..c701f5b 100644
--- a/toys/pending/modprobe.c
+++ b/toys/pending/modprobe.c
@@ -373,6 +373,19 @@ static int ins_mod(char *modules, char *flags)
   int len, res;
   int fd = xopenro(modules);
 
+  while (flags && strlen(toybuf) + strlen(flags) + 2 < sizeof(toybuf)) {
+strcat(toybuf, flags);
+strcat(toybuf, " ");
+  }
+
+#ifdef __NR_finit_module
+  res = syscall(__NR_finit_module, fd, toybuf, 0);
+  if (!res || errno != ENOSYS) {
+	  xclose(fd);
+	  return res;
+  }
+#endif
+
   // TODO xreadfile()
 
   len = fdlength(fd);
@@ -380,10 +393,6 @@ static int ins_mod(char *modules, char *flags)
   xreadall(fd, buf, len);
   xclose(fd);
 
-  while (flags && strlen(toybuf) + strlen(flags) + 2 < sizeof(toybuf)) {
-strcat(toybuf, flags);
-strcat(toybuf, " ");
-  }
   res = syscall(__NR_init_module, buf, len, toybuf);
   if (CFG_TOYBOX_FREE && buf != toybuf) free(buf);
   return res;
-- 
2.11.0.483.g087da7b7c-goog

___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] Stat %Z - What are valid values?

2017-01-25 Thread Rich Felker
On Wed, Jan 25, 2017 at 05:15:37PM -0600, Rob Landley wrote:
> On 01/24/2017 06:10 PM, Rich Felker wrote:
> >>> strptime with %s? I suspect there are some nasty underspecified issues
> >>> with how it interacts with timezones.
> >>
> >> I thought unix time was always UTS and the timezone just affected how it
> >> was displayed?
> > 
> > The problem is that strptime produces a struct tm. When the fields
> > it's parsing to produce this struct tm are "broken down time" fields,
> > strptime does not need to know/care whether the caller is going to
> > interpret the struct tm as UTC or local time; it's just a bunch of
> > numbers. But when strptime is going to take a unix time value (seconds
> > since epoch) and convert it to struct tm, it matters whether the
> > caller is supposed to treat that struct tm as UTC or local.
> 
> According to man 2 time:
> 
>  time()  returns  the  time  as  the  number of seconds since the Epoch,
>  1970-01-01 00:00:00 + (UTC).
> 
> I.E. the definition of unix time is UTC. So %s _not_ being UTC is silly.

Nobody is questioning whether the input read by %s is UTC. The
question is whether the output struct tm is UTC or local.

> My use case (and presumably most people's) is turning a time string into
> a struct timespec. What the actual struct tm fields are is irrelevant to
> that as long as mktime() translates the result back to the right number.

mktime interprets it as local, in which case strptime would have to be
doing the inverse operation, converting from UTC to local for the
struct tm output.

On the other hand strptime output may also be used with timegm (or a
hand-rolled version thereof). This is what you would do if you were
parsing a sting in UTC. With all the existing/standard fields,
strptime is completely agnostic with regards to what you want to do
with the output. Only with %s will it need a hard-coded assumption
about what you're going to do with the output.

> The struct tm has a timezone field in it. As long as strptime sets the
> timezone when it adjusts the hours so mktime() can adjust it _back_, the
> result is the same number you fed into %s. Which is the point of the
> exercise.

mktime cannot use the timezone field of struct tm. It's required to
assume the struct tm is in local time in the current timezone.

Rich
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [PATCH] modprobe: add -d option to specify module directory path(s)

2017-01-25 Thread enh
On Wed, Jan 25, 2017 at 3:59 PM, Steve Muckle  wrote:
> While most systems have their kernel modules, modules.dep etc located at
> /lib/modules/`uname -r` this is not always the case.

in case it isn't obvious: "...such as on Android".

> The -d option may be used to specify a nonstandard path for these files.
> It may be used more than once to specify multiple directories where
> these files may be found.

+TT.dirs->arg = xmalloc(strlen(MODULE_BASE_DIR) + strlen(uts.release) + 1);
+strcpy(TT.dirs->arg, MODULE_BASE_DIR);
+strcat(TT.dirs->arg, uts.release);

or xmprintf?

>
> ___
> Toybox mailing list
> Toybox@lists.landley.net
> http://lists.landley.net/listinfo.cgi/toybox-landley.net
>



-- 
Elliott Hughes - http://who/enh - http://jessies.org/~enh/
Android native code/tools questions? Mail me/drop by/add me as a reviewer.
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


[Toybox] [PATCH] modprobe: add -d option to specify module directory path(s)

2017-01-25 Thread Steve Muckle
While most systems have their kernel modules, modules.dep etc located at
/lib/modules/`uname -r` this is not always the case.

The -d option may be used to specify a nonstandard path for these files.
It may be used more than once to specify multiple directories where
these files may be found.
From 81c53e5a0888d7a965fd042b9cd668d3db4e9fa2 Mon Sep 17 00:00:00 2001
From: Steve Muckle 
Date: Mon, 9 Jan 2017 11:54:20 -0800
Subject: [PATCH] modprobe: add -d option to specify module directory path(s)

While most systems have their kernel modules, modules.dep etc located at
/lib/modules/`uname -r` this is not always the case.

The -d option may be used to specify a nonstandard path for these files.
It may be used more than once to specify multiple directories where
these files may be found.
---
 toys/pending/modprobe.c | 43 ---
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/toys/pending/modprobe.c b/toys/pending/modprobe.c
index 7c93a5c..50ec60f 100644
--- a/toys/pending/modprobe.c
+++ b/toys/pending/modprobe.c
@@ -5,17 +5,18 @@
  *
  * No Standard.
 
-USE_MODPROBE(NEWTOY(modprobe, "alrqvsDb", TOYFLAG_SBIN))
+USE_MODPROBE(NEWTOY(modprobe, "alrqvsDbd*", TOYFLAG_SBIN))
 
 config MODPROBE
   bool "modprobe"
   default n
   help
-usage: modprobe [-alrqvsDb] MODULE [symbol=value][...]
+usage: modprobe [-alrqvsDb] [-d DIR] MODULE [symbol=value][...]
 
 modprobe utility - inserts modules and dependencies.
 
 -a  Load multiple MODULEs
+-d  Load modules from DIR, option may be used multiple times
 -l  List (MODULE is a pattern)
 -r  Remove MODULE (stacks) or do autoclean
 -q  Quiet
@@ -29,6 +30,7 @@ config MODPROBE
 #include 
 
 GLOBALS(
+  struct arg_list *dirs;
   struct arg_list *probes;
   struct arg_list *dbase[256];
   char *cmdopts;
@@ -42,6 +44,7 @@ GLOBALS(
  */
 #define DBASE_SIZE  256
 #define MODNAME_LEN 256
+#define MODULE_BASE_DIR "/lib/modules/"
 
 // Modules flag definations
 #define MOD_ALOADED   0x0001
@@ -492,6 +495,7 @@ void modprobe_main(void)
   FILE *fs;
   struct module_s *module;
   unsigned flags = toys.optflags;
+  struct arg_list *dirs;
 
   TT.dbg = (flags & FLAG_v) ? xprintf : dummy;
 
@@ -506,16 +510,24 @@ void modprobe_main(void)
 return;
   }
 
-  // change directory to /lib/modules// 
-  xchdir("/lib/modules");
-  uname();
-  xchdir(uts.release);
+  if (!TT.dirs) {
+uname();
+TT.dirs = xzalloc(sizeof(struct arg_list));
+TT.dirs->arg = xmalloc(strlen(MODULE_BASE_DIR) + strlen(uts.release) + 1);
+strcpy(TT.dirs->arg, MODULE_BASE_DIR);
+strcat(TT.dirs->arg, uts.release);
+  }
 
   // modules.dep processing for dependency check.
   if (flags & FLAG_l) {
-if (depmode_read_entry(toys.optargs[0])) error_exit("no module found.");
-return;
+for (dirs = TT.dirs; dirs; dirs = dirs->next) {
+  xchdir(dirs->arg);
+  if (!depmode_read_entry(toys.optargs[0]))
+	  return;
+}
+error_exit("no module found.");
   }
+
   // Read /proc/modules to get loaded modules.
   fs = xfopen("/proc/modules", "r");
   
@@ -540,9 +552,18 @@ void modprobe_main(void)
   }
   dirtree_read("/etc/modprobe.conf", config_action);
   dirtree_read("/etc/modprobe.d", config_action);
-  if (TT.symreq) dirtree_read("modules.symbols", config_action);
-  if (TT.nudeps) dirtree_read("modules.alias", config_action);
-  find_dep();
+
+  for (dirs = TT.dirs; dirs; dirs = dirs->next) {
+xchdir(dirs->arg);
+if (TT.symreq) dirtree_read("modules.symbols", config_action);
+if (TT.nudeps) dirtree_read("modules.alias", config_action);
+  }
+
+  for (dirs = TT.dirs; dirs; dirs = dirs->next) {
+	  xchdir(dirs->arg);
+	  find_dep();
+  }
+
   while ((module = llist_popme())) {
 if (!module->rnames) {
   TT.dbg("probing by module name\n");
-- 
2.11.0.483.g087da7b7c-goog

___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [PATCH] Fix microcom command-line help and -X.

2017-01-25 Thread enh
On Wed, Jan 25, 2017 at 2:25 PM, Rob Landley  wrote:
> Catching up...
>
> ctrl-x is not a good escape character, it's the "attention" key for
> emacs. The traditional (telnet) escape character is ctrl-right bracket
> (ascii 29) which as far as I know doesn't conflict with anything. Any
> strong opinions if I change it? (I've done it for now but can switch it
> back if there's strong opposition. Trying to get a release out...)

i thought ctrl-x was weird too, but wanted to duplicate an existing
wheel rather than start on a new one :-)

the other thing that seemed odd to me was that there was no support
for _actually_ sending the escape character.

> I have a loop that calculates all the serial speed values (through 4
> megabits/second) but unfortunately the serial devices I have lying
> around ignore the serial speed setting. (I did -s 115200 and -s 9600 and
> it behaves identically.) Alas, fairly common thing with USB to serial
> converters (which tend to have a fixed speed on one side and a packet
> protocol on the other; one one side you have to match the hardwired
> speed, on the other speed setting is completely ignored).
>
> That said, you've convinced me not to try to make most of the code
> common because nothing else (except stty) really cares about serial
> ports. (Unless I add a pppd which nobody's asked for yet. :)
>
> Rob



-- 
Elliott Hughes - http://who/enh - http://jessies.org/~enh/
Android native code/tools questions? Mail me/drop by/add me as a reviewer.
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] Stat %Z - What are valid values?

2017-01-25 Thread Rob Landley
On 01/24/2017 06:10 PM, Rich Felker wrote:
>>> strptime with %s? I suspect there are some nasty underspecified issues
>>> with how it interacts with timezones.
>>
>> I thought unix time was always UTS and the timezone just affected how it
>> was displayed?
> 
> The problem is that strptime produces a struct tm. When the fields
> it's parsing to produce this struct tm are "broken down time" fields,
> strptime does not need to know/care whether the caller is going to
> interpret the struct tm as UTC or local time; it's just a bunch of
> numbers. But when strptime is going to take a unix time value (seconds
> since epoch) and convert it to struct tm, it matters whether the
> caller is supposed to treat that struct tm as UTC or local.

According to man 2 time:

 time()  returns  the  time  as  the  number of seconds since the Epoch,
 1970-01-01 00:00:00 + (UTC).

I.E. the definition of unix time is UTC. So %s _not_ being UTC is silly.

My use case (and presumably most people's) is turning a time string into
a struct timespec. What the actual struct tm fields are is irrelevant to
that as long as mktime() translates the result back to the right number.
The struct tm has a timezone field in it. As long as strptime sets the
timezone when it adjusts the hours so mktime() can adjust it _back_, the
result is the same number you fed into %s. Which is the point of the
exercise.

(Remember when Linux systems used to have a "store system clock in GMT
instead of localtime" option during config, and the config option went
away because nobody ever _didn't_ do that? Yeah, that.)

> Rich

Rob
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


Re: [Toybox] [PATCH] Fix microcom command-line help and -X.

2017-01-25 Thread Rob Landley
Catching up...

ctrl-x is not a good escape character, it's the "attention" key for
emacs. The traditional (telnet) escape character is ctrl-right bracket
(ascii 29) which as far as I know doesn't conflict with anything. Any
strong opinions if I change it? (I've done it for now but can switch it
back if there's strong opposition. Trying to get a release out...)

I have a loop that calculates all the serial speed values (through 4
megabits/second) but unfortunately the serial devices I have lying
around ignore the serial speed setting. (I did -s 115200 and -s 9600 and
it behaves identically.) Alas, fairly common thing with USB to serial
converters (which tend to have a fixed speed on one side and a packet
protocol on the other; one one side you have to match the hardwired
speed, on the other speed setting is completely ignored).

That said, you've convinced me not to try to make most of the code
common because nothing else (except stty) really cares about serial
ports. (Unless I add a pppd which nobody's asked for yet. :)

Rob
___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net


[Toybox] [PATCH] Remove name length limit for system properties

2017-01-25 Thread dimitry
Hi,

 Android O removes 32 byte limit on system property names. This patch is
the follow up on this change fixing getprop and setprop toys accordingly.
From 3fd93922e01fa30c4ef8a4dd7182e5f9ee0fc3a6 Mon Sep 17 00:00:00 2001
From: Dimitry Ivanov 
Date: Wed, 25 Jan 2017 13:27:03 -0800
Subject: [PATCH] Remove name length limit for system properties

Android O removes name length limit for system properties.

Use __system_property_read_callback instead of deprecated
__system_property_read in getprop and remove check for
property name length in setprop.

Test: adb shell setprop debug.test.very.very.long.property.name valueforpropertywithlongname
Test: adb shell getprop | grep debug.test.very.very.long.property.name
Bug: http://b/33926793
Change-Id: I57ca99ea33283d069cd1b7b9f110ec9fb27f3d19
---
 toys/android/getprop.c | 12 ++--
 toys/android/setprop.c |  3 ---
 2 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/toys/android/getprop.c b/toys/android/getprop.c
index 390cb98..51e7344 100644
--- a/toys/android/getprop.c
+++ b/toys/android/getprop.c
@@ -39,13 +39,8 @@ static char *get_property_context(char *property)
   return context;
 }
 
-static void add_property(const prop_info *pi, void *unused)
+static void read_callback(void *unused, const char *name, const char *value)
 {
-  char name[PROP_NAME_MAX];
-  char value[PROP_VALUE_MAX];
-
-  __system_property_read(pi, name, value);
-
   if (!(TT.size&31)) TT.nv = xrealloc(TT.nv, (TT.size+32)*2*sizeof(char *));
 
   TT.nv[2*TT.size] = xstrdup(name);
@@ -56,6 +51,11 @@ static void add_property(const prop_info *pi, void *unused)
   }
 }
 
+static void add_property(const prop_info *pi, void *unused)
+{
+  __system_property_read_callback(pi, read_callback, NULL);
+}
+
 // Needed to supress extraneous "Loaded property_contexts from" message
 static int selinux_log_callback_local(int type, const char *fmt, ...)
 {
diff --git a/toys/android/setprop.c b/toys/android/setprop.c
index 2af314a..c135615 100644
--- a/toys/android/setprop.c
+++ b/toys/android/setprop.c
@@ -29,9 +29,6 @@ void setprop_main(void)
   // recognize most failures (because it doesn't wait for init), so
   // we duplicate all of init's checks here to help the user.
 
-  if (name_len >= PROP_NAME_MAX)
-error_exit("name '%s' too long; try '%.*s'",
-   name, PROP_NAME_MAX - 1, name);
   if (value_len >= PROP_VALUE_MAX)
 error_exit("value '%s' too long; try '%.*s'",
value, PROP_VALUE_MAX - 1, value);
-- 
2.11.0.483.g087da7b7c-goog

___
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net