[PATCH v1] score:fix smp priority affinity scheduler yeild problem

2022-05-11 Thread tianye
From: Tian Ye 

---
 .../rtems/score/schedulerpriorityaffinitysmp.h |  7 ++-
 .../score/src/schedulerpriorityaffinitysmp.c   | 18 ++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/cpukit/include/rtems/score/schedulerpriorityaffinitysmp.h 
b/cpukit/include/rtems/score/schedulerpriorityaffinitysmp.h
index 498ffcb7aa..bac3ad786e 100644
--- a/cpukit/include/rtems/score/schedulerpriorityaffinitysmp.h
+++ b/cpukit/include/rtems/score/schedulerpriorityaffinitysmp.h
@@ -54,7 +54,7 @@ extern "C" {
   { \
 _Scheduler_priority_SMP_Initialize, \
 _Scheduler_default_Schedule, \
-_Scheduler_priority_SMP_Yield, \
+_Scheduler_priority_affinity_SMP_Yield, \
 _Scheduler_priority_affinity_SMP_Block, \
 _Scheduler_priority_affinity_SMP_Unblock, \
 _Scheduler_priority_affinity_SMP_Update_priority, \
@@ -107,6 +107,11 @@ void _Scheduler_priority_affinity_SMP_Block(
   Scheduler_Node  *node
 );
 
+void _Scheduler_priority_affinity_SMP_Yield(
+  const Scheduler_Control *scheduler,
+  Thread_Control  *thread,
+  Scheduler_Node  *node
+);
 /**
  * @brief Unblocks a thread.
  *
diff --git a/cpukit/score/src/schedulerpriorityaffinitysmp.c 
b/cpukit/score/src/schedulerpriorityaffinitysmp.c
index 86ef7bf83e..d59917340d 100644
--- a/cpukit/score/src/schedulerpriorityaffinitysmp.c
+++ b/cpukit/score/src/schedulerpriorityaffinitysmp.c
@@ -576,3 +576,21 @@ bool _Scheduler_priority_affinity_SMP_Set_affinity(
 
   return true;
 }
+
+void _Scheduler_priority_affinity_SMP_Yield(
+  const Scheduler_Control *scheduler,
+  Thread_Control  *thread,
+  Scheduler_Node  *node
+)
+{
+  Scheduler_Context *context = _Scheduler_Get_context( scheduler );
+
+  _Scheduler_SMP_Yield(
+context,
+thread,
+node,
+_Scheduler_priority_SMP_Extract_from_ready,
+_Scheduler_priority_affinity_SMP_Enqueue,
+_Scheduler_priority_affinity_SMP_Enqueue_scheduled
+  );
+}
-- 
2.25.1

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


Re: [PATCH 0/8] tcpdump: Fix dumping to file and reading from file

2022-05-11 Thread Chris Johns
OK and thanks. The destructor is a nice addition.

I still think running tcpdump on rtems is fun to watch apart from being useful
when things do not work.

Chris

On 11/5/2022 4:56 pm, Sebastian Huber wrote:
> This patch is for the 5-freebsd-12, 6-freebsd-12, and master branches of
> libbsd.
> 
> Sebastian Huber (8):
>   tcpdump01: New test
>   Add program destructor support
>   tcpdump: Make loop monitor cooperative
>   tcpdump: Use rtems_task_exit()
>   tcpdump: Close pcap dumper at program exit
>   tcpdump: No loop monitor if reading from file
>   tcpdump: Ensure loop monitor termination
>   tcpdump: Do not use signals and chroot
> 
>  freebsd/contrib/tcpdump/tcpdump.c|  95 --
>  libbsd.py|   1 +
>  rtemsbsd/include/machine/rtems-bsd-program.h |   6 +
>  rtemsbsd/rtems/program-internal.h|   7 +
>  rtemsbsd/rtems/rtems-program.c   |  51 
>  testsuite/tcpdump01/test_main.c  | 292 +++
>  6 files changed, 428 insertions(+), 24 deletions(-)
>  create mode 100644 testsuite/tcpdump01/test_main.c
> 
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


Re: [PATCH 3/8] tcpdump: Make loop monitor cooperative

2022-05-11 Thread Sebastian Huber

On 11/05/2022 16:56, Joel Sherrill wrote:
Should these be inside an ifdef rtems conditional? Isn't this code 
originally from FreeBSD?


This code is not from FreeBSD.

--
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.hu...@embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

Re: [PATCH 3/8] tcpdump: Make loop monitor cooperative

2022-05-11 Thread Joel Sherrill
Should these be inside an ifdef rtems conditional? Isn't this code
originally from FreeBSD?

--joel

On Wed, May 11, 2022 at 1:56 AM Sebastian Huber <
sebastian.hu...@embedded-brains.de> wrote:

> This helps a bit if the fgetc() is non-blocking.
>
> Update #4650.
> ---
>  freebsd/contrib/tcpdump/tcpdump.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/freebsd/contrib/tcpdump/tcpdump.c
> b/freebsd/contrib/tcpdump/tcpdump.c
> index b1e7f0d1..74c878a3 100644
> --- a/freebsd/contrib/tcpdump/tcpdump.c
> +++ b/freebsd/contrib/tcpdump/tcpdump.c
> @@ -139,6 +139,7 @@ The Regents of the University of California.  All
> rights reserved.\n";
>  #include 
>  #include 
>  #include 
> +#include 
>  #include 
>  #include 
>  #define setpriority(a, b, c)
> @@ -1224,6 +1225,8 @@ pcap_loop_monitor(rtems_task_argument arg)
> pcap_breakloop(pd);
> break;
> }
> +
> +   sched_yield();
> }
>
> rtems_task_delete(RTEMS_SELF);
> --
> 2.35.3
>
> ___
> devel mailing list
> devel@rtems.org
> http://lists.rtems.org/mailman/listinfo/devel
>
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


Re: AW: [PATCH v3 00/11] ENABLE PPS API in RTEMS6

2022-05-11 Thread Sebastian Huber

On 11/05/2022 13:53, gabriel.moy...@dlr.de wrote:

Is there any feedback for improvement? Or are these commits ready to be pushed?


Sorry for the delay, but I was busy with other things in the last days. 
I will have a look at the patch set in the next days. In general, it 
looks good.


--
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.hu...@embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel

AW: [PATCH v3 00/11] ENABLE PPS API in RTEMS6

2022-05-11 Thread Gabriel.Moyano
> This is the 3rd version of the patches for enabling the PPS API in RTEMS6.
> It contains the changes suggested by Sebastian from yesterday (03/05/2022).
> 
> Gabriel Moyano (11):
>   kern_ntptime.c: Disable freebsd features
>   kern_ntptime.c: Add lmax() qmin() definitions
>   kern_tc.c: Add atomic dependencies required by the PPS API
>   kern_tc.c: Replace FREEBSD event mechanism by adding pointers to
> function
>   timecounter.h: Rename tc_getfrequency() to
> _Timecounter_Get_frequency()
>   kern_tc.c: Add definitions required by PPS API
>   kern_tc.c: Enable PPS API support
>   kern_ntptime.c: Add define in order to remove warning
>   timepps.h: PPS_SYNC defined by default
>   timecounter.h: Add _Timecounter_Discipline()
>   testsuites/sptests: Add sppps01 test
> 
>  cpukit/include/rtems/score/timecounter.h  |  25 
>  cpukit/include/sys/timepps.h  |  32 
>  cpukit/include/sys/timetc.h   |   3 +
>  cpukit/score/src/kern_ntptime.c   |  19 +--
>  cpukit/score/src/kern_tc.c|  54 ++-
>  spec/build/testsuites/sptests/grp.yml |   2 +
>  spec/build/testsuites/sptests/sppps01.yml |  19 +++
>  testsuites/sptests/sppps01/init.c | 173 ++
>  8 files changed, 312 insertions(+), 15 deletions(-)  create mode 100644 
> spec/build/testsuites/sptests/sppps01.yml
>  create mode 100644 testsuites/sptests/sppps01/init.c

Is there any feedback for improvement? Or are these commits ready to be pushed?
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


[PATCH 8/8] tcpdump: Do not use signals and chroot

2022-05-11 Thread Sebastian Huber
Update #4650.
---
 freebsd/contrib/tcpdump/tcpdump.c | 22 --
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/freebsd/contrib/tcpdump/tcpdump.c 
b/freebsd/contrib/tcpdump/tcpdump.c
index 73e2e844..5b658412 100644
--- a/freebsd/contrib/tcpdump/tcpdump.c
+++ b/freebsd/contrib/tcpdump/tcpdump.c
@@ -207,8 +207,10 @@ cap_channel_t *capdns;
 static void error(FORMAT_STRING(const char *), ...) NORETURN PRINTFLIKE(1, 2);
 static void warning(FORMAT_STRING(const char *), ...) PRINTFLIKE(1, 2);
 static void exit_tcpdump(int) NORETURN;
+#ifndef __rtems__
 static RETSIGTYPE cleanup(int);
 static RETSIGTYPE child_cleanup(int);
+#endif /* __rtems__ */
 static void print_version(void);
 static void print_usage(void);
 static void show_tstamp_types_and_exit(pcap_t *, const char *device) NORETURN;
@@ -220,6 +222,7 @@ static void show_devices_and_exit (void) NORETURN;
 static void print_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
 static void dump_packet_and_trunc(u_char *, const struct pcap_pkthdr *, const 
u_char *);
 static void dump_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
+#ifndef __rtems__
 static void droproot(const char *, const char *);
 
 #ifdef SIGNAL_REQ_INFO
@@ -233,6 +236,7 @@ RETSIGTYPE requestinfo(int);
 #elif defined(HAVE_ALARM)
   static void verbose_stats_dump(int sig);
 #endif
+#endif /* __rtems__ */
 
 static void info(int);
 static u_int packets_captured;
@@ -624,6 +628,7 @@ static const struct option longopts[] = {
{ NULL, 0, NULL, 0 }
 };
 
+#ifndef __rtems__
 #ifndef _WIN32
 /* Drop root privileges and chroot if necessary */
 static void
@@ -655,7 +660,6 @@ droproot(const char *username, const char *chroot_dir)
fprintf(stderr, "dropped privs to %s\n", 
username);
}
 #else
-#ifndef __rtems__
if (initgroups(pw->pw_name, pw->pw_gid) != 0 ||
setgid(pw->pw_gid) != 0 || setuid(pw->pw_uid) != 0) {
fprintf(stderr, "%s: Couldn't change to '%.32s' uid=%lu 
gid=%lu: %s\n",
@@ -668,7 +672,6 @@ droproot(const char *username, const char *chroot_dir)
else {
fprintf(stderr, "dropped privs to %s\n", username);
}
-#endif /* __rtems__ */
 #endif /* HAVE_LIBCAP_NG */
}
else {
@@ -690,6 +693,7 @@ droproot(const char *username, const char *chroot_dir)
 
 }
 #endif /* _WIN32 */
+#endif /* __rtems__ */
 
 static int
 getWflagChars(int x)
@@ -1294,15 +1298,19 @@ main(int argc, char **argv)
int dlt;
const char *dlt_name;
struct bpf_program fcode;
+#ifndef __rtems__
 #ifndef _WIN32
RETSIGTYPE (*oldhandler)(int);
 #endif
+#endif /* __rtems__ */
struct dump_info dumpinfo;
u_char *pcap_userdata;
char ebuf[PCAP_ERRBUF_SIZE];
char VFileLine[PATH_MAX + 1];
+#ifndef __rtems__
char *username = NULL;
char *chroot_dir = NULL;
+#endif /* __rtems__ */
char *ret = NULL;
char *end;
 #ifdef HAVE_PCAP_FINDALLDEVS
@@ -1674,9 +1682,11 @@ main(int argc, char **argv)
zflag = optarg;
break;
 
+#ifndef __rtems__
case 'Z':
username = optarg;
break;
+#endif /* __rtems__ */
 
case '#':
ndo->ndo_packet_number = 1;
@@ -1973,6 +1983,7 @@ main(int argc, char **argv)
 
init_print(ndo, localnet, netmask, timezone_offset);
 
+#ifndef __rtems__
 #ifndef _WIN32
(void)setsignal(SIGPIPE, cleanup);
(void)setsignal(SIGTERM, cleanup);
@@ -2040,6 +2051,7 @@ main(int argc, char **argv)
 
}
 #endif /* _WIN32 */
+#endif /* __rtems__ */
 
if (pcap_setfilter(pd, ) < 0)
error("%s", pcap_geterr(pd));
@@ -2147,6 +2159,7 @@ main(int argc, char **argv)
pcap_userdata = (u_char *)ndo;
}
 
+#ifndef __rtems__
 #ifdef SIGNAL_REQ_INFO
/*
 * We can't get statistics when reading from a file rather
@@ -2171,6 +2184,7 @@ main(int argc, char **argv)
alarm(1);
 #endif
}
+#endif /* __rtems__ */
 
if (RFileName == NULL) {
/*
@@ -2344,6 +2358,7 @@ main(int argc, char **argv)
exit_tcpdump(status == -1 ? 1 : 0);
 }
 
+#ifndef __rtems__
 /* make a clean exit on interrupts */
 static RETSIGTYPE
 cleanup(int signo _U_)
@@ -2396,6 +2411,7 @@ child_cleanup(int signo _U_)
   wait(NULL);
 }
 #endif /* HAVE_FORK && HAVE_VFORK */
+#endif /* __rtems__ */
 
 static void
 info(register int verbose)
@@ -2756,6 +2772,7 @@ print_packet(u_char *user, const struct pcap_pkthdr *h, 
const u_char *sp)
char Wpcap_version[]="3.1";
 #endif
 
+#ifndef __rtems__
 #ifdef SIGNAL_REQ_INFO
 RETSIGTYPE requestinfo(int signo _U_)
 {
@@ -2784,6 +2801,7 @@ static void verbose_stats_dump(int sig _U_)
alarm(1);
 }
 #endif
+#endif /* __rtems__ */
 
 

[PATCH 6/8] tcpdump: No loop monitor if reading from file

2022-05-11 Thread Sebastian Huber
Update #4650.
---
 freebsd/contrib/tcpdump/tcpdump.c | 16 
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/freebsd/contrib/tcpdump/tcpdump.c 
b/freebsd/contrib/tcpdump/tcpdump.c
index a02fa184..db5c9221 100644
--- a/freebsd/contrib/tcpdump/tcpdump.c
+++ b/freebsd/contrib/tcpdump/tcpdump.c
@@ -1232,8 +1232,8 @@ pcap_loop_monitor(rtems_task_argument arg)
rtems_task_exit();
 }
 
-static int
-pcap_loop_wrapper(pcap_t *pd, int cnt, pcap_handler cb, u_char *ud)
+static void
+pcap_create_loop_monitor(pcap_t *pd)
 {
rtems_status_code sc;
rtems_task_priority priority;
@@ -1248,9 +1248,8 @@ pcap_loop_wrapper(pcap_t *pd, int cnt, pcap_handler cb, 
u_char *ud)
RTEMS_MINIMUM_STACK_SIZE, RTEMS_DEFAULT_MODES,
RTEMS_DEFAULT_ATTRIBUTES, );
if (sc != RTEMS_SUCCESSFUL) {
-   fprintf(stderr, "tcpdump: cannot create helper thread: %s\n",
+   error("cannot create pcap loop monitor thread: %s\n",
rtems_status_text(sc));
-   return (-1);
}
 
fprintf(stdout, "tcpdump: press  or 'q' or 'Q' to quit\n");
@@ -1264,12 +1263,8 @@ pcap_loop_wrapper(pcap_t *pd, int cnt, pcap_handler cb, 
u_char *ud)
 
sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
assert(sc == RTEMS_SUCCESSFUL);
-
-   return (pcap_loop(pd, cnt, cb, ud));
 }
 
-#definepcap_loop(pd, cnt, cb, ud) pcap_loop_wrapper(pd, cnt, cb, ud)
-
 static void
 destroy_pcap_dumper(void *arg)
 {
@@ -2211,6 +2206,11 @@ main(int argc, char **argv)
 #endif /* HAVE_CAPSICUM */
 
do {
+#ifdef __rtems__
+   if (RFileName == NULL) {
+   pcap_create_loop_monitor(pd);
+   }
+#endif /* __rtems__ */
status = pcap_loop(pd, cnt, callback, pcap_userdata);
if (WFileName == NULL) {
/*
-- 
2.35.3

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


[PATCH 7/8] tcpdump: Ensure loop monitor termination

2022-05-11 Thread Sebastian Huber
Update #4650.
---
 freebsd/contrib/tcpdump/tcpdump.c | 44 ---
 1 file changed, 29 insertions(+), 15 deletions(-)

diff --git a/freebsd/contrib/tcpdump/tcpdump.c 
b/freebsd/contrib/tcpdump/tcpdump.c
index db5c9221..73e2e844 100644
--- a/freebsd/contrib/tcpdump/tcpdump.c
+++ b/freebsd/contrib/tcpdump/tcpdump.c
@@ -1197,26 +1197,22 @@ typedef struct {
FILE *in;
pcap_t *pd;
rtems_id master;
+   bool terminate;
 } pcap_loop_context;
 
 static void
 pcap_loop_monitor(rtems_task_argument arg)
 {
-   pcap_loop_context *ctx;
+   const pcap_loop_context *ctx;
FILE *in;
pcap_t *pd;
-   rtems_id master;
rtems_status_code sc;
 
-   ctx = (pcap_loop_context *)arg;
+   ctx = (const pcap_loop_context *)arg;
in = ctx->in;
pd = ctx->pd;
-   master = ctx->master;
-
-   sc = rtems_event_transient_send(master);
-   assert(sc == RTEMS_SUCCESSFUL);
 
-   while (true) {
+   while (!ctx->terminate) {
int c;
 
c = fgetc(in);
@@ -1229,16 +1225,18 @@ pcap_loop_monitor(rtems_task_argument arg)
sched_yield();
}
 
+   sc = rtems_event_transient_send(ctx->master);
+   assert(sc == RTEMS_SUCCESSFUL);
+
rtems_task_exit();
 }
 
 static void
-pcap_create_loop_monitor(pcap_t *pd)
+pcap_create_loop_monitor(pcap_loop_context *ctx, pcap_t *pd)
 {
rtems_status_code sc;
rtems_task_priority priority;
rtems_id id;
-   pcap_loop_context ctx;
 
sc = rtems_task_set_priority(RTEMS_SELF, RTEMS_CURRENT_PRIORITY,
);
@@ -1254,12 +1252,21 @@ pcap_create_loop_monitor(pcap_t *pd)
 
fprintf(stdout, "tcpdump: press  or 'q' or 'Q' to quit\n");
 
-   ctx.in = stdin;
-   ctx.pd = pd;
-   ctx.master = rtems_task_self();
+   ctx->in = stdin;
+   ctx->pd = pd;
+   ctx->master = rtems_task_self();
+   ctx->terminate = false;
sc = rtems_task_start(id, pcap_loop_monitor,
-   (rtems_task_argument));
+   (rtems_task_argument)ctx);
assert(sc == RTEMS_SUCCESSFUL);
+}
+
+static void
+pcap_terminate_loop_monitor(pcap_loop_context *ctx)
+{
+   rtems_status_code sc;
+
+   ctx->terminate = true;
 
sc = rtems_event_transient_receive(RTEMS_WAIT, RTEMS_NO_TIMEOUT);
assert(sc == RTEMS_SUCCESSFUL);
@@ -2207,11 +2214,18 @@ main(int argc, char **argv)
 
do {
 #ifdef __rtems__
+   pcap_loop_context ctx;
+
if (RFileName == NULL) {
-   pcap_create_loop_monitor(pd);
+   pcap_create_loop_monitor(, pd);
}
 #endif /* __rtems__ */
status = pcap_loop(pd, cnt, callback, pcap_userdata);
+#ifdef __rtems__
+   if (RFileName == NULL) {
+   pcap_terminate_loop_monitor();
+   }
+#endif /* __rtems__ */
if (WFileName == NULL) {
/*
 * We're printing packets.  Flush the printed output,
-- 
2.35.3

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


[PATCH 5/8] tcpdump: Close pcap dumper at program exit

2022-05-11 Thread Sebastian Huber
Update #4650.
---
 freebsd/contrib/tcpdump/tcpdump.c | 13 +
 1 file changed, 13 insertions(+)

diff --git a/freebsd/contrib/tcpdump/tcpdump.c 
b/freebsd/contrib/tcpdump/tcpdump.c
index 0438dd37..a02fa184 100644
--- a/freebsd/contrib/tcpdump/tcpdump.c
+++ b/freebsd/contrib/tcpdump/tcpdump.c
@@ -1269,6 +1269,13 @@ pcap_loop_wrapper(pcap_t *pd, int cnt, pcap_handler cb, 
u_char *ud)
 }
 
 #definepcap_loop(pd, cnt, cb, ud) pcap_loop_wrapper(pd, cnt, cb, ud)
+
+static void
+destroy_pcap_dumper(void *arg)
+{
+
+   pcap_dump_close(arg);
+}
 #endif /* __rtems__ */
 int
 #ifndef __rtems__
@@ -2125,6 +2132,12 @@ main(int argc, char **argv)
if (Uflag)
pcap_dump_flush(p);
 #endif
+#ifdef __rtems__
+   if (rtems_bsd_program_add_destructor(destroy_pcap_dumper, p) ==
+   NULL) {
+   error("cannot add destructor");
+   }
+#endif /* __rtems__ */
} else {
dlt = pcap_datalink(pd);
ndo->ndo_if_printer = get_if_printer(ndo, dlt);
-- 
2.35.3

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


[PATCH 1/8] tcpdump01: New test

2022-05-11 Thread Sebastian Huber
Update #4650.
---
 libbsd.py   |   1 +
 testsuite/tcpdump01/test_main.c | 292 
 2 files changed, 293 insertions(+)
 create mode 100644 testsuite/tcpdump01/test_main.c

diff --git a/libbsd.py b/libbsd.py
index 2d3b718d..983f41a1 100644
--- a/libbsd.py
+++ b/libbsd.py
@@ -5588,6 +5588,7 @@ class tests(builder.Module):
 self.addTest(mm.generator['test']('ipsec01', ['test_main']))
 self.addTest(mm.generator['test']('openssl01', ['test_main']))
 self.addTest(mm.generator['test']('openssl02', ['test_main']))
+self.addTest(mm.generator['test']('tcpdump01', ['test_main']))
 
 def load(mm):
 
diff --git a/testsuite/tcpdump01/test_main.c b/testsuite/tcpdump01/test_main.c
new file mode 100644
index ..0f31cdde
--- /dev/null
+++ b/testsuite/tcpdump01/test_main.c
@@ -0,0 +1,292 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+
+/**
+ * @file
+ *
+ * @brief Tests the tcpdump command.
+ */
+
+/*
+ * Copyright (C) 2022 embedded brains GmbH
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+#include 
+
+#define TEST_NAME "LIBBSD TCPDUMP 1"
+
+typedef struct {
+   rtems_id main_id;
+   int sp[2];
+} test_context;
+
+static test_context test_instance;
+
+static void
+epoch_cleanup(void)
+{
+   rtems_status_code sc;
+
+   sc = rtems_task_wake_after(CK_EPOCH_LENGTH);
+   assert(sc == RTEMS_SUCCESSFUL);
+}
+
+static void
+init_addr(struct sockaddr_in *addr)
+{
+   int ok;
+
+   memset(addr, 0, sizeof(*addr));
+   addr->sin_family = AF_INET;
+   addr->sin_port = htons(1234);
+   ok = inet_aton("127.0.0.1", >sin_addr);
+   assert(ok != 0);
+}
+
+static rtems_id
+start_task(rtems_task_entry entry, void *arg)
+{
+   rtems_task_priority prio;
+   rtems_id id;
+   rtems_status_code sc;
+
+   sc = rtems_task_set_priority(RTEMS_SELF, RTEMS_CURRENT_PRIORITY, );
+   assert(sc == RTEMS_SUCCESSFUL);
+
+   sc = rtems_task_create(rtems_build_name('T', 'E', 'S', 'T' ), prio - 1,
+   32 * 1024, RTEMS_DEFAULT_MODES, RTEMS_DEFAULT_ATTRIBUTES, );
+   assert(sc == RTEMS_SUCCESSFUL);
+
+   sc = rtems_task_start(id, entry, (rtems_task_argument)arg);
+   assert(sc == RTEMS_SUCCESSFUL);
+
+   return id;
+}
+
+static void
+test_tcpdump_help(void)
+{
+   char *argv[] = {
+   "tcpdump",
+   "-h",
+   NULL
+   };
+   rtems_resource_snapshot snapshot;
+   int exit_code;
+
+   rtems_resource_snapshot_take();
+
+   exit_code = rtems_bsd_command_tcpdump(RTEMS_BSD_ARGC(argv), argv);
+   assert(exit_code == 0);
+
+   assert(rtems_resource_snapshot_check());
+}
+
+static const char raw_packets_file[] = "packets.bin";
+
+static void
+run_tcpdump_write_raw_packets(rtems_task_argument arg)
+{
+   test_context *ctx;
+   FILE *file;
+   FILE *saved_stdin;
+   FILE *saved_stdout;
+   FILE *saved_stderr;
+   int rv;
+   char *argv[] = {
+   "tcpdump",
+   "-n",
+   "-i",
+   "lo0",
+   "-w",
+   RTEMS_DECONST(char *, raw_packets_file),
+   NULL
+   };
+   int exit_code;
+   rtems_status_code sc;
+
+   ctx = (test_context *)arg;
+
+   saved_stdin = stdin;
+   saved_stdout = stdout;
+   saved_stderr = stderr;
+
+   file = fdopen(ctx->sp[1], "r+");
+   assert(file != NULL);
+
+   stdin = file;
+   stdout = 

[PATCH 4/8] tcpdump: Use rtems_task_exit()

2022-05-11 Thread Sebastian Huber
Update #4650.
---
 freebsd/contrib/tcpdump/tcpdump.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/freebsd/contrib/tcpdump/tcpdump.c 
b/freebsd/contrib/tcpdump/tcpdump.c
index 74c878a3..0438dd37 100644
--- a/freebsd/contrib/tcpdump/tcpdump.c
+++ b/freebsd/contrib/tcpdump/tcpdump.c
@@ -1229,8 +1229,7 @@ pcap_loop_monitor(rtems_task_argument arg)
sched_yield();
}
 
-   rtems_task_delete(RTEMS_SELF);
-   assert(0);
+   rtems_task_exit();
 }
 
 static int
-- 
2.35.3

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


[PATCH 2/8] Add program destructor support

2022-05-11 Thread Sebastian Huber
Update #4650.
---
 rtemsbsd/include/machine/rtems-bsd-program.h |  6 +++
 rtemsbsd/rtems/program-internal.h|  7 +++
 rtemsbsd/rtems/rtems-program.c   | 51 
 3 files changed, 64 insertions(+)

diff --git a/rtemsbsd/include/machine/rtems-bsd-program.h 
b/rtemsbsd/include/machine/rtems-bsd-program.h
index dacd0d69..3062c1a2 100644
--- a/rtemsbsd/include/machine/rtems-bsd-program.h
+++ b/rtemsbsd/include/machine/rtems-bsd-program.h
@@ -60,6 +60,12 @@ rtems_bsd_program_call_main_with_data_restore(const char 
*name,
 int (*main)(int, char **), int argc, char **argv,
 void *data_buf, const size_t data_size);
 
+void *
+rtems_bsd_program_add_destructor(void (*destructor)(void *), void *arg);
+
+void
+rtems_bsd_program_remove_destructor(void *cookie, bool call);
+
 void
 rtems_bsd_program_exit(int exit_code) __dead2;
 
diff --git a/rtemsbsd/rtems/program-internal.h 
b/rtemsbsd/rtems/program-internal.h
index da817130..2104c064 100644
--- a/rtemsbsd/rtems/program-internal.h
+++ b/rtemsbsd/rtems/program-internal.h
@@ -60,6 +60,12 @@ struct program_allocmem_item {
LIST_ENTRY(program_allocmem_item) entries;
 };
 
+struct program_destructor {
+   void(*destructor)(void *);
+   void*arg;
+   LIST_ENTRY(program_destructor) link;
+};
+
 struct rtems_bsd_program_control {
void *context;
int exit_code;
@@ -68,6 +74,7 @@ struct rtems_bsd_program_control {
LIST_HEAD(, program_fd_item) open_fd;
LIST_HEAD(, program_file_item) open_file;
LIST_HEAD(, program_allocmem_item) allocated_mem;
+   LIST_HEAD(, program_destructor) destructors;
 };
 
 struct rtems_bsd_program_control *rtems_bsd_program_get_control_or_null(void);
diff --git a/rtemsbsd/rtems/rtems-program.c b/rtemsbsd/rtems/rtems-program.c
index 370609d4..4d2ce6ef 100644
--- a/rtemsbsd/rtems/rtems-program.c
+++ b/rtemsbsd/rtems/rtems-program.c
@@ -224,6 +224,18 @@ allocmem_free_all(struct rtems_bsd_program_control 
*prog_ctrl)
}
 }
 
+static void
+call_destructors(struct rtems_bsd_program_control *prog_ctrl)
+{
+   struct program_destructor *node;
+   struct program_destructor *tmp;
+
+   LIST_FOREACH_SAFE(node, _ctrl->destructors, link, tmp) {
+   (*node->destructor)(node->arg);
+   free(node);
+   }
+}
+
 int
 rtems_bsd_program_call(const char *name, int (*prog)(void *), void *context)
 {
@@ -251,6 +263,7 @@ rtems_bsd_program_call(const char *name, int (*prog)(void 
*), void *context)
LIST_INIT(_ctrl->open_fd);
LIST_INIT(_ctrl->open_file);
LIST_INIT(_ctrl->allocated_mem);
+   LIST_INIT(_ctrl->destructors);
 
if (setjmp(prog_ctrl->return_context) == 0) {
exit_code = (*prog)(context);
@@ -262,10 +275,48 @@ rtems_bsd_program_call(const char *name, int (*prog)(void 
*), void *context)
fd_close_all(prog_ctrl);
file_close_all(prog_ctrl);
allocmem_free_all(prog_ctrl);
+   call_destructors(prog_ctrl);
free(prog_ctrl);
return (exit_code);
 }
 
+void *
+rtems_bsd_program_add_destructor(void (*destructor)(void *), void *arg)
+{
+   struct rtems_bsd_program_control *prog_ctrl;
+   struct program_destructor *node;
+
+   prog_ctrl = rtems_bsd_program_get_control_or_null();
+   if (prog_ctrl == NULL) {
+   return (NULL);
+   }
+
+   node = malloc(sizeof(*node));
+   if (node == NULL) {
+   return (NULL);
+   }
+
+   node->destructor = destructor;
+   node->arg = arg;
+   LIST_INSERT_HEAD(_ctrl->destructors, node, link);
+   return (node);
+}
+
+void
+rtems_bsd_program_remove_destructor(void *cookie, bool call)
+{
+   struct program_destructor *node;
+
+   node = cookie;
+   LIST_REMOVE(node, link);
+
+   if (call) {
+   (*node->destructor)(node->arg);
+   }
+
+   free(node);
+}
+
 void
 rtems_bsd_program_exit(int exit_code)
 {
-- 
2.35.3

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


[PATCH 3/8] tcpdump: Make loop monitor cooperative

2022-05-11 Thread Sebastian Huber
This helps a bit if the fgetc() is non-blocking.

Update #4650.
---
 freebsd/contrib/tcpdump/tcpdump.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/freebsd/contrib/tcpdump/tcpdump.c 
b/freebsd/contrib/tcpdump/tcpdump.c
index b1e7f0d1..74c878a3 100644
--- a/freebsd/contrib/tcpdump/tcpdump.c
+++ b/freebsd/contrib/tcpdump/tcpdump.c
@@ -139,6 +139,7 @@ The Regents of the University of California.  All rights 
reserved.\n";
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #define setpriority(a, b, c)
@@ -1224,6 +1225,8 @@ pcap_loop_monitor(rtems_task_argument arg)
pcap_breakloop(pd);
break;
}
+
+   sched_yield();
}
 
rtems_task_delete(RTEMS_SELF);
-- 
2.35.3

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


[PATCH 0/8] tcpdump: Fix dumping to file and reading from file

2022-05-11 Thread Sebastian Huber
This patch is for the 5-freebsd-12, 6-freebsd-12, and master branches of
libbsd.

Sebastian Huber (8):
  tcpdump01: New test
  Add program destructor support
  tcpdump: Make loop monitor cooperative
  tcpdump: Use rtems_task_exit()
  tcpdump: Close pcap dumper at program exit
  tcpdump: No loop monitor if reading from file
  tcpdump: Ensure loop monitor termination
  tcpdump: Do not use signals and chroot

 freebsd/contrib/tcpdump/tcpdump.c|  95 --
 libbsd.py|   1 +
 rtemsbsd/include/machine/rtems-bsd-program.h |   6 +
 rtemsbsd/rtems/program-internal.h|   7 +
 rtemsbsd/rtems/rtems-program.c   |  51 
 testsuite/tcpdump01/test_main.c  | 292 +++
 6 files changed, 428 insertions(+), 24 deletions(-)
 create mode 100644 testsuite/tcpdump01/test_main.c

-- 
2.35.3

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel