Read structure contents using read() system call - c

#include "common.h"
#include <string.h>
struct buffer
{
int no;
char name[20];
};
int main()
{
struct buffer buf;
struct buffer read_buf;
int fd;
if((fd = open("read_write.txt",O_CREAT|O_RDWR,S_IRUSR|S_IWUSR)) < 0)
{
PRINT_ERROR(errorbuf);
}
buf.no = 10;
strcpy(buf.name,"nitin");
if(write(fd, &buf, sizeof(struct buffer)) < 0)
{
PRINT_ERROR(errorbuf);
}
printf("Written successfully\n");
/* Add code here to read the content of the structure into 'read_buf' */
exit(0);
}
common.h
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
char errorbuf[20];
#define PRINT_ERROR(errorbuf) \
do \
{ \
sprintf(errorbuf,"%s:%d",__FILE__,__LINE__); \
perror(errorbuf); \
exit(-1); \
}while(0);
I have written a structure into the file. But i am getting confused on how to retrieve each element of the structure written earlier into the object 'read_buf'. Kindly tell me how to do this.
Thanks

lseek(fd,0,SEEK_SET);
read(fd,&buf,sizeof(struct buffer);
Will work, but there are some other things you will have to worry about.
This is not portable.
You will have to worry about structure packing on different builds.
You will have endian problems cross platform.
Windows you may need O_BINARY.
It is nearly always better to repackage to structure in to a known format (with known endianess) so that you can reliably read the data back.

You read it back like this:
ssize_t bytes_read = read(fd, &buf, sizeof buf);
if(-1 == bytes_read)
; // handle read error
if(sizeof buf != bytes_read)
; // handle incomplete read

Writing/reading binary structures to/from files is not portable (depending on the platform architecture, structure padding etc). To make your program robust, you can use something like XDR.

Related

Again a question about reading a directory file with C

How we can use C functions open/read/close properly in order to read a linux directory?. I notice serveral others asked this question before, about on reading directories in C, also that several ones suggest the use of readdir/opendir/closedir functions, I know, but RnK book (the C programming language) in fact define or introduces those readdir/opendir/closedir/ functions, the problem is that read() function "not read" properly directories; it returns -1 instead of the number of bytes readed. Is there any change into the actual read() C function that produces this or it is necesary modifications to read()?
Here is my example code:
#include <sys/types.h>
#include <unistd.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
int main(){
int fd;
size_t nbytes;
ssize_t bytes_read;
char buf[20];
fd=open(".",O_RDONLY,0);
nbytes = sizeof(buf);
bytes_read = read(fd, buf, nbytes);
printf("Buf size: %ld file descriptor: %d bytes readed:
%ld\n",nbytes,fd,bytes_read);
}
Compiling above code in ubuntu linux read gives bytes_read = -1.
Thanks in advance

Shared memory data using a header file

I'm doing an assignment for class in which we need to create some shared memory that can be accessed from 2 different programs. There is a header file called shm.h that has the kinds of data we need to be able to share. So far my code looks like this.
shm.h
#ifndef SHM_H
#define SHM_H
//<Define an enum called StatusEnus with the enumerations "INVALID", "VALID"
and "CONSUMED">
#define enum StatusEnus{INVALID, VALID, CONSUMED} StatusEnus
//<Define a typedef structure with the enum above and an "int" variable
//called "data">
#define struct ShmData{StatusEnus status; int data;}ShmData;
#define SIZE 8
#endif
server_template
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>
#include "shm.h"
int main(int argc, char* argv[])
{
//void* memPtr;
int retVal = 0;
//<Confirm argc is 2 and if not print a usage string.>
if(argc != 2){
printf("Please enter 2 arguments5");
}
/*<Use the POSIX "shm_open" API to open file descriptor with
"O_CREAT | O_RDWR" options and the "0666" permissions> */
//returns -1 on error
int file = shm_open("sharedMem", O_CREAT | O_RDWR, 0666);
if (file == -1) retVal =-1;
/*<Use the "ftruncate" API to set the size to the size of your
structure shm.h>*/
retVal = ftruncate(file,SIZE);
//<Use the "mmap" API to memory map the file descriptor>
void* data = mmap(0, SIZE,PROT_WRITE | PROT_WRITE, MAP_SHARED, file, 0);
/*<Set the "status" field to INVALID>
<Set the "data" field to atoi(argv[1])>
<Set the "status" field to VALID>*/
ShmData->status = INVALID;
ShmData->data = atoi(argv[1]);
ShmData->status = VALID;
printf("[Server]: Server data Valid... waiting for client\n");
while(ShmData->status != CONSUMED)
{
sleep(1);
}
printf("[Server]: Server Data consumed!\n");
/*<use the "munmap" API to unmap the pointer>
<use the "close" API to close the file Descriptor>
<use the "shm_unlink" API to revert the shm_open call above>*/
printf("[Server]: Server exiting...\n");
return(retVal);
}
So my goal is to be able to access the fields like ShmData->StatusEnus from the server but also then be able to access these files from a separate program. However as it stands I keep getting to errors either related to the deceleration of ShmData. How do I make sure that the shared memory I am creating contains that enum and int field from my structure?

Setting Immutable Flag using ioctl() in C

I have attempted to make a script that creates a file and then sets it as immutable similar to the chattr +i command for linux. The script compiles (with gcc), runs and the file is created. However the file itself is not immutable and can be removed with a simple rm -f. I have attempted to stacktrace where chattr is called and I found a function called ioctl. I then used what little information I could gather and came up with what I have below. I narrowed it down from ext2_fs.h but it just doesn't seem to work. I've clearly overlooked something.
Updates to previous entry: Compiles but returns -1 on ioctl() function. Bad address shown with perror().
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
int main()
{
FILE *fp;
char shovel[16] = "I have a shovel!";
fp = fopen("/shovel.txt", "w+");
fwrite(shovel, sizeof(shovel[0]), sizeof(shovel)/sizeof(shovel[0]), fp);
ioctl(fileno(fp), FS_IOC_SETFLAGS, 0x00000010);
fclose(fp);
}
Any help appreciated.
You are using the right ioctl command, but you're passing it the wrong arguments.
The manpage for ioctl_list(2) shows that FS_IOC_SETFLAGS expects to receive a pointer to int (an int *), yet you're passing it an integer literal (hence the Bad Address error).
The fact that you don't to any error checking whatsoever is also not helping.
The correct flag to pass to FS_IOC_SETFLAGS is a pointer holding the value EXT2_IMMUTABLE_FL, which is defined in ext2fs/ext2_fs.h (some older / different Linux distributions seem to have it under linux/ext2_fs.h), so you'll need to #include <ext2fs/etx2_fs.h>. Make sure to install e2fslibs-dev (and probably you'll need linux-headers too).
This code is working:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <ext2fs/ext2_fs.h>
int main()
{
FILE *fp;
char shovel[16] = "I have a shovel!";
if ((fp = fopen("shovel.txt", "w+")) == NULL) {
perror("fopen(3) error");
exit(EXIT_FAILURE);
}
fwrite(shovel, sizeof(shovel[0]), sizeof(shovel)/sizeof(shovel[0]), fp);
int val = EXT2_IMMUTABLE_FL;
if (ioctl(fileno(fp), FS_IOC_SETFLAGS, &val) < 0)
perror("ioctl(2) error");
fclose(fp);
return 0;
}
Remember to run this as root.
UPDATE:
As Giuseppe Guerrini suggests in his answer, you might want to use FS_IMMUTABLE_FL instead, and you won't need to include ext2_fs.h:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
int main()
{
FILE *fp;
char shovel[16] = "I have a shovel!";
if ((fp = fopen("shovel.txt", "w+")) == NULL) {
perror("fopen(3) error");
exit(EXIT_FAILURE);
}
fwrite(shovel, sizeof(shovel[0]), sizeof(shovel)/sizeof(shovel[0]), fp);
int val = FS_IMMUTABLE_FL;
if (ioctl(fileno(fp), FS_IOC_SETFLAGS, &val) < 0)
perror("ioctl(2) error");
fclose(fp);
return 0;
}
The main problem is that the ioctl wants a pointer to the mask, not a direct constant. You have to define a int variable, store the mask (0x10) in it and pass its address as third argument of ioctl.
Also, I'd add some hints:
other programs to change attributes are used to use low-level I/O directly (open, close...). Also, the file is usually opened with O_RDONLY.
Use FS_IMMUTABLE_FL istead the raw constant.
Get the current attribute mask first (FS_IOC_SETFLAGS) and mask it with the new flag, so other settings are not lost by the service.

Reading/Writing errno or stack variable causes segfault. Why?

I have a really weird problem here, and haven't managed to find an answer online.
It appears after debugging with printf statements that a segfault ocurred when trying to read errno. Commenting problemed lines out one by one as they cause segfault resulted in having to comment out every reference to errno, after a readdir() call reaches the end of the directory stream and returns NULL.
Even then, the code then segfaults later when trying to access another automatic variable, file_count.
What is going on? Is this a stack overflow? How do I make it stop?
The code is below, if you feel the need to wade through it. All the problematic references to errno are removed, and the program segfaults after successfully executing the third second last line: printf("printing file_count\n");.
EDIT1: Here's a GDB backtrace:
#0 0xc95bf881 in strcpy () from /usr/lib/libc.so.1
#1 0x08051543 in dir_get_list (user=0x8047b88 "user1") at maildir.c:231
#2 0x08050f3e in main (argc=4, argv=0x80479f4) at maildir.c:43
END EDIT1
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#define MAX_FILENAME_LENGTH 255
#define MAX_USERNAME_LENGTH 40
#define MAX_PASSWORD_LENGTH 20
typedef int bool;
#define true 1
#define false 0
struct files_struct{
/*The number of email messages in a maildir.*/
int count;
/*A pointer to an array of pointers to the strings of the filenames. */
char **FileNames;
/*A pointer to an array of ints that give the corresponding size of the file.*/
int *FileSize;
};
typedef struct files_struct FilesStruct;
void dir_set_path(char* path);
bool check_user(char* username, char* pass);
FilesStruct* dir_get_list(char* user);
void delete_mail(char* user, char* filename);
char* get_file(char* user, char* filename);
FilesStruct* dir_get_list(char* user){
char maildir_name[MAX_FILENAME_LENGTH];
DIR * maildir_fd;
struct dirent *maildir_p;
strcpy(maildir_name,"./");
strncat(maildir_name,user,MAX_USERNAME_LENGTH);
strcat(maildir_name,"/");
if((pthread_mutex_lock(&maildir_root_mutex))<0)
perror("ERROR on locking maildir_root_mutex");
printf("Opening directory ""%s""\n",maildir_name);
if((maildir_fd = opendir(maildir_name))==NULL)
perror("ERROR on opendir");
int file_count = 0;
/* scan over entire directory, counting number of files to that data can be properly malloced */
while(1){
if((maildir_p = readdir(maildir_fd))==NULL){
closedir(maildir_fd);
printf("breaking loop\n");
break;
}
char file[MAX_FILENAME_LENGTH+1];
strcpy(file,maildir_p->d_name);
printf("File %d: '%s'\n",file_count+1,maildir_p->d_name);
/* if the file is a file other than an email */
if(!strcmp(".",file)||!strcmp("..",file)||!strcmp("pass",file)||!strcmp(".svn",file)){
printf("Continuing without incrementing file_count\n");
continue;
}
file_count++;
}
printf("%u\n",maildir_fd);
printf("printing file_count\n");
printf("%d",file_count);
printf("file_count printed successfully");
/* Additional code OMITTED */
I came across this recently. In my instance it was that another module had declared:
int errno = 0;
as a global, instead of #including errno.h. Any code that used the "proper" errno would immediately segfault.

write() returns -1 when writing to I2C_SLAVE device

I've read through the Linux kernel documents on i2c and written a code to try to replicate the command i2cset -y 0 0x60 0x05 0xff
The code that I've written is here:
#include <stdio.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <string.h>
int main(){
int file;
file = open("/dev/i2c-0", O_RDWR);
if (file < 0) {
exit(1);
}
int addr = 0x60;
if(ioctl(file, I2C_SLAVE, addr) < 0){
exit(1);
}
__u8 reg = 0x05;
__u8 res;
__u8 data = 0xff;
int written = write(file, &reg, 1);
printf("write returned %d\n", written);
written = write(file, &data, 1);
printf("write returned %d\n", written);
}
When I compile and run this code I get:
write returned -1
write returned -1
I've tried to follow exactly what the docs tell me, my understanding is that the address is set first with the call to ioctl, then I need to write() the register and then the data that I want sent to the register.
I've also tried to use use SMbus, but I can't get my code to compile using this, it complains at the linking stage that it can't find the functions.
Have I made any mistakes in this code? I'm a beginner to i2c and don't have a lot of experience with c either.
EDIT: errno give the following message: Operation not supported. I am logged in as root on this machine though, so I don't think it can be a permissions thing, although I may be wrong.
The way I got around this problem was to use SMBus, in particular the functions i2c_smbus_write_byte_data and i2c_smbus_read_byte_data. I was able to use these functions to successfully read and write to the device.
I did have a little trouble finding these functions, I kept trying to download libraries using apt-get to install the appropriate header files. In the end I simply downloaded the files smbus.c and smbus.h.
Then the code I needed was:
#include <stdio.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include "smbus.h"
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
int main(){
int file;
file = open("/dev/i2c-0", O_RDWR);
if (file < 0) {
exit(1);
}
int addr = 0x60;
if(ioctl(file, I2C_SLAVE, addr) < 0){
exit(1);
}
__u8 reg = 0x05; /* Device register to access */
__s32 res;
res = i2c_smbus_write_byte_data(file, reg, 0xff);
close(file);
}
Then if I compile the smbus.c file: gcc -c smbus.c and myfile: gcc -c myfile.c, then link them: gcc smbus.o myfile.o -o myexe I get a working executable that runs my I2C command. Ofcourse, I have smbus.c and smbus.h in the same directory as myfile.c.
In C, you can check the content of the errno variable to get more details into what went wrong. It is automatically declared when including errno.h and you can get a more descriptive text by calling strerror(errno).
Have you checked that you had write access to /dev/i2c-0 ?

Resources