Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: socket: syscall for socketpair(2) #24367

Closed
wants to merge 7 commits into from
Closed

net: socket: syscall for socketpair(2) #24367

wants to merge 7 commits into from

Conversation

cfriedt
Copy link
Member

@cfriedt cfriedt commented Apr 15, 2020

Provide an implementation for socketpair(2). Currently, this is limited to the AF_UNIX address family, SOCK_STREAM, SOCK_RAW, and protocol 0.

Please feel free to suggest additional testing in tests/net/socket/socketpair/ or to suggest improvements in the implementation.

Fixes #24366

@zephyrbot zephyrbot added area: Networking area: API Changes to public APIs area: Tests Issues related to a particular existing or missing test labels Apr 15, 2020
@zephyrbot
Copy link
Collaborator

zephyrbot commented Apr 15, 2020

Some checks failed. Please fix and resubmit.

checkpatch issues

-:50: WARNING:LONG_LINE_COMMENT: line over 80 characters
#50: FILE: include/net/socket.h:168:
+ * <https://pubs.opengroup.org/onlinepubs/009695399/functions/socketpair.html>`__

-:162: WARNING:LONG_LINE: line over 80 characters
#162: FILE: subsys/net/lib/sockets/socketpair.c:25:
+#define D(fmt, args...) printk("D: %s(): %d: " fmt "\n", __func__, __LINE__, ##args)

-:179: WARNING:LINE_SPACING: Missing a blank line after declarations
#179: FILE: subsys/net/lib/sockets/socketpair.c:42:
+	bool blocking :1;
+	u8_t __aligned(4) buf[CONFIG_NET_SOCKETPAIR_BUFFER_SIZE];

-:184: ERROR:OPEN_BRACE: open brace '{' following function declarations go on the next line
#184: FILE: subsys/net/lib/sockets/socketpair.c:47:
+static void spair_init(struct spair *spair) {

-:256: ERROR:SPACING: space required before the open parenthesis '('
#256: FILE: subsys/net/lib/sockets/socketpair.c:119:
+	for(size_t i = 0; i < 2; ++i) {

-:278: WARNING:LONG_LINE: line over 80 characters
#278: FILE: subsys/net/lib/sockets/socketpair.c:141:
+		z_finalize_fd(tmp[i], obj[i], (const struct fd_op_vtable *) &spair_fd_op_vtable);

-:279: WARNING:LONG_LINE: line over 80 characters
#279: FILE: subsys/net/lib/sockets/socketpair.c:142:
+		D("finalized fd: %d spair: %p vtable: %p &recv_q: %p", tmp[i], obj[i], &spair_fd_op_vtable, &obj[i]->recv_q);

-:282: ERROR:SPACING: space required before the open parenthesis '('
#282: FILE: subsys/net/lib/sockets/socketpair.c:145:
+	for(size_t i = 0; i < 2; ++i) {

-:292: ERROR:SPACING: space required before the open parenthesis '('
#292: FILE: subsys/net/lib/sockets/socketpair.c:155:
+	for(size_t i = 0; i < 2; ++i) {

-:299: ERROR:SPACING: space required before the open parenthesis '('
#299: FILE: subsys/net/lib/sockets/socketpair.c:162:
+	for(size_t i = 0; i < 2; ++i) {

-:311: WARNING:LONG_LINE: line over 80 characters
#311: FILE: subsys/net/lib/sockets/socketpair.c:174:
+static inline int z_vrfy_zsock_socketpair(int family, int type, int proto, int *sv)

-:328: ERROR:SPACING: space prohibited after that open parenthesis '('
#328: FILE: subsys/net/lib/sockets/socketpair.c:191:
+	if ( spair->magic != SPAIR_MAGIC ) {

-:328: ERROR:SPACING: space prohibited before that close parenthesis ')'
#328: FILE: subsys/net/lib/sockets/socketpair.c:191:
+	if ( spair->magic != SPAIR_MAGIC ) {

-:349: WARNING:LONG_LINE_STRING: line over 80 characters
#349: FILE: subsys/net/lib/sockets/socketpair.c:212:
+	D("recv_q: buffer: %p size: %u bytes_used: %u read_index: %u write_index: %u flags: %u",

-:350: WARNING:LONG_LINE: line over 80 characters
#350: FILE: subsys/net/lib/sockets/socketpair.c:213:
+		spair->recv_q.buffer, spair->recv_q.size, spair->recv_q.bytes_used,

-:351: WARNING:LONG_LINE: line over 80 characters
#351: FILE: subsys/net/lib/sockets/socketpair.c:214:
+		spair->recv_q.read_index, spair->recv_q.write_index, spair->recv_q.flags);

-:352: WARNING:LONG_LINE: line over 80 characters
#352: FILE: subsys/net/lib/sockets/socketpair.c:215:
+	res = k_pipe_get(&spair->recv_q, (void *)buffer, count, & bytes_read, 0, K_NO_WAIT);

-:352: ERROR:SPACING: space prohibited after that '&' (ctx:WxW)
#352: FILE: subsys/net/lib/sockets/socketpair.c:215:
+	res = k_pipe_get(&spair->recv_q, (void *)buffer, count, & bytes_read, 0, K_NO_WAIT);
 	                                                        ^

-:366: WARNING:LONG_LINE: line over 80 characters
#366: FILE: subsys/net/lib/sockets/socketpair.c:229:
+	struct spair *const remote = z_get_fd_obj(spair->remote, (const struct fd_op_vtable *) &spair_fd_op_vtable, 0);

-:371: ERROR:SPACING: space prohibited after that open parenthesis '('
#371: FILE: subsys/net/lib/sockets/socketpair.c:234:
+	if ( spair->magic != SPAIR_MAGIC ) {

-:371: ERROR:SPACING: space prohibited before that close parenthesis ')'
#371: FILE: subsys/net/lib/sockets/socketpair.c:234:
+	if ( spair->magic != SPAIR_MAGIC ) {

-:393: WARNING:LONG_LINE_STRING: line over 80 characters
#393: FILE: subsys/net/lib/sockets/socketpair.c:256:
+	D("recv_q: buffer: %p size: %u bytes_used: %u read_index: %u write_index: %u flags: %u",

-:394: WARNING:LONG_LINE: line over 80 characters
#394: FILE: subsys/net/lib/sockets/socketpair.c:257:
+		remote->recv_q.buffer, remote->recv_q.size, remote->recv_q.bytes_used,

-:395: WARNING:LONG_LINE: line over 80 characters
#395: FILE: subsys/net/lib/sockets/socketpair.c:258:
+		remote->recv_q.read_index, remote->recv_q.write_index, remote->recv_q.flags);

-:396: WARNING:LONG_LINE: line over 80 characters
#396: FILE: subsys/net/lib/sockets/socketpair.c:259:
+	res = k_pipe_put(&remote->recv_q, (void *)buffer, count, & bytes_written, 0, K_NO_WAIT);

-:396: ERROR:SPACING: space prohibited after that '&' (ctx:WxW)
#396: FILE: subsys/net/lib/sockets/socketpair.c:259:
+	res = k_pipe_put(&remote->recv_q, (void *)buffer, count, & bytes_written, 0, K_NO_WAIT);
 	                                                         ^

-:412: ERROR:SPACING: space prohibited after that open parenthesis '('
#412: FILE: subsys/net/lib/sockets/socketpair.c:275:
+	if ( spair->magic != SPAIR_MAGIC ) {

-:412: ERROR:SPACING: space prohibited before that close parenthesis ')'
#412: FILE: subsys/net/lib/sockets/socketpair.c:275:
+	if ( spair->magic != SPAIR_MAGIC ) {

-:495: WARNING:ENOSYS: ENOSYS means 'invalid syscall nr' and nothing else
#495: FILE: subsys/net/lib/sockets/socketpair.c:358:
+	errno = ENOSYS;

-:519: WARNING:ENOSYS: ENOSYS means 'invalid syscall nr' and nothing else
#519: FILE: subsys/net/lib/sockets/socketpair.c:382:
+	errno = ENOSYS;

-:532: WARNING:ENOSYS: ENOSYS means 'invalid syscall nr' and nothing else
#532: FILE: subsys/net/lib/sockets/socketpair.c:395:
+	errno = ENOSYS;

-:637: WARNING:UNSPECIFIED_INT: Prefer 'unsigned int' to bare use of 'unsigned'
#637: FILE: tests/net/socket/socketpair/src/main.c:26:
+	const unsigned family, const char *family_s,

-:638: WARNING:UNSPECIFIED_INT: Prefer 'unsigned int' to bare use of 'unsigned'
#638: FILE: tests/net/socket/socketpair/src/main.c:27:
+	const unsigned type, const char *type_s,

-:639: WARNING:UNSPECIFIED_INT: Prefer 'unsigned int' to bare use of 'unsigned'
#639: FILE: tests/net/socket/socketpair/src/main.c:28:
+	const unsigned proto, const char *proto_s

-:662: ERROR:SPACING: space required before the open parenthesis '('
#662: FILE: tests/net/socket/socketpair/src/main.c:51:
+	for(int i = 1; i > 0; --i) {

-:688: WARNING:CONSTANT_COMPARISON: Comparisons should place the constant on the right side of the test
#688: FILE: tests/net/socket/socketpair/src/main.c:77:
+		zassert_true(0 == strncmp(expected_msg, actual_msg,

-:714: WARNING:CONSTANT_COMPARISON: Comparisons should place the constant on the right side of the test
#714: FILE: tests/net/socket/socketpair/src/main.c:103:
+		zassert_true(0 == strncmp(expected_msg, actual_msg,

-:733: WARNING:LONG_LINE: line over 80 characters
#733: FILE: tests/net/socket/socketpair/src/main.c:122:
+		res = recvfrom(sv[(!i) & 1], actual_msg, sizeof(actual_msg), 0, NULL, &len);

-:740: WARNING:CONSTANT_COMPARISON: Comparisons should place the constant on the right side of the test
#740: FILE: tests/net/socket/socketpair/src/main.c:129:
+		zassert_true(0 == strncmp(expected_msg, actual_msg,

-:767: WARNING:CONSTANT_COMPARISON: Comparisons should place the constant on the right side of the test
#767: FILE: tests/net/socket/socketpair/src/main.c:156:
+		zassert_true(0 == strncmp(expected_msg, actual_msg,

-:786: WARNING:UNSPECIFIED_INT: Prefer 'unsigned int' to bare use of 'unsigned'
#786: FILE: tests/net/socket/socketpair/src/main.c:175:
+		unsigned u;

-:795: ERROR:C99_COMMENTS: do not use C99 // comments
#795: FILE: tests/net/socket/socketpair/src/main.c:184:
+		//{ SOCK_RAW,    "SOCK_RAW"    },

-:801: ERROR:SPACING: space required before the open parenthesis '('
#801: FILE: tests/net/socket/socketpair/src/main.c:190:
+	for(size_t i = 0; i < ARRAY_SIZE(address_family); ++i) {

-:802: ERROR:SPACING: space required before the open parenthesis '('
#802: FILE: tests/net/socket/socketpair/src/main.c:191:
+		for(size_t j = 0; j < ARRAY_SIZE(socket_type); ++j) {

-:803: ERROR:SPACING: space required before the open parenthesis '('
#803: FILE: tests/net/socket/socketpair/src/main.c:192:
+			for(size_t k = 0; k < ARRAY_SIZE(protocol); ++k) {

-:805: WARNING:LONG_LINE: line over 80 characters
#805: FILE: tests/net/socket/socketpair/src/main.c:194:
+					address_family[i].u, address_family[i].s,

-:871: ERROR:SPACING: space required before the open parenthesis '('
#871: FILE: tests/net/socket/socketpair/src/main.c:260:
+	for(size_t i = 0; i < 2; ++i) {

-:874: WARNING:LONG_LINE_STRING: line over 80 characters
#874: FILE: tests/net/socket/socketpair/src/main.c:263:
+		zassert_equal(res, -1, "bind should fail on a socketpair endpoint");

-:875: WARNING:LONG_LINE_STRING: line over 80 characters
#875: FILE: tests/net/socket/socketpair/src/main.c:264:
+		zassert_equal(errno, EISCONN, "bind should set errno to EISCONN");

-:878: WARNING:LONG_LINE_STRING: line over 80 characters
#878: FILE: tests/net/socket/socketpair/src/main.c:267:
+		zassert_equal(res, -1, "connect should fail on a socketpair endpoint");

-:879: WARNING:LONG_LINE_STRING: line over 80 characters
#879: FILE: tests/net/socket/socketpair/src/main.c:268:
+		zassert_equal(errno, EISCONN, "connect should set errno to EISCONN");

-:882: WARNING:LONG_LINE_STRING: line over 80 characters
#882: FILE: tests/net/socket/socketpair/src/main.c:271:
+		zassert_equal(res, -1, "listen should fail on a socketpair endpoint");

-:883: WARNING:LONG_LINE_STRING: line over 80 characters
#883: FILE: tests/net/socket/socketpair/src/main.c:272:
+		zassert_equal(errno, EINVAL, "listen should set errno to EINVAL");

-:886: WARNING:LONG_LINE_STRING: line over 80 characters
#886: FILE: tests/net/socket/socketpair/src/main.c:275:
+		zassert_equal(res, -1, "accept should fail on a socketpair endpoint");

-:887: WARNING:LONG_LINE_STRING: line over 80 characters
#887: FILE: tests/net/socket/socketpair/src/main.c:276:
+		zassert_equal(errno, EOPNOTSUPP, "accept should set errno to EOPNOTSUPP");

-:897: ERROR:OPEN_BRACE: open brace '{' following function declarations go on the next line
#897: FILE: tests/net/socket/socketpair/src/main.c:286:
+void test_socket_socketpair_select(void) {

-:901: ERROR:OPEN_BRACE: open brace '{' following function declarations go on the next line
#901: FILE: tests/net/socket/socketpair/src/main.c:290:
+void test_socket_socketpair_poll(void) {

- total: 21 errors, 36 warnings, 871 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

Your patch has style problems, please review.

NOTE: Ignored message types: AVOID_EXTERNS BRACES CONFIG_EXPERIMENTAL CONST_STRUCT DATE_TIME FILE_PATH_CHANGES MINMAX NETWORKING_BLOCK_COMMENT_STYLE PRINTK_WITHOUT_KERN_LEVEL SPLIT_STRING VOLATILE

NOTE: If any of the errors are false positives, please report
      them to the maintainers.

Gitlint issues

Commit a9eb4efd6f:
1: UC2 Body does not contain a 'Signed-off-by:' line
1: UC3 Title does not follow [subsystem]: [subject] (and should not start with literal subsys:): "stashing"
1: UC6 Body has no content, should at least have 1 line.
3: B6 Body message is missing

Commit 83c433af0c:
1: UC2 Body does not contain a 'Signed-off-by:' line
1: UC3 Title does not follow [subsystem]: [subject] (and should not start with literal subsys:): "write(2), read(2), send(2), recv(2), sendto(2), recvfrom(2) working"
3: UC4 Line exceeds max length (74>72): "Except write / read need to be macro'ed out to send / recv due to Zephyr's"
9: UC4 Line exceeds max length (74>72): "same error that I encountered before when there was an assertion verifying"
46: UC4 Line exceeds max length (94>72): " #1 k_spin_lock (l=0x20000bf4 <kheap_poolheap.heap_mem_pool+104>) at ../include/spinlock.h:41"
47: UC4 Line exceeds max length (309>72): " #2 z_pipe_put_internal (pipe=0x20000be0 <kheap_poolheap.heap_mem_pool+84>, async_desc=0x0 <crc32_ieee>, data=0x2879c "Hello, socketpair(2) world!", bytes_to_write=27, bytes_written=0x20001e90 <ztest_thread_stack+648>, min_xfer=0, timeout=0) at /home/cfriedt/workspace/zephyrproject/zephyr/kernel/pipes.c:449"
48: UC4 Line exceeds max length (262>72): " #3 0x000261da in z_impl_k_pipe_put (pipe=0x20000be0 <kheap_poolheap.heap_mem_pool+84>, data=0x2879c, bytes_to_write=27, bytes_written=0x20001e90 <ztest_thread_stack+648>, min_xfer=0, timeout=0) at /home/cfriedt/workspace/zephyrproject/zephyr/kernel/pipes.c:741"
49: UC4 Line exceeds max length (238>72): " #4 0x0001d24e in k_pipe_put (pipe=0x20000be0 <kheap_poolheap.heap_mem_pool+84>, data=0x2879c, bytes_to_write=27, bytes_written=0x20001e90 <ztest_thread_stack+648>, min_xfer=0, timeout=0) at zephyr/include/generated/syscalls/kernel.h:908"
50: UC4 Line exceeds max length (195>72): " #5 0x0000808a in spair_write (obj=0x20000bec <kheap_poolheap.heap_mem_pool+96>, buffer=0x2879c, count=27) at /home/cfriedt/workspace/zephyrproject/zephyr/subsys/net/lib/sockets/socketpair.c:217"
51: UC4 Line exceeds max length (239>72): " #6 0x0001d442 in spair_sendto (obj=0x20000bec <kheap_poolheap.heap_mem_pool+96>, buf=0x2879c, len=27, flags=0, dest_addr=0x0 <crc32_ieee>, addrlen=0) at /home/cfriedt/workspace/zephyrproject/zephyr/subsys/net/lib/sockets/socketpair.c:294"
52: UC4 Line exceeds max length (201>72): " #7 0x0001d1d2 in z_impl_zsock_sendto (sock=1, buf=0x2879c, len=27, flags=0, dest_addr=0x0 <crc32_ieee>, addrlen=0) at /home/cfriedt/workspace/zephyrproject/zephyr/subsys/net/lib/sockets/sockets.c:566"
53: UC4 Line exceeds max length (159>72): " #8 0x0001c1f0 in zsock_sendto (sock=1, buf=0x2879c, len=27, flags=0, dest_addr=0x0 <crc32_ieee>, addrlen=0) at zephyr/include/generated/syscalls/socket.h:137"
54: UC4 Line exceeds max length (141>72): " #9 0x0001bf88 in zsock_send (sock=1, buf=0x2879c, len=27, flags=0) at /home/cfriedt/workspace/zephyrproject/zephyr/include/net/socket.h:293"
55: UC4 Line exceeds max length (233>72): " #10 0x00006c18 in happy_path_inner (family=6, family_s=0x28ee8 "AF_LOCAL", type=1, type_s=0x28efc "SOCK_STREAM", proto=0, proto_s=0x28f14 "0") at /home/cfriedt/workspace/zephyrproject/zephyr/tests/net/socket/socketpair/src/main.c:58"
56: UC4 Line exceeds max length (146>72): " #11 0x0000700c in test_socket_socketpair_happy_path () at /home/cfriedt/workspace/zephyrproject/zephyr/tests/net/socket/socketpair/src/main.c:185"

Commit 1b1b0ca382:
1: UC2 Body does not contain a 'Signed-off-by:' line
1: UC3 Title does not follow [subsystem]: [subject] (and should not start with literal subsys:): "works now with the following patch by pabigot"
7: UC4 Line exceeds max length (95>72): "@@ -135,10 +135,11 @@ void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size)"

Commit 9f6ff50e8a:
1: UC2 Body does not contain a 'Signed-off-by:' line
1: UC3 Title does not follow [subsystem]: [subject] (and should not start with literal subsys:): "still encountering spinlock error"
1: UC6 Body has no content, should at least have 1 line.
3: B6 Body message is missing

Commit 0db21791af:
1: UC2 Body does not contain a 'Signed-off-by:' line
1: UC3 Title does not follow [subsystem]: [subject] (and should not start with literal subsys:): "fixed prj.conf"
1: UC6 Body has no content, should at least have 1 line.
3: B6 Body message is missing

Commit c069a91aea:
1: UC2 Body does not contain a 'Signed-off-by:' line
1: UC3 Title does not follow [subsystem]: [subject] (and should not start with literal subsys:): "stashing"
1: UC6 Body has no content, should at least have 1 line.
3: B6 Body message is missing

Commit 2e14682ea7:
3: UC4 Line exceeds max length (78>72): "Provide an implementation for socketpair(2). Currently, this is limited to the"

Identity/Emails issues

a9eb4efd6f5102cd52cee8c16db16ad038d2c44c: author email (Christopher Friedt [email protected]) needs to match one of the signed-off-by entries.

83c433af0ce81758c47eead22f457daf5d510e88: author email (Christopher Friedt [email protected]) needs to match one of the signed-off-by entries.

1b1b0ca382ba6fd91d22c5a43b65be10a340e149: author email (Christopher Friedt [email protected]) needs to match one of the signed-off-by entries.

9f6ff50e8a7134d87c91714b7a67e1d1e8e9d772: author email (Christopher Friedt [email protected]) needs to match one of the signed-off-by entries.

0db21791afb81c1c4f70e94cbee09445f89b03e7: author email (Christopher Friedt [email protected]) needs to match one of the signed-off-by entries.

c069a91aea9748d871549eb4b86f7fe8bffbf6f5: author email (Christopher Friedt [email protected]) needs to match one of the signed-off-by entries.

Tip: The bot edits this comment instead of posting a new one, so you can check the comment's history to see earlier messages.

Copy link
Member

@jukkar jukkar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, minor nits. We also need to add more tests.

subsys/net/lib/sockets/sockets.c Outdated Show resolved Hide resolved
subsys/net/lib/sockets/sockets.c Outdated Show resolved Hide resolved
subsys/net/lib/sockets/sockets.c Outdated Show resolved Hide resolved
subsys/net/lib/sockets/sockets.c Outdated Show resolved Hide resolved
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put the tests to separate commit

tests/net/socket/socketpair/src/main.c Outdated Show resolved Hide resolved
tests/net/socket/socketpair/src/main.c Outdated Show resolved Hide resolved
tests/net/socket/socketpair/src/main.c Show resolved Hide resolved
@pfalcon pfalcon added the area: POSIX POSIX API Library label Apr 15, 2020
Copy link
Contributor

@pfalcon pfalcon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an obvious security issue with this patch, see inline comment.

subsys/net/lib/sockets/sockets.c Outdated Show resolved Hide resolved
subsys/net/lib/sockets/sockets.c Outdated Show resolved Hide resolved
include/net/net_ip.h Outdated Show resolved Hide resolved
subsys/net/lib/sockets/sockets.c Outdated Show resolved Hide resolved
subsys/net/lib/sockets/sockets.c Outdated Show resolved Hide resolved
@cfriedt
Copy link
Member Author

cfriedt commented Apr 16, 2020

If it's ok with you all (@pfalcon, @jukkar, @ceolin, @tbursztyka), I will leave this PR open and implement pipe(2) separately. I believe this review will be fairly cut & dry once pipe(2) is implemented.

Also, do you know of a simple way I can run my tests natively or in Qemu? So far all I have been contributing to is BLE.

Sorry for all of the whitespace issues - clang-format does not do the trick (#23823) but uncrustify might (#21392)

This was referenced Apr 16, 2020
@cfriedt
Copy link
Member Author

cfriedt commented Apr 18, 2020

It's probably better to not use pipe(2) for socketpair(2) since a pair of sockets form a bidirectional channel rather than a unidirectional channel. Likely, will use a pair of k_fifo. Technically, it is possible using pipe(2) but that would mean 4 file descriptors would need to be created instead of only 2.

So from that perspective, maybe it is better to just keep socketpair(2) exclusively in net and even reuse much of the existing socket machinery.

cfriedt added 6 commits April 19, 2020 16:12
Provide an implementation for socketpair(2). Currently, this is limited to the
AF_UNIX address family, SOCK_STREAM, SOCK_RAW, and protocol 0.

Fixes #24366

Signed-off-by: Christopher Friedt <[email protected]>
diff --git a/kernel/pipes.c b/kernel/pipes.c
index efd2855c5e..c6ae71555d 100644
--- a/kernel/pipes.c
+++ b/kernel/pipes.c
@@ -135,10 +135,11 @@ void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size)
        pipe->bytes_used = 0;
        pipe->read_index = 0;
        pipe->write_index = 0;
-       pipe->flags = 0;
+       pipe->lock = (struct k_spinlock){};
        z_waitq_init(&pipe->wait_q.writers);
        z_waitq_init(&pipe->wait_q.readers);
        SYS_TRACING_OBJ_INIT(k_pipe, pipe);
+       pipe->flags = 0;
        z_object_init(pipe);
 }
Except write / read need to be macro'ed out to send / recv due to Zephyr's
POSIX naming, otherwise they attempt to use write / read from stdio.

Currently only working in the sv[0] -> sv[1] direction.

If I attempt to write in the sv[1] -> sv[0] direction, I encounter the
same error that I encountered before when there was an assertion verifying
the pipe's spinlock.

Output:

ninja: Entering directory `build'
[0/1] To exit from QEMU enter: 'CTRL+a, x'[QEMU] CPU: cortex-m3
qemu-system-arm: warning: nic stellaris_enet.0 has no peer
*** Booting Zephyr OS build zephyr-v2.2.0-1661-g0cc6bdf36c5c  ***
Running test suite socket_socketpair
===================================================================
starting test - test_socket_socketpair_happy_path
calling socketpair(AF_LOCAL, SOCK_STREAM, 0, sv)
data direction: 0 -> 1
testing write(2)
testing read(2)
testing send(2)
testing recv(2)
testing sendto(2)
testing recvfrom(2)
data direction: 1 -> 0
testing write(2)
ASSERTION FAIL [z_spin_lock_valid(l)] @ ../include/spinlock.h:44
	Recursive spinlock 0x20000bf4
E: r0/a1:  0x00000004  r1/a2:  0x0000002c  r2/a3:  0x0000000a
E: r3/a4:  0x00000000 r12/ip:  0x01010101 r14/lr:  0x00017c39
E:  xpsr:  0x41000000
E: Faulting instruction address (r15/pc): 0x0001d002
E: >>> ZEPHYR FATAL ERROR 4: Kernel panic on CPU 0
E: Current thread: 0x200009c0 (unknown)
E: Halting system
qemu-system-arm: terminating on signal 2
ninja: build stopped: interrupted by user.

Stack Trace:

 #0  arch_irq_lock () at ../include/arch/arm/aarch32/asm_inline_gcc.h:56
 #1  k_spin_lock (l=0x20000bf4 <kheap_poolheap.heap_mem_pool+104>) at ../include/spinlock.h:41
 #2  z_pipe_put_internal (pipe=0x20000be0 <kheap_poolheap.heap_mem_pool+84>, async_desc=0x0 <crc32_ieee>, data=0x2879c "Hello, socketpair(2) world!", bytes_to_write=27, bytes_written=0x20001e90 <ztest_thread_stack+648>, min_xfer=0, timeout=0) at /home/cfriedt/workspace/zephyrproject/zephyr/kernel/pipes.c:449
 #3  0x000261da in z_impl_k_pipe_put (pipe=0x20000be0 <kheap_poolheap.heap_mem_pool+84>, data=0x2879c, bytes_to_write=27, bytes_written=0x20001e90 <ztest_thread_stack+648>, min_xfer=0, timeout=0) at /home/cfriedt/workspace/zephyrproject/zephyr/kernel/pipes.c:741
 #4  0x0001d24e in k_pipe_put (pipe=0x20000be0 <kheap_poolheap.heap_mem_pool+84>, data=0x2879c, bytes_to_write=27, bytes_written=0x20001e90 <ztest_thread_stack+648>, min_xfer=0, timeout=0) at zephyr/include/generated/syscalls/kernel.h:908
 #5  0x0000808a in spair_write (obj=0x20000bec <kheap_poolheap.heap_mem_pool+96>, buffer=0x2879c, count=27) at /home/cfriedt/workspace/zephyrproject/zephyr/subsys/net/lib/sockets/socketpair.c:217
 #6  0x0001d442 in spair_sendto (obj=0x20000bec <kheap_poolheap.heap_mem_pool+96>, buf=0x2879c, len=27, flags=0, dest_addr=0x0 <crc32_ieee>, addrlen=0) at /home/cfriedt/workspace/zephyrproject/zephyr/subsys/net/lib/sockets/socketpair.c:294
 #7  0x0001d1d2 in z_impl_zsock_sendto (sock=1, buf=0x2879c, len=27, flags=0, dest_addr=0x0 <crc32_ieee>, addrlen=0) at /home/cfriedt/workspace/zephyrproject/zephyr/subsys/net/lib/sockets/sockets.c:566
 #8  0x0001c1f0 in zsock_sendto (sock=1, buf=0x2879c, len=27, flags=0, dest_addr=0x0 <crc32_ieee>, addrlen=0) at zephyr/include/generated/syscalls/socket.h:137
 #9  0x0001bf88 in zsock_send (sock=1, buf=0x2879c, len=27, flags=0) at /home/cfriedt/workspace/zephyrproject/zephyr/include/net/socket.h:293
 #10 0x00006c18 in happy_path_inner (family=6, family_s=0x28ee8 "AF_LOCAL", type=1, type_s=0x28efc "SOCK_STREAM", proto=0, proto_s=0x28f14 "0") at /home/cfriedt/workspace/zephyrproject/zephyr/tests/net/socket/socketpair/src/main.c:58
 #11 0x0000700c in test_socket_socketpair_happy_path () at /home/cfriedt/workspace/zephyrproject/zephyr/tests/net/socket/socketpair/src/main.c:185
@pfalcon pfalcon added this to the v2.3.0 milestone Apr 22, 2020
@pfalcon
Copy link
Contributor

pfalcon commented Apr 22, 2020

@cfriedt: Same comment as for the other PR - please deal with checkstyle issues.

(Also, to set expectations right - there was a talk about release readiness meeting. Which reminds that we may be approaching a pre-release freeze. Whatever isn't merged before it, may need to wait a month or so until next devel window is open. Surely feel free to work at you pace. What I wanted to say is that if you don't get a prompt review (once CI passes), don't hesitate to ping me. Thanks.)

@cfriedt
Copy link
Member Author

cfriedt commented Apr 22, 2020

Going to close the PR for the next while. Will reopen soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: Networking area: POSIX POSIX API Library area: Tests Issues related to a particular existing or missing test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

syscall for socketpair(2)
5 participants