xiaoxiang781216 commented on code in PR #3090: URL: https://github.com/apache/nuttx-apps/pull/3090#discussion_r2142277875
########## system/syslogd/syslogd_main.c: ########## @@ -0,0 +1,376 @@ +/**************************************************************************** + * apps/system/syslogd/syslogd_main.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for asyslogditional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <unistd.h> + +#ifdef CONFIG_LIBC_EXECFUNCS +#include <spawn.h> +#endif + +#include <getopt.h> +#include <syslog.h> + +#include <arpa/inet.h> +#include <netinet/in.h> +#include <sys/socket.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* syslogd program version */ + +#define SYSLOGD_VERSION "0.0.0" + +/* Minimum buffer size check */ + +#if CONFIG_SYSTEM_SYSLOGD_ENTRYSIZE < 480 +#error "SYSTEM_SYSLOGD_ENTRYSIZE must be more than 480 to satisfy RFC 5424" +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: print_usage + ****************************************************************************/ + +static void print_usage(void) +{ + fprintf(stderr, "usage:\n"); + fprintf(stderr, " %s [-vdn]\n", CONFIG_SYSTEM_SYSLOGD_PROGNAME); +} + +/**************************************************************************** + * Name: find_end_pos + * + * Description: + * Find the position of the newline character separating the syslog entry. + * + * Return: Index of the newline character, -1 if not found. + ****************************************************************************/ + +static ssize_t find_endpos(char *buf, size_t n) Review Comment: let's call `strchrnul(buf, n, '\n')` which is more fast. ########## system/syslogd/syslogd_main.c: ########## @@ -0,0 +1,376 @@ +/**************************************************************************** + * apps/system/syslogd/syslogd_main.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for asyslogditional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <unistd.h> + +#ifdef CONFIG_LIBC_EXECFUNCS +#include <spawn.h> +#endif + +#include <getopt.h> +#include <syslog.h> + +#include <arpa/inet.h> +#include <netinet/in.h> +#include <sys/socket.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* syslogd program version */ + +#define SYSLOGD_VERSION "0.0.0" + +/* Minimum buffer size check */ + +#if CONFIG_SYSTEM_SYSLOGD_ENTRYSIZE < 480 +#error "SYSTEM_SYSLOGD_ENTRYSIZE must be more than 480 to satisfy RFC 5424" +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: print_usage + ****************************************************************************/ + +static void print_usage(void) +{ + fprintf(stderr, "usage:\n"); + fprintf(stderr, " %s [-vdn]\n", CONFIG_SYSTEM_SYSLOGD_PROGNAME); +} + +/**************************************************************************** + * Name: find_end_pos + * + * Description: + * Find the position of the newline character separating the syslog entry. + * + * Return: Index of the newline character, -1 if not found. + ****************************************************************************/ + +static ssize_t find_endpos(char *buf, size_t n) +{ + ssize_t pos; + + for (pos = 0; pos < n; pos++) + { + if (buf[pos] == '\n') + { + return pos; + } + } + + return -1; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int main(int argc, FAR char **argv) +{ + int fd; + int sock; + int c; + ssize_t bread; + ssize_t bsent; + ssize_t endpos; + size_t bufpos = 0; + struct sockaddr_in server; + char buffer[CONFIG_SYSTEM_SYSLOGD_ENTRYSIZE]; + bool debugmode = false; + bool skiplog = false; +#ifdef CONFIG_LIBC_EXECFUNCS + pid_t pid; + bool background = true; + char **new_argv; +#endif + + /* Parse command line options */ + + while ((c = getopt(argc, argv, ":vdn")) != -1) + { + switch (c) + { + case 'v': + + /* Print version and exit */ + + printf("%s " SYSLOGD_VERSION " (NuttX)\n", argv[0]); + return EXIT_SUCCESS; + break; Review Comment: remove ########## system/syslogd/syslogd_main.c: ########## @@ -0,0 +1,376 @@ +/**************************************************************************** + * apps/system/syslogd/syslogd_main.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for asyslogditional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <unistd.h> + +#ifdef CONFIG_LIBC_EXECFUNCS +#include <spawn.h> +#endif + +#include <getopt.h> +#include <syslog.h> + +#include <arpa/inet.h> +#include <netinet/in.h> +#include <sys/socket.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* syslogd program version */ + +#define SYSLOGD_VERSION "0.0.0" + +/* Minimum buffer size check */ + +#if CONFIG_SYSTEM_SYSLOGD_ENTRYSIZE < 480 +#error "SYSTEM_SYSLOGD_ENTRYSIZE must be more than 480 to satisfy RFC 5424" +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: print_usage + ****************************************************************************/ + +static void print_usage(void) +{ + fprintf(stderr, "usage:\n"); + fprintf(stderr, " %s [-vdn]\n", CONFIG_SYSTEM_SYSLOGD_PROGNAME); +} + +/**************************************************************************** + * Name: find_end_pos + * + * Description: + * Find the position of the newline character separating the syslog entry. + * + * Return: Index of the newline character, -1 if not found. + ****************************************************************************/ + +static ssize_t find_endpos(char *buf, size_t n) +{ + ssize_t pos; + + for (pos = 0; pos < n; pos++) + { + if (buf[pos] == '\n') + { + return pos; + } + } + + return -1; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int main(int argc, FAR char **argv) +{ + int fd; + int sock; + int c; + ssize_t bread; + ssize_t bsent; + ssize_t endpos; + size_t bufpos = 0; + struct sockaddr_in server; + char buffer[CONFIG_SYSTEM_SYSLOGD_ENTRYSIZE]; + bool debugmode = false; + bool skiplog = false; +#ifdef CONFIG_LIBC_EXECFUNCS + pid_t pid; + bool background = true; + char **new_argv; +#endif + + /* Parse command line options */ + + while ((c = getopt(argc, argv, ":vdn")) != -1) + { + switch (c) + { + case 'v': + + /* Print version and exit */ + + printf("%s " SYSLOGD_VERSION " (NuttX)\n", argv[0]); + return EXIT_SUCCESS; + break; + + case 'd': + + /* Enable debug mode and stay in foreground */ + + debugmode = true; + printf("Enabling debug mode.\n"); +#ifdef CONFIG_LIBC_EXECFUNCS + background = false; +#endif + break; + + case 'n': + + /* Stay in foreground */ + +#ifdef CONFIG_LIBC_EXECFUNCS + background = false; +#endif + break; + + case '?': + print_usage(); + exit(EXIT_FAILURE); + break; + } + } + + /* Run this program in the background as a spawned task if the background + * option was selected. + */ + +#ifdef CONFIG_LIBC_EXECFUNCS + if (background) + { + /* Set up the arguments, which is identical to the original except with + * an added `-n` flag to ensure that the new process does not 'respawn' + */ + + new_argv = malloc(sizeof(char *) * (argc + 1)); + new_argv[0] = argv[0]; /* Same program name */ + new_argv[1] = "-n"; /* Prevent daemon from spawning another child */ + memcpy(&new_argv[2], &argv[1], sizeof(char *) * (argc - 1)); + + /* Spawn the child for backgrounding now */ + + if (posix_spawn(&pid, argv[0], NULL, NULL, new_argv, NULL) != 0) + { + fprintf(stderr, "Failed to fork() to background process: %d\n", + errno); + free(new_argv); + return EXIT_FAILURE; + } + else + { + /* We succeeded in spawning, exit now. */ + + free(new_argv); + return EXIT_SUCCESS; + } + } +#endif /* CONFIG_LIBC_EXECFUNCS */ + + /* Set up client connection information */ + + server.sin_family = AF_INET; + server.sin_port = htons(CONFIG_SYSTEM_SYSLOGD_PORT); + server.sin_addr.s_addr = inet_addr(CONFIG_SYSTEM_SYSLOGD_ADDR); + + if (server.sin_addr.s_addr == INADDR_NONE) + { + fprintf(stderr, "Invalid address '%s'\n", CONFIG_SYSTEM_SYSLOGD_ADDR); + return EXIT_FAILURE; + } + + /* Create a UDP socket */ + + if (debugmode) + { + printf("Creating UDP socket %s:%u\n", CONFIG_SYSTEM_SYSLOGD_ADDR, + CONFIG_SYSTEM_SYSLOGD_PORT); + } + + sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock < 0) + { + fprintf(stderr, "Couldn't create UDP socket: %d\n", errno); + return EXIT_FAILURE; + } + + /* Open syslog stream */ + + if (debugmode) + { + printf("Opening syslog device '%s' to read entries.\n", + CONFIG_SYSLOG_DEVPATH); + } + + fd = open(CONFIG_SYSLOG_DEVPATH, O_RDWR); + if (fd < 0) + { + fprintf(stderr, "Could not open syslog stream: %d", errno); + close(sock); + return EXIT_FAILURE; + } + + /* Transmit syslog messages forever */ + + if (debugmode) + { + printf("Beginning to continuously transmit syslog entries.\n"); + } + + while (true) Review Comment: for (; ;) ########## system/syslogd/syslogd_main.c: ########## @@ -0,0 +1,376 @@ +/**************************************************************************** + * apps/system/syslogd/syslogd_main.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for asyslogditional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <unistd.h> + +#ifdef CONFIG_LIBC_EXECFUNCS +#include <spawn.h> +#endif + +#include <getopt.h> +#include <syslog.h> + +#include <arpa/inet.h> +#include <netinet/in.h> +#include <sys/socket.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* syslogd program version */ + +#define SYSLOGD_VERSION "0.0.0" + +/* Minimum buffer size check */ + +#if CONFIG_SYSTEM_SYSLOGD_ENTRYSIZE < 480 +#error "SYSTEM_SYSLOGD_ENTRYSIZE must be more than 480 to satisfy RFC 5424" +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: print_usage + ****************************************************************************/ + +static void print_usage(void) +{ + fprintf(stderr, "usage:\n"); + fprintf(stderr, " %s [-vdn]\n", CONFIG_SYSTEM_SYSLOGD_PROGNAME); +} + +/**************************************************************************** + * Name: find_end_pos + * + * Description: + * Find the position of the newline character separating the syslog entry. + * + * Return: Index of the newline character, -1 if not found. + ****************************************************************************/ + +static ssize_t find_endpos(char *buf, size_t n) +{ + ssize_t pos; + + for (pos = 0; pos < n; pos++) + { + if (buf[pos] == '\n') + { + return pos; + } + } + + return -1; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int main(int argc, FAR char **argv) +{ + int fd; + int sock; + int c; + ssize_t bread; + ssize_t bsent; + ssize_t endpos; + size_t bufpos = 0; + struct sockaddr_in server; + char buffer[CONFIG_SYSTEM_SYSLOGD_ENTRYSIZE]; + bool debugmode = false; + bool skiplog = false; +#ifdef CONFIG_LIBC_EXECFUNCS + pid_t pid; + bool background = true; + char **new_argv; +#endif + + /* Parse command line options */ + + while ((c = getopt(argc, argv, ":vdn")) != -1) + { + switch (c) + { + case 'v': + + /* Print version and exit */ + + printf("%s " SYSLOGD_VERSION " (NuttX)\n", argv[0]); + return EXIT_SUCCESS; + break; + + case 'd': + + /* Enable debug mode and stay in foreground */ + + debugmode = true; + printf("Enabling debug mode.\n"); +#ifdef CONFIG_LIBC_EXECFUNCS + background = false; +#endif + break; + + case 'n': + + /* Stay in foreground */ + +#ifdef CONFIG_LIBC_EXECFUNCS + background = false; +#endif + break; + + case '?': + print_usage(); + exit(EXIT_FAILURE); + break; + } + } + + /* Run this program in the background as a spawned task if the background + * option was selected. + */ + +#ifdef CONFIG_LIBC_EXECFUNCS + if (background) + { + /* Set up the arguments, which is identical to the original except with + * an added `-n` flag to ensure that the new process does not 'respawn' + */ + + new_argv = malloc(sizeof(char *) * (argc + 1)); + new_argv[0] = argv[0]; /* Same program name */ + new_argv[1] = "-n"; /* Prevent daemon from spawning another child */ + memcpy(&new_argv[2], &argv[1], sizeof(char *) * (argc - 1)); + + /* Spawn the child for backgrounding now */ + + if (posix_spawn(&pid, argv[0], NULL, NULL, new_argv, NULL) != 0) + { + fprintf(stderr, "Failed to fork() to background process: %d\n", + errno); + free(new_argv); + return EXIT_FAILURE; + } + else + { + /* We succeeded in spawning, exit now. */ + + free(new_argv); + return EXIT_SUCCESS; + } + } +#endif /* CONFIG_LIBC_EXECFUNCS */ + + /* Set up client connection information */ + + server.sin_family = AF_INET; + server.sin_port = htons(CONFIG_SYSTEM_SYSLOGD_PORT); + server.sin_addr.s_addr = inet_addr(CONFIG_SYSTEM_SYSLOGD_ADDR); + + if (server.sin_addr.s_addr == INADDR_NONE) + { + fprintf(stderr, "Invalid address '%s'\n", CONFIG_SYSTEM_SYSLOGD_ADDR); + return EXIT_FAILURE; + } + + /* Create a UDP socket */ + + if (debugmode) + { + printf("Creating UDP socket %s:%u\n", CONFIG_SYSTEM_SYSLOGD_ADDR, + CONFIG_SYSTEM_SYSLOGD_PORT); + } + + sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock < 0) + { + fprintf(stderr, "Couldn't create UDP socket: %d\n", errno); + return EXIT_FAILURE; + } + + /* Open syslog stream */ + + if (debugmode) + { + printf("Opening syslog device '%s' to read entries.\n", + CONFIG_SYSLOG_DEVPATH); + } + + fd = open(CONFIG_SYSLOG_DEVPATH, O_RDWR); + if (fd < 0) + { + fprintf(stderr, "Could not open syslog stream: %d", errno); + close(sock); + return EXIT_FAILURE; + } + + /* Transmit syslog messages forever */ + + if (debugmode) + { + printf("Beginning to continuously transmit syslog entries.\n"); + } + + while (true) + { + /* Read as much data as possible into the remaining space in our buffer + */ + + bread = read(fd, &buffer[bufpos], sizeof(buffer) - bufpos); + if (bread < 0) + { + fprintf(stderr, "Failed to read from syslog: %d", errno); + continue; /* Just try again */ + } + + if (bread == 0 && bufpos == 0) + { + /* Stream is over, terminate the program. */ + + if (debugmode) + { + printf("Syslog stream depleted, exiting...\n"); + } + + close(fd); Review Comment: break to avoid the code dup ########## system/syslogd/syslogd_main.c: ########## @@ -0,0 +1,376 @@ +/**************************************************************************** + * apps/system/syslogd/syslogd_main.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for asyslogditional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <fcntl.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <unistd.h> + +#ifdef CONFIG_LIBC_EXECFUNCS +#include <spawn.h> +#endif + +#include <getopt.h> +#include <syslog.h> + +#include <arpa/inet.h> +#include <netinet/in.h> +#include <sys/socket.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* syslogd program version */ + +#define SYSLOGD_VERSION "0.0.0" + +/* Minimum buffer size check */ + +#if CONFIG_SYSTEM_SYSLOGD_ENTRYSIZE < 480 +#error "SYSTEM_SYSLOGD_ENTRYSIZE must be more than 480 to satisfy RFC 5424" +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: print_usage + ****************************************************************************/ + +static void print_usage(void) +{ + fprintf(stderr, "usage:\n"); + fprintf(stderr, " %s [-vdn]\n", CONFIG_SYSTEM_SYSLOGD_PROGNAME); +} + +/**************************************************************************** + * Name: find_end_pos + * + * Description: + * Find the position of the newline character separating the syslog entry. + * + * Return: Index of the newline character, -1 if not found. + ****************************************************************************/ + +static ssize_t find_endpos(char *buf, size_t n) +{ + ssize_t pos; + + for (pos = 0; pos < n; pos++) + { + if (buf[pos] == '\n') + { + return pos; + } + } + + return -1; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int main(int argc, FAR char **argv) +{ + int fd; + int sock; + int c; + ssize_t bread; + ssize_t bsent; + ssize_t endpos; + size_t bufpos = 0; + struct sockaddr_in server; + char buffer[CONFIG_SYSTEM_SYSLOGD_ENTRYSIZE]; + bool debugmode = false; + bool skiplog = false; +#ifdef CONFIG_LIBC_EXECFUNCS + pid_t pid; + bool background = true; + char **new_argv; +#endif + + /* Parse command line options */ + + while ((c = getopt(argc, argv, ":vdn")) != -1) + { + switch (c) + { + case 'v': + + /* Print version and exit */ + + printf("%s " SYSLOGD_VERSION " (NuttX)\n", argv[0]); + return EXIT_SUCCESS; + break; + + case 'd': + + /* Enable debug mode and stay in foreground */ + + debugmode = true; + printf("Enabling debug mode.\n"); +#ifdef CONFIG_LIBC_EXECFUNCS + background = false; +#endif + break; + + case 'n': + + /* Stay in foreground */ + +#ifdef CONFIG_LIBC_EXECFUNCS + background = false; +#endif + break; + + case '?': + print_usage(); + exit(EXIT_FAILURE); + break; + } + } + + /* Run this program in the background as a spawned task if the background + * option was selected. + */ + +#ifdef CONFIG_LIBC_EXECFUNCS + if (background) + { + /* Set up the arguments, which is identical to the original except with + * an added `-n` flag to ensure that the new process does not 'respawn' + */ + + new_argv = malloc(sizeof(char *) * (argc + 1)); + new_argv[0] = argv[0]; /* Same program name */ + new_argv[1] = "-n"; /* Prevent daemon from spawning another child */ + memcpy(&new_argv[2], &argv[1], sizeof(char *) * (argc - 1)); + + /* Spawn the child for backgrounding now */ + + if (posix_spawn(&pid, argv[0], NULL, NULL, new_argv, NULL) != 0) + { + fprintf(stderr, "Failed to fork() to background process: %d\n", + errno); + free(new_argv); + return EXIT_FAILURE; + } + else + { + /* We succeeded in spawning, exit now. */ + + free(new_argv); + return EXIT_SUCCESS; + } + } +#endif /* CONFIG_LIBC_EXECFUNCS */ + + /* Set up client connection information */ + + server.sin_family = AF_INET; + server.sin_port = htons(CONFIG_SYSTEM_SYSLOGD_PORT); + server.sin_addr.s_addr = inet_addr(CONFIG_SYSTEM_SYSLOGD_ADDR); + + if (server.sin_addr.s_addr == INADDR_NONE) + { + fprintf(stderr, "Invalid address '%s'\n", CONFIG_SYSTEM_SYSLOGD_ADDR); + return EXIT_FAILURE; + } + + /* Create a UDP socket */ + + if (debugmode) + { + printf("Creating UDP socket %s:%u\n", CONFIG_SYSTEM_SYSLOGD_ADDR, + CONFIG_SYSTEM_SYSLOGD_PORT); + } + + sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock < 0) + { + fprintf(stderr, "Couldn't create UDP socket: %d\n", errno); + return EXIT_FAILURE; + } + + /* Open syslog stream */ + + if (debugmode) + { + printf("Opening syslog device '%s' to read entries.\n", + CONFIG_SYSLOG_DEVPATH); + } + + fd = open(CONFIG_SYSLOG_DEVPATH, O_RDWR); + if (fd < 0) + { + fprintf(stderr, "Could not open syslog stream: %d", errno); + close(sock); + return EXIT_FAILURE; + } + + /* Transmit syslog messages forever */ + + if (debugmode) + { + printf("Beginning to continuously transmit syslog entries.\n"); + } + + while (true) + { + /* Read as much data as possible into the remaining space in our buffer + */ + + bread = read(fd, &buffer[bufpos], sizeof(buffer) - bufpos); + if (bread < 0) + { + fprintf(stderr, "Failed to read from syslog: %d", errno); + continue; /* Just try again */ + } + + if (bread == 0 && bufpos == 0) Review Comment: we should output the buffer log and exit loop if `bread <= 0`: ```suggestion if (bread <= 0) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org