Computing > Embedded Computing

Raspbery, Open and read my txt file stored in /boot from C.

(1/2) > >>

luiHS:
 
Hello.
Is there an easy way to open and read a text file stored in Raspeberry's /boot, from a C program?

I need to have a text file in /boot for my C program to read, and based on a parameter run some routines or others.

The problem is that creating the text file in /boot does not allow me to change the owner from root to pi, nor does it allow me to read the file from my C program.

guenthert:

--- Quote from: luiHS on June 08, 2021, 05:15:58 pm ---
Hello.
Is there an easy way to open and read a text file stored in Raspeberry's /boot, from a C program?

I need to have a text file in /boot for my C program to read, and based on a parameter run some routines or others.

The problem is that creating the text file in /boot does not allow me to change the owner from root to pi, nor does it allow me to read the file from my C program.

--- End quote ---
    For a file to be read in *nix it needs to be readable by the user (euid) of the process attempting the read.  The owner of the file is relevant only in as much file permissions apply.  Make the file readable by everyone ("others" or "all") e.g. by executing `chmod a+r /boot/file_of_interest.txt` with superuser privileges (e.g. by prefixing aforementioned command with `sudo `).

dave j:
/boot is the mount point for the boot partition, which is formatted as FAT32. It needs to be FAT32 because that is the only filesystem that the Pi's boot mechanism supports. FAT filesystems don't support Unix style permissions so everything on the partition is assumed to have the permissions given to the mount point.

You won't be able to write a file to the partition unless you're root but you should be able to read as any user. (I've just tried creating a text file in /boot as root and reading it as user pi on one of my Pis and it worked.)

janoc:

--- Quote from: luiHS on June 08, 2021, 05:15:58 pm ---The problem is that creating the text file in /boot does not allow me to change the owner from root to pi, nor does it allow me to read the file from my C program.

--- End quote ---

You need to mount the VFAT partition with correct options. Open the /etc/fstab file in an editor and check the line:


--- Code: ---/dev/mmcXXX /boot  vfat  defaults 0 2
--- End code ---

What you need to do is to change that line to something like:


--- Code: ---/dev/mmcXXX /boot  vfat  defaults,umask=000 0 2
--- End code ---

Then save it and reboot.

That will allow anyone to open and write files on that partition. And no, you can't change owner of an individual file, FAT doesn't have a concept of file owners. You can only remap the files to appear with a different owner, e.g. using the uid and gid options in /etc/fstab.

E.g. the following will make all files on that filesystem appear as being owned by the user with the UID 1000 (which should be the user 'pi' on Raspbian).

--- Code: ---/dev/mmcXXX /boot  vfat  defaults,uid=1000 0 2
--- End code ---

You can find the user IDs in /etc/passwd or by typing:

--- Code: ---id username
--- End code ---
(replace "username" with the user you want to check)


guenthert:
   For just reading the file as non-privileged user the mount options don't need to be changed (and allowing non-privileged users to modify files under /boot is, er, not generally advisable).  Defaults for VFAT imply that files are created 0755, i.e. readable by all (among other things).  So, what exactly is the problem?

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version