Skip to content

Commit

Permalink
Add The Linux Programming Interface under examples
Browse files Browse the repository at this point in the history
https://man7.org/tlpi/index.html

Signed-off-by: chenzhijia <chenzhijia@xiaomi.com>
  • Loading branch information
Cccccczj authored and xiaoxiang781216 committed Oct 31, 2024
1 parent 4ba65cc commit 34ca696
Show file tree
Hide file tree
Showing 8 changed files with 810 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/tlpi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/tlpi
57 changes: 57 additions & 0 deletions examples/tlpi/0001-add-ename.c.inc.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
From 57d210aa51e9706c3e21156d873bf356527be8c7 Mon Sep 17 00:00:00 2001
From: chenzhijia <chenzhijia@xiaomi.com>
Date: Tue, 19 Mar 2024 11:26:27 +0800
Subject: [PATCH] Linux case base:add ename.c.inc

add source code ename.c.inc

Change-Id: Ie31e2ac3908ce46cfb22102d8cc18fa1b961deb4
Signed-off-by: chenzhijia <chenzhijia@xiaomi.com>
---

diff --git a/lib/ename.c.inc b/lib/ename.c.inc
new file mode 100644
index 0000000..42642e1
--- /dev/null
+++ b/lib/ename.c.inc
@@ -0,0 +1,39 @@
+/* ename.c.inc
+
+ Built on GNU/Linux x86_64 with glibc 2.38
+*/
+static char *ename[] = {
+ /* 0 */ "",
+ /* 1 */ "EPERM", "ENOENT", "ESRCH", "EINTR", "EIO", "ENXIO",
+ /* 7 */ "E2BIG", "ENOEXEC", "EBADF", "ECHILD",
+ /* 11 */ "EAGAIN/EWOULDBLOCK", "ENOMEM", "EACCES", "EFAULT",
+ /* 15 */ "ENOTBLK", "EBUSY", "EEXIST", "EXDEV", "ENODEV",
+ /* 20 */ "ENOTDIR", "EISDIR", "EINVAL", "ENFILE", "EMFILE",
+ /* 25 */ "ENOTTY", "ETXTBSY", "EFBIG", "ENOSPC", "ESPIPE",
+ /* 30 */ "EROFS", "EMLINK", "EPIPE", "EDOM", "ERANGE",
+ /* 35 */ "EDEADLK/EDEADLOCK", "ENAMETOOLONG", "ENOLCK", "ENOSYS",
+ /* 39 */ "ENOTEMPTY", "ELOOP", "", "ENOMSG", "EIDRM", "ECHRNG",
+ /* 45 */ "EL2NSYNC", "EL3HLT", "EL3RST", "ELNRNG", "EUNATCH",
+ /* 50 */ "ENOCSI", "EL2HLT", "EBADE", "EBADR", "EXFULL", "ENOANO",
+ /* 56 */ "EBADRQC", "EBADSLT", "", "EBFONT", "ENOSTR", "ENODATA",
+ /* 62 */ "ETIME", "ENOSR", "ENONET", "ENOPKG", "EREMOTE",
+ /* 67 */ "ENOLINK", "EADV", "ESRMNT", "ECOMM", "EPROTO",
+ /* 72 */ "EMULTIHOP", "EDOTDOT", "EBADMSG", "EOVERFLOW",
+ /* 76 */ "ENOTUNIQ", "EBADFD", "EREMCHG", "ELIBACC", "ELIBBAD",
+ /* 81 */ "ELIBSCN", "ELIBMAX", "ELIBEXEC", "EILSEQ", "ERESTART",
+ /* 86 */ "ESTRPIPE", "EUSERS", "ENOTSOCK", "EDESTADDRREQ",
+ /* 90 */ "EMSGSIZE", "EPROTOTYPE", "ENOPROTOOPT",
+ /* 93 */ "EPROTONOSUPPORT", "ESOCKTNOSUPPORT",
+ /* 95 */ "EOPNOTSUPP/ENOTSUP", "EPFNOSUPPORT", "EAFNOSUPPORT",
+ /* 98 */ "EADDRINUSE", "EADDRNOTAVAIL", "ENETDOWN", "ENETUNREACH",
+ /* 102 */ "ENETRESET", "ECONNABORTED", "ECONNRESET", "ENOBUFS",
+ /* 106 */ "EISCONN", "ENOTCONN", "ESHUTDOWN", "ETOOMANYREFS",
+ /* 110 */ "ETIMEDOUT", "ECONNREFUSED", "EHOSTDOWN", "EHOSTUNREACH",
+ /* 114 */ "EALREADY", "EINPROGRESS", "ESTALE", "EUCLEAN",
+ /* 118 */ "ENOTNAM", "ENAVAIL", "EISNAM", "EREMOTEIO", "EDQUOT",
+ /* 123 */ "ENOMEDIUM", "EMEDIUMTYPE", "ECANCELED", "ENOKEY",
+ /* 127 */ "EKEYEXPIRED", "EKEYREVOKED", "EKEYREJECTED",
+ /* 130 */ "EOWNERDEAD", "ENOTRECOVERABLE", "ERFKILL", "EHWPOISON"
+};
+
+#define MAX_ENAME 133
\ No newline at end of file
111 changes: 111 additions & 0 deletions examples/tlpi/0002-fix-setenv-compilation-error.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
From f1154cd6558b99aa5827ac98b20a20753dabd865 Mon Sep 17 00:00:00 2001
From: chenzhijia <chenzhijia@xiaomi.com>
Date: Tue, 18 Jun 2024 18:19:18 +0800
Subject: [PATCH] tlpi/proc:fix setenv case

Remove duplicate functions and invalid commands

Change-Id: I59d84511356f6da7313844bfad63ac466fd3075f
Signed-off-by: chenzhijia <chenzhijia@xiaomi.com>
---

diff --git a/proc/setenv.c b/proc/setenv.c
index d396628..d75ae35 100644
--- a/proc/setenv.c
+++ b/proc/setenv.c
@@ -21,92 +21,25 @@
#include <errno.h>

int
-unsetenv(const char *name)
-{
- extern char **environ;
- char **ep, **sp;
- size_t len;
-
- if (name == NULL || name[0] == '\0' || strchr(name, '=') != NULL) {
- errno = EINVAL;
- return -1;
- }
-
- len = strlen(name);
-
- for (ep = environ; *ep != NULL; )
- if (strncmp(*ep, name, len) == 0 && (*ep)[len] == '=') {
-
- /* Remove found entry by shifting all successive entries
- back one element */
-
- for (sp = ep; *sp != NULL; sp++)
- *sp = *(sp + 1);
-
- /* Continue around the loop to further instances of 'name' */
-
- } else {
- ep++;
- }
-
- return 0;
-}
-
-int
-setenv(const char *name, const char *value, int overwrite)
-{
- char *es;
-
- if (name == NULL || name[0] == '\0' || strchr(name, '=') != NULL ||
- value == NULL) {
- errno = EINVAL;
- return -1;
- }
-
- if (getenv(name) != NULL && overwrite == 0)
- return 0;
-
- unsetenv(name); /* Remove all occurrences */
-
- es = malloc(strlen(name) + strlen(value) + 2);
- /* +2 for '=' and null terminator */
- if (es == NULL)
- return -1;
-
- strcpy(es, name);
- strcat(es, "=");
- strcat(es, value);
-
- return (putenv(es) != 0) ? -1 : 0;
-}
-
-#ifdef TEST_IT
-
-int
main()
{
if (putenv("TT=xxxxx") != 0)
perror("putenv");

system("echo '***** Environment before unsetenv(TT)'; "
- "printenv | grep ^TT");
- system("echo 'Total lines from printenv:' `printenv | wc -l`");
+ "env");

unsetenv("TT");

system("echo '***** Environment after unsetenv(TT)'; "
- "printenv | grep ^TT");
- system("echo 'Total lines from printenv:' `printenv | wc -l`");
+ "env");

setenv("xyz", "one", 1);
setenv("xyz", "two", 0);
setenv("xyz2", "222", 0);

system("echo '***** Environment after setenv() calls'; "
- "printenv | grep ^x");
- system("echo 'Total lines from printenv:' `printenv | wc -l`");
+ "env");

exit(EXIT_SUCCESS);
}
-
-#endif
66 changes: 66 additions & 0 deletions examples/tlpi/0003-fix-memoryleak-in-check_password.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
From 03445e7bf9916a58a4b80d7062d577f38ed2b279 Mon Sep 17 00:00:00 2001
From: chenzhijia <chenzhijia@xiaomi.com>
Date: Tue, 14 May 2024 16:40:28 +0800
Subject: [PATCH] tlpi/users_groups:Fix memory leak in check_password

free memory before program exits

Change-Id: I28789b5ceaec4aaa45bb13f66cb3f61de1111841
Signed-off-by: chenzhijia <chenzhijia@xiaomi.com>
---

diff --git a/users_groups/check_password.c b/users_groups/check_password.c
index c142ac7..a70cbb6 100644
--- a/users_groups/check_password.c
+++ b/users_groups/check_password.c
@@ -30,6 +30,22 @@
#include <shadow.h>
#include "tlpi_hdr.h"

+#define fatal(msg) \
+ do \
+ { \
+ free(username); \
+ fatal(msg); \
+ } \
+ while (0)
+
+#define errExit(msg) \
+ do \
+ { \
+ free(username); \
+ errExit(msg); \
+ } \
+ while (0)
+
int
main(int argc, char *argv[])
{
@@ -53,7 +69,7 @@
printf("Username: ");
fflush(stdout);
if (fgets(username, lnmax, stdin) == NULL)
- exit(EXIT_FAILURE); /* Exit on EOF */
+ goto err; /* Exit on EOF */

len = strlen(username);
if (username[len - 1] == '\n')
@@ -85,12 +101,17 @@
authOk = strcmp(encrypted, pwd->pw_passwd) == 0;
if (!authOk) {
printf("Incorrect password\n");
- exit(EXIT_FAILURE);
+ goto err;
}

printf("Successfully authenticated: UID=%ld\n", (long) pwd->pw_uid);

/* Now do authenticated work... */

+ free(username);
exit(EXIT_SUCCESS);
+
+err:
+ free(username);
+ exit(EXIT_FAILURE);
}
37 changes: 37 additions & 0 deletions examples/tlpi/0004-fix-compilation-error.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
From 65230c70d69b253028c0f912dcd1ae0cb7446015 Mon Sep 17 00:00:00 2001
From: chenzhijia <chenzhijia@xiaomi.com>
Date: Wed, 30 Oct 2024 10:52:48 +0800
Subject: [PATCH] tlpi/sockets:Fix compilation error

Fix compilation error in qemu

Change-Id: I854ed64d163398807a07fa697bdea89bfab77236
Signed-off-by: chenzhijia <chenzhijia@xiaomi.com>
---

diff --git a/sockets/list_host_addresses.c b/sockets/list_host_addresses.c
index 4db58f6..e1864c8 100644
--- a/sockets/list_host_addresses.c
+++ b/sockets/list_host_addresses.c
@@ -23,7 +23,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
-#include <linux/if_link.h>
+//#include <linux/if_link.h>

int
main(int argc, char *argv[])
diff --git a/sockets/sendfile.c b/sockets/sendfile.c
index 54549d7..3697099 100644
--- a/sockets/sendfile.c
+++ b/sockets/sendfile.c
@@ -17,7 +17,7 @@
#include "tlpi_hdr.h"
#define BUF_SIZE 8192

-ssize_t
+static ssize_t
sendfile(int out_fd, int in_fd, off_t *offset, size_t count)
{
off_t orig;
23 changes: 23 additions & 0 deletions examples/tlpi/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

config EXAMPLES_TLPI
tristate "The Linux Programming Interface Example"
default n
depends on ALLOW_GPL_COMPONENTS
---help---
Enable the tlpi

if EXAMPLES_TLPI

config EXAMPLES_TLPI_PRIORITY
int "Task priority"
default 100

config EXAMPLES_TLPI_STACKSIZE
int "Stack size"
default DEFAULT_TASK_STACKSIZE

endif
23 changes: 23 additions & 0 deletions examples/tlpi/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
############################################################################
# apps/examples/tlpi/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional 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.
#
############################################################################

ifneq ($(CONFIG_EXAMPLES_TLPI),)
CONFIGURED_APPS += $(APPDIR)/examples/tlpi
endif
Loading

0 comments on commit 34ca696

Please sign in to comment.