ioctl() and file open mode - linux

I wonder if I should open a file using mode O_RDONLY, O_WRONLY or O_RDWR before calling ioctl.
There are reading and writing ioctls. For example _IOC_DIR(VIDIOC_QUERYCAP) will return _IOC_READ.
So I thought when I am calling this ioctl I have to open the file O_RDONLY.
But surprisingly I can open the file with mode=0 and it's still working:
#include <stdio.h> // for printf()
#include <linux/videodev2.h> // for struct v4l2_capability
#include <fcntl.h> // for open()
#include <sys/ioctl.h> // for ioctl()
#include <unistd.h> // for close()
int main(int argc, char** argv)
{
int fd = open("/dev/video0", 0); // mode set to 0
if (fd == -1)
{
printf("open failed\n");
return 1;
}
struct v4l2_capability cap;
if (ioctl(fd, VIDIOC_QUERYCAP, &cap) == -1)
{
printf("ioctl failed\n");
close(fd);
return 1;
}
printf("%s\n", cap.card);
return 0;
}
So my question is how to set the mode properly. Maybe someone got a link to an official documentation.

Related

How does named pipe in Linux works?

I'm trying to use named pipe in Linux, using mkfifo.
Below are codes for sender / reader of named pipe :
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define MSG_SIZE 100
//SENDER
int main() {
char msg[MSG_SIZE] = "FIFO is for first-in, first-out.";
int fd;
int cnt;
if(fd = open("./hello", O_RDWR) < 0) { //fifo acts as a typical file
perror("Failed to make fifo : ");
printf("Error : %d\n", errno);
exit(1);
}
int len = strlen(msg) + 1;
while(1) {
if(write(fd, msg, len) == -1) {
perror("Failed to write into fifo : ");
exit(1);
}
else{
printf("Succeed to write msg!");
break;
}
}
sleep(1);
return 0;
}
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define MSG_SIZE 100
//READER
int main() {
char msg_rc[MSG_SIZE];
int fd;
if(access("./hello",F_OK) == 0) {
fd = open("./hello", O_RDWR);
if(fd < 0) {
perror("Failed to open fifo : ");
exit(1);
}
}
else {
if(fd = mkfifo("./hello", O_RDWR) < 0) {
perror("Failed to open fifo : ");
exit(1);
}
}
printf("Waiting for fifo update");
while(1) {
if(read(fd, msg_rc, MSG_SIZE) == -1) {
perror("Failed to read from fifo : ");
exit(1);
}
printf("Read msg : %s\n", msg_rc);
break;
}
return 0;
}
When I used these codes,
% ./fifoSender
FIFO is for first-in, first-out.Succeed to write msg!%
% ./fifoReader
fifoSender shows msg, and fifoReader shows no msg, which are not desirable behavior.
Any advice will be helpful. Thanks in advance
The main problem is these two lines
if(fd = open("./hello", O_RDWR) < 0) {
if(fd = mkfifo("./hello", O_RDWR) < 0) {
They don't do what you think they do.
Firstly, O_RDWR is a wrong argument for mkfifo. It expects Unix file permissions mask, such as S_IRWU"S_IRWG|S_IRWO, or just 0666.
Secondly and most importantly, since the priority of = is lower than that of <, they are parsed like this:
if(fd = (open("./hello", O_RDWR) < 0)) {
Now, when open("./hello", O_RDWR) is less than 0 (which is highly likely, given that it is either never created or created with an incorrect mode), fd becomes 1, which is the standard output file descriptor.
The correct incantation is
if((fd = open("./hello", O_RDWR)) < 0) { // note () around assignment
if((fd = mkfifo("./hello", 0666)) < 0) {
There are several more subtle shortcomings.
Here is the working code,
// sender
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define PIPE_NAME "./hello"
int main(int argc, char *argv[])
{
const char *message = "FIFO is for first-in, first-out\n";
// Open the pipe for writing
int pipe_fd = open(PIPE_NAME, O_WRONLY);
if (pipe_fd < 0) {
perror("open");
exit(1);
}
// Write data to the pipe
while(1){
int n = write(pipe_fd, message, strlen(message));
if (n < 0) {
perror("write");
exit(1);
}
sleep(1);
}
// Close the pipe
close(pipe_fd);
return 0;
}
//receiver
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#define PIPE_NAME "./hello"
int main(int argc, char *argv[])
{
// Create the named pipe
mknod(PIPE_NAME, S_IFIFO | 0666, 0);
// Open the pipe for reading
int pipe_fd = open(PIPE_NAME, O_RDONLY);
if (pipe_fd < 0) {
perror("open");
exit(1);
}
// Read data from the pipe
char buffer[1024];
while(1){
int n = read(pipe_fd, buffer, sizeof(buffer));
if (n < 0) {
perror("read");
exit(1);
}
// Print the data that was read
printf("Received: %.*s\n", n, buffer);
}
// Close the pipe
close(pipe_fd);
return 0;
}

trying to use pipe(2) with the sort unix tool but not working

I have been struggling to find what I'm doing wrong and I can't seem to find the issue. When I compile the code below, I get an I/O error.
e.g: /usr/bin/sort: read failed: -: Input/output error
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv, char **envp)
{
int fd[2];
pid_t pid;
pipe(fd);
pid = fork();
if (pid == -1) {
exit(EXIT_FAILURE);
}
if (pid == 0) { /* child */
char *exe[]= { "/usr/bin/sort", NULL };
close(fd[0]);
execve("/usr/bin/sort", exe, envp);
}
else {
char *a[] = { "zilda", "andrew", "bartholomeu", NULL };
int i;
close(fd[1]);
for (i = 0; a[i]; i++)
printf("%s\n", a[i]);
}
return 0;
}
dup2(fd[0], 0) in the child.
dup2(fd[1], 1) in the parent.
close the other fd.

How to use static file descriptor using poll linux function call?

poll is running for infinite time interval.
i want poll to hit when some thing is written into file or when file is updated.
but poll is not able to detect when file is written.
#include <stdio.h>
#include <poll.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
char buf[5]="true";
struct pollfd ufds[1];
int rv;
ufds[0].fd = 0;
ufds[0].events = POLLIN;
char *filename="textfile.txt";
ssize_t ret_write,ret_read;
ufds[0].fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 5);
if(ufds[0].fd== -1){
perror("open");
return 3;
}
while(1) {
ret_write= write (ufds[0].fd, &buf, (ssize_t) 5);
if((rv = poll(ufds, 1,-1 )) == -1) perror("select");
else if (rv == 0) printf("Timeout occurred!\n");
else if (ufds[0].revents & POLLIN) {
printf("return hit\n");
read(ufds[0].fd, buf, 5);
}
fflush(stdout);
}
return 0;
}
Your example can't work because the file is not open for reading. Even if the file was opened for reading, the code would not work as intended because poll would return sucessfully on end of file.
What you want is the inotify function. Please try it by yourself first, and ask a question when you have code not working as intended.

Linux: Loopback incoming packets on an interface

What is the best possible way to send packets coming on an interface back to the same interface without changing anything in the packet. I want to have a loopback effect for the actual traffic coming on one of my interfaces e.g eth0
I think you can easily achieve this with Python/Scapy. Something like
sniff(iface="eth0", prn=lambda x: sendp(x, iface="eth0"))
should do it.
I don't think you can do this easily with a physical interface. I used the tap module for this purpose, though. It's quite simple: I create a new tap interface, and my program writes back everything that is read from the device. I used this to test a proprietary network protocol - so it might or might not work for what you intend to do. The code is quite simple:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_tun.h>
#define DEVNAME "gnlo0"
static int tun_alloc(char *dev)
{
struct ifreq ifr;
int fd, ret;
if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
perror("open");
return -1;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP;
if (*dev)
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
ret = ioctl(fd, TUNSETIFF, (void *)&ifr);
if (ret < 0) {
close(fd);
perror("ioctl TUNSETIFF");
return ret;
}
strcpy(dev, ifr.ifr_name);
return fd;
}
int main(int argc, char **argv)
{
int fd = -1;
int ret = 1;
char dev[IFNAMSIZ];
strncpy(dev, DEVNAME, IFNAMSIZ - 1);
printf("opening %s\n", dev);
fd = tun_alloc(dev);
if (fd < 0)
goto out;
char buf[512];
snprintf(buf, sizeof(buf) - 1,
"ip addr flush dev %s; ip link set dev %s up", dev, dev);
if (system(buf) < 0) {
perror("system");
goto out;
}
while (1) {
unsigned char packet[65535];
int len = read(fd, packet, sizeof(packet));
if (len < 0) {
perror("read");
goto out;
}
printf("incoming packet [%d octets]\n", len);
len = write(fd, packet, len);
printf("fed back packet [%d octets]\n", len);
}
ret = 0;
out:
if (fd >= 0)
close(fd);
return ret;
}

Fail to wake up from epoll_wait when other process closes fifo

I'm seeing different epoll and select behavior in two different binaries and was hoping for some debugging help. In the following, epoll_wait and select will be used interchangeably.
I have two processes, one writer and one reader, that communicate over a fifo. The reader performs an epoll_wait to be notified of writes. I would also like to know when the writer closes the fifo, and it appears that epoll_wait should notify me of this as well. The following toy program, which behaves as expected, illustrates what I'm trying to accomplish:
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/stat.h>
#include <unistd.h>
int
main(int argc, char** argv)
{
const char* filename = "tempfile";
char buf[1024];
memset(buf, 0, sizeof(buf));
struct stat statbuf;
if (!stat(filename, &statbuf))
unlink(filename);
mkfifo(filename, S_IRUSR | S_IWUSR);
pid_t pid = fork();
if (!pid) {
int fd = open(filename, O_WRONLY);
printf("Opened %d for writing\n", fd);
sleep(3);
close(fd);
} else {
int fd = open(filename, O_RDONLY);
printf("Opened %d for reading\n", fd);
static const int MAX_LENGTH = 1;
struct epoll_event init;
struct epoll_event evs[MAX_LENGTH];
int efd = epoll_create(MAX_LENGTH);
int i;
for (i = 0; i < MAX_LENGTH; ++i) {
init.data.u64 = 0;
init.data.fd = fd;
init.events |= EPOLLIN | EPOLLPRI | EPOLLHUP;
epoll_ctl(efd, EPOLL_CTL_ADD, fd, &init);
}
while (1) {
int nfds = epoll_wait(efd, evs, MAX_LENGTH, -1);
printf("%d fds ready\n", nfds);
int nread = read(fd, buf, sizeof(buf));
if (nread < 0) {
perror("read");
exit(1);
} else if (!nread) {
printf("Child %d closed the pipe\n", pid);
break;
}
printf("Reading: %s\n", buf);
}
}
return 0;
}
However, when I do this with another reader (whose code I'm not privileged to post, but which makes the exact same calls--the toy program is modeled on it), the process does not wake when the writer closes the fifo. The toy reader also gives the desired semantics with select. The real reader configured to use select also fails.
What might account for the different behavior of the two? For any provided hypotheses, how can I verify them? I'm running Linux 2.6.38.8.
strace is a great tool to confirm that the system calls are invoked correctly (i.e. parameters are passed correctly and they don't return any unexpected errors).
In addition to that I would recommend using lsof to check that no other process has that FIFO still opened.

Resources