xiaoxiang781216 commented on code in PR #3539:
URL: https://github.com/apache/nuttx-apps/pull/3539#discussion_r3407462036
##########
examples/thttpd/content/tasks/tasks.c:
##########
@@ -78,38 +66,210 @@ static FAR const char *g_policynames[4] =
* Private Functions
****************************************************************************/
-/* NOTEs:
- *
- * 1. One limitation in the use of NXFLAT is that functions that are
- * referenced as a pointer-to-a-function must have global scope.
- * Otherwise ARM GCC will generate some bad logic.
- * 2. In general, when called back, there is no guarantee to that PIC
- * registers will be valid and, unless you take special precautions, it
- * could be dangerous to reference global variables in the callback
- * function.
- */
-
-void show_task(FAR struct tcb_s *tcb, FAR void *arg)
+/****************************************************************************
+ * Name: tasks_trim_value
+ ****************************************************************************/
+
+static FAR char *tasks_trim_value(FAR char *line)
{
- FAR const char *policy;
+ FAR char *end;
- /* Show task/thread status */
+ while (isblank((unsigned char)*line) && *line != '\0')
+ {
+ line++;
+ }
- policy = g_policynames[(tcb->flags & TCB_FLAG_POLICY_MASK) >>
- TCB_FLAG_POLICY_SHIFT];
-#if CONFIG_TASK_NAME_SIZE > 0
- printf("%5d %3d %4s %7s %8s %s\n",
-#else
- printf("%5d %3d %4s %7s %8s\n",
-#endif
- tcb->pid, tcb->sched_priority, policy,
- g_ttypenames[(tcb->flags & TCB_FLAG_TTYPE_MASK) >>
- TCB_FLAG_TTYPE_SHIFT],
- g_statenames[tcb->task_state]
-#if CONFIG_TASK_NAME_SIZE > 0
- , tcb->name
-#endif
- );
+ end = line + strlen(line);
+ while (end > line &&
+ (*(end - 1) == '\n' || *(end - 1) == '\r' ||
+ isblank((unsigned char)*(end - 1))))
+ {
+ end--;
+ }
+
+ *end = '\0';
+ return line;
+}
+
+/****************************************************************************
+ * Name: tasks_is_pid_dir
+ ****************************************************************************/
+
+static bool tasks_is_pid_dir(FAR const struct dirent *entryp)
+{
+ FAR const char *name = entryp->d_name;
+
+ if (!DIRENT_ISDIRECTORY(entryp->d_type) || *name == '\0')
+ {
+ return false;
+ }
+
+ for (; *name != '\0'; name++)
+ {
+ if (!isdigit((unsigned char)*name))
+ {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+/****************************************************************************
+ * Name: tasks_parse_status_line
+ ****************************************************************************/
+
+static void tasks_parse_status_line(FAR char *line,
+ FAR struct thttpd_task_status_s *status)
+{
+ if (strncmp(line, "Name:", 5) == 0)
+ {
+ status->name = tasks_trim_value(&line[5]);
+ }
+ else if (strncmp(line, "Type:", 5) == 0)
+ {
+ status->type = tasks_trim_value(&line[5]);
+ }
+ else if (strncmp(line, "State:", 6) == 0)
+ {
+ status->state = tasks_trim_value(&line[6]);
+ }
+ else if (strncmp(line, "Priority:", 9) == 0)
+ {
+ status->priority = tasks_trim_value(&line[9]);
+ }
+ else if (strncmp(line, "Scheduler:", 10) == 0)
+ {
+ status->policy = tasks_trim_value(&line[10]);
+ }
+}
+
+/****************************************************************************
+ * Name: tasks_parse_status
+ ****************************************************************************/
+
+static void tasks_parse_status(FAR char *buffer,
+ FAR struct thttpd_task_status_s *status)
+{
+ FAR char *line = buffer;
+
+ while (line != NULL && *line != '\0')
+ {
+ FAR char *next = strchr(line, '\n');
+ if (next != NULL)
+ {
+ *next++ = '\0';
+ }
+
+ tasks_parse_status_line(line, status);
+ line = next;
+ }
+}
+
+/****************************************************************************
+ * Name: tasks_read_status
+ ****************************************************************************/
+
+static int tasks_read_status(FAR const char *pid, FAR char *buffer,
+ size_t buflen,
+ FAR struct thttpd_task_status_s *status)
+{
+ char filepath[sizeof(THTTPD_PROCFS_MOUNTPOINT) + NAME_MAX +
+ sizeof("/status") + 1];
+ ssize_t nread;
+ size_t total = 0;
+ int ret;
+ int fd;
+
+ ret = snprintf(filepath, sizeof(filepath), "%s/%s/status",
+ THTTPD_PROCFS_MOUNTPOINT, pid);
+ if (ret < 0 || (size_t)ret >= sizeof(filepath))
Review Comment:
```suggestion
if (ret < 0 || ret >= sizeof(filepath))
```
##########
examples/thttpd/content/tasks/tasks.c:
##########
@@ -78,38 +66,210 @@ static FAR const char *g_policynames[4] =
* Private Functions
****************************************************************************/
-/* NOTEs:
- *
- * 1. One limitation in the use of NXFLAT is that functions that are
- * referenced as a pointer-to-a-function must have global scope.
- * Otherwise ARM GCC will generate some bad logic.
- * 2. In general, when called back, there is no guarantee to that PIC
- * registers will be valid and, unless you take special precautions, it
- * could be dangerous to reference global variables in the callback
- * function.
- */
-
-void show_task(FAR struct tcb_s *tcb, FAR void *arg)
+/****************************************************************************
+ * Name: tasks_trim_value
+ ****************************************************************************/
+
+static FAR char *tasks_trim_value(FAR char *line)
{
- FAR const char *policy;
+ FAR char *end;
- /* Show task/thread status */
+ while (isblank((unsigned char)*line) && *line != '\0')
+ {
+ line++;
+ }
- policy = g_policynames[(tcb->flags & TCB_FLAG_POLICY_MASK) >>
- TCB_FLAG_POLICY_SHIFT];
-#if CONFIG_TASK_NAME_SIZE > 0
- printf("%5d %3d %4s %7s %8s %s\n",
-#else
- printf("%5d %3d %4s %7s %8s\n",
-#endif
- tcb->pid, tcb->sched_priority, policy,
- g_ttypenames[(tcb->flags & TCB_FLAG_TTYPE_MASK) >>
- TCB_FLAG_TTYPE_SHIFT],
- g_statenames[tcb->task_state]
-#if CONFIG_TASK_NAME_SIZE > 0
- , tcb->name
-#endif
- );
+ end = line + strlen(line);
+ while (end > line &&
+ (*(end - 1) == '\n' || *(end - 1) == '\r' ||
+ isblank((unsigned char)*(end - 1))))
+ {
+ end--;
+ }
+
+ *end = '\0';
+ return line;
+}
+
+/****************************************************************************
+ * Name: tasks_is_pid_dir
+ ****************************************************************************/
+
+static bool tasks_is_pid_dir(FAR const struct dirent *entryp)
+{
+ FAR const char *name = entryp->d_name;
+
+ if (!DIRENT_ISDIRECTORY(entryp->d_type) || *name == '\0')
+ {
+ return false;
+ }
+
+ for (; *name != '\0'; name++)
+ {
+ if (!isdigit((unsigned char)*name))
+ {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+/****************************************************************************
+ * Name: tasks_parse_status_line
+ ****************************************************************************/
+
+static void tasks_parse_status_line(FAR char *line,
+ FAR struct thttpd_task_status_s *status)
+{
+ if (strncmp(line, "Name:", 5) == 0)
+ {
+ status->name = tasks_trim_value(&line[5]);
+ }
+ else if (strncmp(line, "Type:", 5) == 0)
+ {
+ status->type = tasks_trim_value(&line[5]);
+ }
+ else if (strncmp(line, "State:", 6) == 0)
+ {
+ status->state = tasks_trim_value(&line[6]);
+ }
+ else if (strncmp(line, "Priority:", 9) == 0)
+ {
+ status->priority = tasks_trim_value(&line[9]);
+ }
+ else if (strncmp(line, "Scheduler:", 10) == 0)
+ {
+ status->policy = tasks_trim_value(&line[10]);
+ }
+}
+
+/****************************************************************************
+ * Name: tasks_parse_status
+ ****************************************************************************/
+
+static void tasks_parse_status(FAR char *buffer,
+ FAR struct thttpd_task_status_s *status)
+{
+ FAR char *line = buffer;
+
+ while (line != NULL && *line != '\0')
+ {
+ FAR char *next = strchr(line, '\n');
+ if (next != NULL)
+ {
+ *next++ = '\0';
+ }
+
+ tasks_parse_status_line(line, status);
+ line = next;
+ }
+}
+
+/****************************************************************************
+ * Name: tasks_read_status
+ ****************************************************************************/
+
+static int tasks_read_status(FAR const char *pid, FAR char *buffer,
+ size_t buflen,
+ FAR struct thttpd_task_status_s *status)
+{
+ char filepath[sizeof(THTTPD_PROCFS_MOUNTPOINT) + NAME_MAX +
+ sizeof("/status") + 1];
+ ssize_t nread;
+ size_t total = 0;
+ int ret;
+ int fd;
+
+ ret = snprintf(filepath, sizeof(filepath), "%s/%s/status",
+ THTTPD_PROCFS_MOUNTPOINT, pid);
+ if (ret < 0 || (size_t)ret >= sizeof(filepath))
+ {
+ return -ENAMETOOLONG;
+ }
+
+ fd = open(filepath, O_RDONLY);
+ if (fd < 0)
+ {
+ return -errno;
+ }
+
+ while (total < buflen - 1)
+ {
+ nread = read(fd, &buffer[total], buflen - 1 - total);
+ if (nread < 0)
+ {
+ ret = -errno;
+ close(fd);
+ return ret;
+ }
+ else if (nread == 0)
+ {
+ break;
+ }
+
+ total += (size_t)nread;
+ }
+
+ close(fd);
+ buffer[total] = '\0';
+ tasks_parse_status(buffer, status);
+ return OK;
+}
+
+/****************************************************************************
+ * Name: show_task
+ ****************************************************************************/
+
+static void show_task(FAR const char *pid,
+ FAR const struct thttpd_task_status_s *status)
+{
+ printf("%5s %-12s %-14s %-7s %-18s %s\n",
+ pid, status->priority, status->policy, status->type,
+ status->state, status->name);
+}
+
+/****************************************************************************
+ * Name: show_tasks
+ ****************************************************************************/
+
+static int show_tasks(void)
+{
+ FAR struct dirent *entryp;
+ DIR *dirp;
+
+ dirp = opendir(THTTPD_PROCFS_MOUNTPOINT);
+ if (dirp == NULL)
+ {
+ printf("Unable to open %s (is procfs mounted?): %d\n",
+ THTTPD_PROCFS_MOUNTPOINT, errno);
+ return ERROR;
Review Comment:
```suggestion
return -errno;
```
##########
examples/thttpd/content/tasks/tasks.c:
##########
@@ -78,38 +66,210 @@ static FAR const char *g_policynames[4] =
* Private Functions
****************************************************************************/
-/* NOTEs:
- *
- * 1. One limitation in the use of NXFLAT is that functions that are
- * referenced as a pointer-to-a-function must have global scope.
- * Otherwise ARM GCC will generate some bad logic.
- * 2. In general, when called back, there is no guarantee to that PIC
- * registers will be valid and, unless you take special precautions, it
- * could be dangerous to reference global variables in the callback
- * function.
- */
-
-void show_task(FAR struct tcb_s *tcb, FAR void *arg)
+/****************************************************************************
+ * Name: tasks_trim_value
+ ****************************************************************************/
+
+static FAR char *tasks_trim_value(FAR char *line)
{
- FAR const char *policy;
+ FAR char *end;
- /* Show task/thread status */
+ while (isblank((unsigned char)*line) && *line != '\0')
+ {
+ line++;
+ }
- policy = g_policynames[(tcb->flags & TCB_FLAG_POLICY_MASK) >>
- TCB_FLAG_POLICY_SHIFT];
-#if CONFIG_TASK_NAME_SIZE > 0
- printf("%5d %3d %4s %7s %8s %s\n",
-#else
- printf("%5d %3d %4s %7s %8s\n",
-#endif
- tcb->pid, tcb->sched_priority, policy,
- g_ttypenames[(tcb->flags & TCB_FLAG_TTYPE_MASK) >>
- TCB_FLAG_TTYPE_SHIFT],
- g_statenames[tcb->task_state]
-#if CONFIG_TASK_NAME_SIZE > 0
- , tcb->name
-#endif
- );
+ end = line + strlen(line);
+ while (end > line &&
+ (*(end - 1) == '\n' || *(end - 1) == '\r' ||
+ isblank((unsigned char)*(end - 1))))
+ {
+ end--;
+ }
+
+ *end = '\0';
+ return line;
+}
+
+/****************************************************************************
+ * Name: tasks_is_pid_dir
+ ****************************************************************************/
+
+static bool tasks_is_pid_dir(FAR const struct dirent *entryp)
+{
+ FAR const char *name = entryp->d_name;
+
+ if (!DIRENT_ISDIRECTORY(entryp->d_type) || *name == '\0')
+ {
+ return false;
+ }
+
+ for (; *name != '\0'; name++)
+ {
+ if (!isdigit((unsigned char)*name))
+ {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+/****************************************************************************
+ * Name: tasks_parse_status_line
+ ****************************************************************************/
+
+static void tasks_parse_status_line(FAR char *line,
+ FAR struct thttpd_task_status_s *status)
+{
+ if (strncmp(line, "Name:", 5) == 0)
+ {
+ status->name = tasks_trim_value(&line[5]);
+ }
+ else if (strncmp(line, "Type:", 5) == 0)
+ {
+ status->type = tasks_trim_value(&line[5]);
+ }
+ else if (strncmp(line, "State:", 6) == 0)
+ {
+ status->state = tasks_trim_value(&line[6]);
+ }
+ else if (strncmp(line, "Priority:", 9) == 0)
+ {
+ status->priority = tasks_trim_value(&line[9]);
+ }
+ else if (strncmp(line, "Scheduler:", 10) == 0)
+ {
+ status->policy = tasks_trim_value(&line[10]);
+ }
+}
+
+/****************************************************************************
+ * Name: tasks_parse_status
+ ****************************************************************************/
+
+static void tasks_parse_status(FAR char *buffer,
+ FAR struct thttpd_task_status_s *status)
+{
+ FAR char *line = buffer;
+
+ while (line != NULL && *line != '\0')
+ {
+ FAR char *next = strchr(line, '\n');
+ if (next != NULL)
+ {
+ *next++ = '\0';
+ }
+
+ tasks_parse_status_line(line, status);
+ line = next;
+ }
+}
+
+/****************************************************************************
+ * Name: tasks_read_status
+ ****************************************************************************/
+
+static int tasks_read_status(FAR const char *pid, FAR char *buffer,
+ size_t buflen,
+ FAR struct thttpd_task_status_s *status)
+{
+ char filepath[sizeof(THTTPD_PROCFS_MOUNTPOINT) + NAME_MAX +
+ sizeof("/status") + 1];
+ ssize_t nread;
+ size_t total = 0;
+ int ret;
+ int fd;
+
+ ret = snprintf(filepath, sizeof(filepath), "%s/%s/status",
+ THTTPD_PROCFS_MOUNTPOINT, pid);
+ if (ret < 0 || (size_t)ret >= sizeof(filepath))
+ {
+ return -ENAMETOOLONG;
+ }
+
+ fd = open(filepath, O_RDONLY);
+ if (fd < 0)
+ {
+ return -errno;
+ }
+
+ while (total < buflen - 1)
+ {
+ nread = read(fd, &buffer[total], buflen - 1 - total);
+ if (nread < 0)
+ {
+ ret = -errno;
+ close(fd);
+ return ret;
+ }
+ else if (nread == 0)
+ {
+ break;
+ }
+
+ total += (size_t)nread;
Review Comment:
```suggestion
total += nread;
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]