diff options
Diffstat (limited to 'man7/unix.7')
-rw-r--r-- | man7/unix.7 | 137 |
1 files changed, 66 insertions, 71 deletions
diff --git a/man7/unix.7 b/man7/unix.7 index 2b41465da..cfa41880a 100644 --- a/man7/unix.7 +++ b/man7/unix.7 @@ -1,14 +1,9 @@ +.\" SPDX-License-Identifier: Linux-man-pages-1-para +.\" .\" This man page is Copyright (C) 1999 Andi Kleen <ak@muc.de>, .\" Copyright (C) 2008-2014, Michael Kerrisk <mtk.manpages@gmail.com>, .\" and Copyright (C) 2016, Heinrich Schuchardt <xypron.glpk@gmx.de> .\" -.\" %%%LICENSE_START(VERBATIM_ONE_PARA) -.\" Permission is granted to distribute possibly modified copies -.\" of this page provided the header is included verbatim, -.\" and in case of nontrivial modification author and date -.\" of the modification is added to the header. -.\" %%%LICENSE_END -.\" .\" Modified, 2003-12-02, Michael Kerrisk, <mtk.manpages@gmail.com> .\" Modified, 2003-09-23, Adam Langley .\" Modified, 2004-05-27, Michael Kerrisk, <mtk.manpages@gmail.com> @@ -17,7 +12,7 @@ .\" address that can appear in the sockaddr_un structure: pathname, .\" unnamed, and abstract. .\" -.TH UNIX 7 2023-02-10 "Linux man-pages 6.03" +.TH UNIX 7 2023-07-15 "Linux man-pages 6.05.01" .SH NAME unix \- sockets for local interprocess communication .SH SYNOPSIS @@ -665,7 +660,7 @@ is returned. .B SIOCINQ is defined in .IR <linux/sockios.h> . -.\" FIXME . http://sources.redhat.com/bugzilla/show_bug.cgi?id=12002, +.\" FIXME . https://www.sourceware.org/bugzilla/show_bug.cgi?id=12002, .\" filed 2010-09-10, may cause SIOCINQ to be defined in glibc headers Alternatively, you can use the synonymous @@ -907,16 +902,16 @@ a null terminator for the string returned in .in +4n .EX void *addrp; - +\& addrlen = sizeof(struct sockaddr_un); addrp = malloc(addrlen + 1); if (addrp == NULL) /* Handle error */ ; memset(addrp, 0, addrlen + 1); - +\& if (getsockname(sfd, (struct sockaddr *) addrp, &addrlen)) == \-1) /* handle error */ ; - +\& printf("sun_path = %s\en", ((struct sockaddr_un *) addrp)\->sun_path); .EE .in @@ -963,14 +958,14 @@ $ /* * File connection.h */ - +\& #define SOCKET_NAME "/tmp/9Lq7BNBnBycd6nxy.socket" #define BUFFER_SIZE 12 - +\& /* * File server.c */ - +\& #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -978,7 +973,7 @@ $ #include <sys/un.h> #include <unistd.h> #include "connection.h" - +\& int main(int argc, char *argv[]) { @@ -989,123 +984,123 @@ main(int argc, char *argv[]) int data_socket; int result; char buffer[BUFFER_SIZE]; - +\& /* Create local socket. */ - +\& connection_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0); if (connection_socket == \-1) { perror("socket"); exit(EXIT_FAILURE); } - +\& /* * For portability clear the whole structure, since some * implementations have additional (nonstandard) fields in * the structure. */ - +\& memset(&name, 0, sizeof(name)); - +\& /* Bind socket to socket name. */ - +\& name.sun_family = AF_UNIX; strncpy(name.sun_path, SOCKET_NAME, sizeof(name.sun_path) \- 1); - +\& ret = bind(connection_socket, (const struct sockaddr *) &name, sizeof(name)); if (ret == \-1) { perror("bind"); exit(EXIT_FAILURE); } - +\& /* * Prepare for accepting connections. The backlog size is set * to 20. So while one request is being processed other requests * can be waiting. */ - +\& ret = listen(connection_socket, 20); if (ret == \-1) { perror("listen"); exit(EXIT_FAILURE); } - +\& /* This is the main loop for handling connections. */ - +\& for (;;) { - +\& /* Wait for incoming connection. */ - +\& data_socket = accept(connection_socket, NULL, NULL); if (data_socket == \-1) { perror("accept"); exit(EXIT_FAILURE); } - +\& result = 0; for (;;) { - +\& /* Wait for next data packet. */ - +\& ret = read(data_socket, buffer, sizeof(buffer)); if (ret == \-1) { perror("read"); exit(EXIT_FAILURE); } - +\& /* Ensure buffer is 0\-terminated. */ - +\& buffer[sizeof(buffer) \- 1] = 0; - +\& /* Handle commands. */ - +\& if (!strncmp(buffer, "DOWN", sizeof(buffer))) { down_flag = 1; break; } - +\& if (!strncmp(buffer, "END", sizeof(buffer))) { break; } - +\& /* Add received summand. */ - +\& result += atoi(buffer); } - +\& /* Send result. */ - +\& sprintf(buffer, "%d", result); ret = write(data_socket, buffer, sizeof(buffer)); if (ret == \-1) { perror("write"); exit(EXIT_FAILURE); } - +\& /* Close socket. */ - +\& close(data_socket); - +\& /* Quit on DOWN command. */ - +\& if (down_flag) { break; } } - +\& close(connection_socket); - +\& /* Unlink the socket. */ - +\& unlink(SOCKET_NAME); - +\& exit(EXIT_SUCCESS); } - +\& /* * File client.c */ - +\& #include <errno.h> #include <stdio.h> #include <stdlib.h> @@ -1114,7 +1109,7 @@ main(int argc, char *argv[]) #include <sys/un.h> #include <unistd.h> #include "connection.h" - +\& int main(int argc, char *argv[]) { @@ -1122,37 +1117,37 @@ main(int argc, char *argv[]) int ret; int data_socket; char buffer[BUFFER_SIZE]; - +\& /* Create local socket. */ - +\& data_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0); if (data_socket == \-1) { perror("socket"); exit(EXIT_FAILURE); } - +\& /* * For portability clear the whole structure, since some * implementations have additional (nonstandard) fields in * the structure. */ - +\& memset(&addr, 0, sizeof(addr)); - +\& /* Connect socket to socket address. */ - +\& addr.sun_family = AF_UNIX; strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) \- 1); - +\& ret = connect(data_socket, (const struct sockaddr *) &addr, sizeof(addr)); if (ret == \-1) { fprintf(stderr, "The server is down.\en"); exit(EXIT_FAILURE); } - +\& /* Send arguments. */ - +\& for (size_t i = 1; i < argc; ++i) { ret = write(data_socket, argv[i], strlen(argv[i]) + 1); if (ret == \-1) { @@ -1160,34 +1155,34 @@ main(int argc, char *argv[]) break; } } - +\& /* Request result. */ - +\& strcpy(buffer, "END"); ret = write(data_socket, buffer, strlen(buffer) + 1); if (ret == \-1) { perror("write"); exit(EXIT_FAILURE); } - +\& /* Receive result. */ - +\& ret = read(data_socket, buffer, sizeof(buffer)); if (ret == \-1) { perror("read"); exit(EXIT_FAILURE); } - +\& /* Ensure buffer is 0\-terminated. */ - +\& buffer[sizeof(buffer) \- 1] = 0; - +\& printf("Result = %s\en", buffer); - +\& /* Close socket. */ - +\& close(data_socket); - +\& exit(EXIT_SUCCESS); } .EE |