Linux open named pipe

Example of using named pipes in Linux shell (Bash)

One of the best examples of a practical use of a named pipe.

Another useful behavior is using netcat as a proxy. Both ports and hosts can be redirected. Look at this example:

nc -l 12345 | nc www.google.com 80 

Port 12345 represents the request.

This starts a nc server on port 12345 and all the connections get redirected to google.com:80 . If a web browser makes a request to nc , the request will be sent to google but the response will not be sent to the web browser. That is because pipes are unidirectional. This can be worked around with a named pipe to redirect the input and output.

mkfifo backpipe nc -l 12345 0backpipe 

While this serves as a useful example, if you’re building proxies of this nature you’d generally be better off using socat, which is a Unix command-line tool that essentially implements a very fully featured domain-specific language for building proxies and (to a more limited degree) servers. This particular example would be done more reliably and efficiently with socat STDIO TCP:www.google.com:80 .

mkfifo named_pipe echo "Hi" > named_pipe & cat named_pipe 

The first command creates the pipe.

The second command writes to the pipe (blocking). The & puts this into the background so you can continue to type commands in the same shell. It will exit when the FIFO is emptied by the next command.

The last command reads from the pipe.

It’s customary for «#» to refer to a root prompt (ie, a prompt in a root shell). There’s nothing here that would require running in a root shell.

The echo will block so this won’t run if executed in the same shell unless second line is place in the background with an ending & .

Open two different shells, and leave them side by side. In both, go to the /tmp/ directory:

mkfifo myPipe echo "IPC_example_between_two_shells">myPipe 
while read line; do echo "What has been passed through the pipe is $"; done 

First shell won't give you any prompt back until you execute the second part of the code in the second shell. It's because the fifo read and write is blocking.

You can also have a look at the FIFO type by doing a ls -al myPipe and see the details of this specific type of file.

Next step would be to embark the code in a script!

Creating a named pipe

On Unix-likes named pipe (FIFO) is a special type of file with no content. The mkfifo command creates the pipe on a file system (assigns a name to it), but doesn't open it. You need to open and close it separately like any other file.

Using a named pipe

Named pipes are useful when you need to pipe from/to multiple processes or if you can't connect two processes with an anonymous pipe. They can be used in multiple ways:

    In parallel with another process:

$ echo 'Hello pipe!' > pipe_name & # runs writer in a background $ cat pipe_name Hello pipe! 
$ # open the pipe on auxiliary FD #5 in both ways (otherwise it will block), $ # then open descriptors for writing and reading and close the auxiliary FD $ exec 5<>pipe_name 3>pipe_name 4&- $ $ echo 'Hello pipe!' >&3 # write into the pipe through FD #3 . $ exec 3>&- # close the FD when you're done $ # (otherwise reading will block) $ cat  
$ handler() < >cat > exec 3 trap - USR1 # unregister signal handler (see below) > unset -f handler writer # undefine the functions > > $ $ exec 4<>pipe_name 3&- $ trap handler USR1 # register handler for signal USR1 $ $ writer() < >if ; then > kill -USR1 $PPID # send the signal USR1 to a specified process > echo 'Hello pipe!' > pipe_name > fi > > $ export -f writer # pass the function to child shells $ $ bash -c writer & # can actually be run sequentially as well $ Hello pipe! 

Destroying a named pipe

The pipe itself (and its content) gets destroyed when all descriptors to it are closed. What's left is just a name.
To make the pipe anonymous and unavailable under the given name (can be done when the pipe is still open) you could use the rm con­sole com­mand (it's the opposite of mkfifo command):

Источник

What are Named Pipes or FIFO in Linux/Unix systems?

Pipes were meant for communication between related processes. We Cannot use pipes for unrelated process communication. Then to achieve unrelated processes communication, the simple answer is Named Pipes. Even though this works for related processes, it gives no meaning to use the named pipes for related process communication.

Unlike pipe, we can use single named pipe that can be used for two-way communication (communication between the server and the client, plus the client and the server at the same time) as Named Pipe supports bi-directional communication.

Another name for named pipe is FIFO (First-In-First-Out). Let us see the system call (mknod()) to create a named pipe, which is a kind of a special file.

#include #include #include #include int mknod(const char *pathname, mode_t mode, dev_t dev);

This system call would create a special file or file system node such as ordinary file, device file, or FIFO. The arguments to the system call are pathname, mode and dev. The pathname along with the attributes of mode and device information. The pathname is relative, if the directory is not specified it would be created in the current directory. The mode specified is the mode of file which specifies the file type such as the type of file and the file mode as mentioned in the following tables. The dev field is to specify device information such as major and minor device numbers.

File Type Description File Type Description
S_IFBLK block special S_IFREG Regular file
S_IFCHR character special S_IFDIR Directory
S_IFIFO FIFO special S_IFLNK Symbolic Link
File Mode Description File Mode Description
S_IRWXU Read, write, execute/search by owner S_IWGRP Write permission, group
S_IRUSR Read permission,owler S_IXGRP Execute/search permission, group
S_IWUSR Write permission, owner S_IRWXO Execute/search permission, group
S_IXUSR Execute/search permission, owner S_IROTH Read permission, others
S_IRWXG Read, write, execute/search by group S_IWOTH Write permission, others
S_IRGRP Read permission, group S_IXOTH Execute/search permission, others

File mode can also be represented in octal notation such as 0XYZ, where X represents owner, Y represents group, and Z represents others. The value of X, Y or Z can range from 0 to 7. The values for read, write and execute are 4, 2, 1 respectively. If needed in combination of read, write and execute, then add the values accordingly.

Say, if, 0640, then this means read and write (4 + 2 = 6) for owner, read (4) for group and no permissions (0) for others.

This call would return zero on success and -1 in case of failure. To know the cause of failure, check with errno variable or perror() function.

#include #include int mkfifo(const char *pathname, mode_t mode)

This library function creates a FIFO special file, which is used for named pipe. The arguments to this function is file name and mode. The file name can be either absolute path or relative path. If full path name (or absolute path) is not given, the file would be created in the current folder of the executing process. The file mode information is as described in mknod() system call.

This call would return zero on success and -1 in case of failure. To know the cause of failure, check with errno variable or perror() function.

Let us consider a program of running the server on one terminal and running the client on another terminal. The program would only perform one-way communication. The client accepts the user input and sends the message to the server, the server prints the message on the output. The process is continued until the user enters the string “end”.

Let us understand this with an example −

Step 1 − Create two processes, one is fifoserver and another one is fifoclient.

Step 2 − Server process performs the following −

  • Creates a named pipe (using system call mknod()) with name “MYFIFO”, if not created.
  • Opens the named pipe for read only purposes.
  • Here, created FIFO with permissions of read and write for Owner. Read for Group and no permissions for Others.
  • Waits infinitely for message from the Client.
  • If the message received from the client is not “end”, prints the message. If the message is “end”, closes the fifo and ends the process.

Step 3 − Client process performs the following −

  • 1) Opens the named pipe for write only purposes.
  • 2) Accepts the string from the user.
  • 3) Checks, if the user enters “end” or other than “end”. Either way, it sends a message to the server. However, if the string is “end”, this closes the FIFO and also ends the process.
  • 4) Repeats infinitely until the user enters string “end”.

Two-way Communication Using Named Pipes

The communication between pipes are meant to be unidirectional. Pipes were restricted to one-way communication in general and need at least two pipes for two-way communication. Pipes are meant for inter-related processes only. Pipes can’t be used for unrelated processes communication, say, if we want to execute one process from one terminal and another process from another terminal, it is not possible with pipes. Named pipe is meant for communication between two or more unrelated processes and can also have bi-directional communication.

Already, we have seen the one-directional communication between named pipes, i.e., the messages from the client to the server. Now, let us take a look at the bi-directional communication i.e., the client sending message to the server and the server receiving the message and sending back another message to the client using the same named pipe.

Step 1 − Create two processes, one is fifoserver_twoway and another one is fifoclient_twoway.

Step 2 − Server process performs the following −

  • Creates a named pipe (using library function mkfifo()) with name “fifo_twoway” in /tmp directory, if not created.
  • Opens the named pipe for read and write purposes.
  • Here, created FIFO with permissions of read and write for Owner. Read for Group and no permissions for Others.
  • Waits infinitely for a message from the client.
  • If the message received from the client is not “end”, prints the message and reverses the string. The reversed string is sent back to the client. If the message is “end”, closes the fifo and ends the process.

Step 3 − Client process performs the following −

  • Opens the named pipe for read and write purposes.
  • Accepts string from the user.
  • Checks, if the user enters “end” or other than “end”. Either way, it sends a message to the server. However, if the string is “end”, this closes the FIFO and also ends the process.
  • If the message is sent as not “end”, it waits for the message (reversed string) from the client and prints the reversed string.
  • Repeats infinitely until the user enters the string “end”.

Источник

Читайте также:  Как запустить докер контейнер linux
Оцените статью
Adblock
detector