Commit graph

377 commits

Author SHA1 Message Date
Matthias Schiffer
d3fa561e5a blobmsg_json: add new functions blobmsg_format_json_value*
The current blobmsg_format_json* functions will return invalid JSON when
the "list" argument is given as false (blobmsg_format_element() will
output the name of the blob_attr as if the value is printed as part of a
JSON object).

To avoid breaking software relying on this behaviour, introduce new
functions which will never print the blob_attr name and thus always
produce valid JSON.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
Acked-by: Jo-Philipp Wich <jo@mein.io>
Signed-off-by: Felix Fietkau <nbd@nbd.name> [cosmetic style fix]
2016-07-02 10:10:08 +02:00
Eyal Birger
d197c8ffa3 uloop: handle waker pipe write() return value
Recent glibc warns if result of read() or write() is unused.

Added a retry in case of EINTR, all other faults are silently discarded.

Signed-off-by: Eyal Birger <eyal.birger@gmail.com>

-----
- I was not able to reproduce the EINTR case, but it seems to be the right
  thing to do
- Retrying on EAGAIN in this case would be weird as there is no one to read
  from the other end of the pipe. We could call waker_consume() directly but
  since the size of the message is just one byte, I think this would be dead
  code
2016-06-26 13:05:54 +02:00
Matthias Schiffer
1ad3d93eb8 loop: make uloop_run() return the cancelling signal
When a process quits in response to a signal it handles, it should to so
be re-sending the signal to itself. This especially important for SIGINT,
as is explained in [1].

uloop currently hides the reason for quitting uloop_run(). Fix this by
returning the signal that caused the loop to quit (or 0 when uloop_end()
was used), so a program using loop an comply with [1].

[1] https://www.cons.org/cracauer/sigint.html

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2016-06-26 12:55:31 +02:00
Matthias Schiffer
1f019ceea1 Fix various memory management issues
Consistently handle allocation failures. Some functions are changed to
return bool or int instead of void to allow returning an error.

Also fix a buffer size miscalculation in lua/uloop and use _exit() instead
of exit() on errors after forking.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2016-06-26 12:53:51 +02:00
Felix Fietkau
c2f2c47f3e uloop: add missing waker_pipe initialization
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-06-15 15:14:51 +02:00
Yousong Zhou
4e3a47a4cb uloop: use a waker for notifying sigchld and loop cancel events
Fix a race condition when do_sigchld, uloop_cancelled were set just
before epoll_wait(timeout=-1), resulting the loop stuck in the syscall
without noticing the events just happened

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-06-15 11:54:37 +02:00
Felix Fietkau
1257a38a6e uloop: revert signalfd support for now
It hasn't fixed the reported race condition and it introduced some new
issues.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-05-19 10:59:48 +02:00
Felix Fietkau
93be9309b8 uloop: add back support for overriding signal handlers when signalfd is in use
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-05-17 13:59:05 +02:00
Felix Fietkau
004be15be4 uloop: fix signal unblocking
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-05-17 13:35:00 +02:00
Felix Fietkau
b06cd8c58e uloop: retry waitpid on signal interrupt
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-05-17 13:27:44 +02:00
Felix Fietkau
6a75b3b643 uloop: try to use signalfd for signal handling if available
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-05-17 13:23:38 +02:00
Felix Fietkau
8ae74b4378 uloop: move epoll code into a separate file
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-05-17 13:23:38 +02:00
Felix Fietkau
4b0aa9ccc2 uloop: move kqueue code into a separate file
Signed-off-by: Felix Fietkau <nbd@nbd.name>
2016-05-17 13:23:38 +02:00
Matthias Schiffer
155bf39896 blobmsg_json: simplify add_separator and fix thread-safety
The hard-coded length limits are replaced with strlen to make the code more
robust.

Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
2016-04-06 17:43:47 +02:00
Felix Fietkau
dfe446e2a9 jshn: use an avl tree for env variables to speed up processing of bigger json files
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2016-03-05 18:51:31 +01:00
Yousong Zhou
5326ce1046 examples: add shunit2 tests for json_script
Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
2016-02-20 13:36:08 +01:00
Yousong Zhou
02db6d71b6 json_script: add "isdir" support
Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
2016-02-20 13:36:08 +01:00
Jo-Philipp Wich
f43da92dd7 blobmsg_json: support json_type_null in blobmsg_add_json_element()
We already serialize BLOBMSG_TYPE_UNSPEC as "null" so represent JSON null
values as blob attribute type unspec with payload size zero.

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
2016-02-09 23:30:32 +01:00
Felix Fietkau
0c2fbbca80 usock: implement usock_inet_timeout() with RFC6555 support
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2016-01-28 12:07:25 +01:00
Felix Fietkau
bc7e277276 ustream-fd: handle ENOTCONN for read/write on not-yet-connected sockets
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2016-01-15 21:35:48 +01:00
Felix Fietkau
663d7d4b88 lua: add gc/delete support for processes
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2016-01-15 21:26:43 +01:00
Felix Fietkau
a8d12b22e0 lua: create a common function for userdata with gc
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2016-01-15 21:19:47 +01:00
Felix Fietkau
f8b32f7620 usock: add usock_inet, which returns the remote address
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2016-01-15 21:04:16 +01:00
Karl Palsson
682e071ef7 libubox: lua: use pkg-config built in module to search for alternatives
Different distributions have different names for the lua 5.1 package.
Use cmake's built in pkg-config support to search for the first one,
rather than running it explicitly and searching for a single version.

Signed-off-by: Karl Palsson <karlp@remake.is>
2015-11-22 13:17:07 +01:00
Felix Fietkau
10429bccd0 json_script: allow non-string types in command arguments
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2015-11-08 20:19:08 +01:00
Felix Fietkau
3d45c47752 json_script: add support for aborting script processing
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2015-11-06 22:58:15 +01:00
Sergiy Kibrik
136a519626 ulog: avoid accidental /dev/kmsg creation
Race is possible in ulog_kmsg(): if no /dev/kmsg exists
(e.g. while /dev gets re-mounted) regular file created instead.
>From this point system goes without kernel logger:
special character file can't be created anymore, all clients keep
overwriting single message in regular file.

To avoid this we open file in "r+" mode which doesn't create
file if it's not found.

Signed-off-by: Sergiy Kibrik <sakib@meta.ua>
Cc: John Crispin <blogic@openwrt.org>
2015-09-15 07:29:59 +02:00
Bachtin, Dmitri
d66a89f010 b64_encode(): fixed input[] not initialized warn under gcc 4.4.5
Signed-off-by: Dmitri Bachtin <dbachtin@init-ka.de>
2015-09-09 11:03:07 +02:00
Bachtin, Dmitri
2b89f2169b Build static version of libblobmsg_json
Signed-off-by: Dmitri Bachtin <dbachtin@init-ka.de>
2015-09-09 11:02:49 +02:00
Philip Craig
e88d816d6e ustream-fd: handle uloop errors
The default uloop error handling is to delete the fd. Continue to do
that, but also set the write_error flag and notify the user.

Signed-off-by: Philip Craig <philipjcraig@gmail.com>
2015-07-14 16:57:05 +02:00
Felix Fietkau
e7e554c194 md5: include utils.h instead of endian.h to fix portability issues
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2015-06-29 16:13:08 +02:00
Steven Barth
32e49d687b md5: include endian.h for musl, otherwise hashes are invalid for BE
Signed-off-by: Steven Barth <steven@midlink.org>
2015-06-29 16:09:32 +02:00
Yousong Zhou
d1c66ef113 ulog: always use stderr for ulog_stdio().
stdout is for normal program output.

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
2015-06-14 15:45:57 +02:00
Nikolay Dimitrov
4c94515cc8 libubox: cmake: Add BUILD_EXAMPLES option
Add ability to skip building the examples. The default value is ON to follow
the original cmake behavior.

Signed-off-by: Nikolay Dimitrov <picmaster@mail.bg>
2015-06-14 15:25:38 +02:00
Yousong Zhou
8964d77f80 ustream-fd: stop trying to read when s->read_blocked is set.
User's s->notify_read is called in ustream_fill_read().  If
s->read_blocked is set there, then stop reading more.

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
2015-06-14 15:16:25 +02:00
Yousong Zhou
cead67c804 ustream-fd: readability change.
Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
2015-06-14 15:16:10 +02:00
Yousong Zhou
9386d0717a ustream: tweak ustream_prepare_buf() a bit.
No functional change.

 - Reuse existing NULL check on buf.
 - Add some comments for ease of reading the code.

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
2015-06-14 15:15:43 +02:00
Yegor Yefremov
791a361ad1 ubox: CMake: fix json-c detection
Use PKG_SEARCH_MODULE() to detect json-c library,
otherwise the search fails, if both json-c and json
are not present in pkg-config database.

Signed-off-by: Yegor Yefremov <yegorslists@googlemail.com>
2015-05-10 14:14:45 +02:00
Felix Fietkau
a8e70c6d36 add a base64 implementation (based on FreeBSD code)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
2015-05-08 14:34:29 +02:00
John Crispin
b8d9b382e3 allow process callback to call uloop_end()
Signed-off-by: John Crispin <blogic@openwrt.org>
2015-03-21 05:50:23 +01:00
Jo-Philipp Wich
2a9edb4642 ulog: implement ulog_close()
Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
2015-02-26 13:35:50 +01:00
Jo-Philipp Wich
ca6d547205 ulog: introduce new simple logging api
The ulog api is intended to be used by procd, fstools, ubox etc. to provide
a generic logging api for early boot messages and automatic switching between
syslog / kmsg / stdout depending on the way the process is executed.

Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
2015-02-26 10:43:56 +01:00
Rafał Miłecki
827ad8337e uloop: ignore SIGPIPE by default
Most app don't want to crash because of unhandled SIGPIPE. It could
happen is such trivial situations like writing to socket.

Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
2015-01-28 10:51:15 +01:00
Yousong Zhou
ad9aa180d3 utils: use clock_get_time() for clock_gettime() on Apple.
It turns out that mach_absolute_time() is not monotonic at all.  While
at it, convert the CLOCK_REALTIME implementation to using
clock_get_time() as well.

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
2015-01-21 20:11:30 +01:00
Yousong Zhou
08c27ceb01 uloop: optimize uloop_timeout_set() implementaiton a bit.
Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
2015-01-21 20:02:46 +01:00
Yousong Zhou
73c4b4fdfe ustream-fd: fix logic invert of write polling.
ustream_write_pending() returns true if write buffer was flushed there.

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
2015-01-21 20:02:03 +01:00
Yousong Zhou
60236c4853 ustream: add function ustream_read().
It can be used to fill caller-specified buffer with data already in
ustream read buffer.  Useful in the following use pattern.

	int available = ustream_pending_data(s, false);
	if (available >= sizeof(struct msghdr)) {
		struct msghdr h;
		ustream_read(s, &h, sizeof(h));
	}

Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
2015-01-21 20:00:39 +01:00
Rafał Miłecki
f1c794b29e usock: set socket flags right after creating it
Otherwise some flags like USOCK_NONBLOCK wouldn't work as expected
(O_NONBLOCK affects connect behavior).

Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
2015-01-15 13:10:58 +01:00
Rafał Miłecki
ad9b5a387d usock: add helper waiting for socket to be ready
Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
2015-01-15 13:10:49 +01:00
Yousong Zhou
fecaf2f5f6 examples: add example code for json_script.
Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
2014-12-22 15:46:35 +01:00