user855443
user855443

Reputation: 2940

readFile throws "permission denied" for readable file

I try to read all files from my disk and fail with a specific file. What can impede reading a file when it is a regular file and is readable (both checked in the code and with stat)?

stat "/proc/1/task/1/maps"
  File: /proc/1/task/1/maps
  Size: 0           Blocks: 0          IO Block: 1024   regular empty file
Device: 4h/4d   Inode: 209680      Links: 1
Access: (0444/-r--r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-05-05 11:34:57.840873751 +0200
Modify: 2017-05-05 11:34:57.840873751 +0200
Change: 2017-05-05 11:34:57.840873751 +0200
 Birth: -

The code is

module Main  

import System.Posix.Files

main = do 
         putStrLn "tets_maps - trying to read file /proc/1/task/1/maps" 
         let fn = "/proc/1/task/1/maps" :: FilePath 
         status <- getSymbolicLinkStatus fn
         readable <-  fileAccess  fn  True  False  False   
         putStrLn  "tets_maps - status regularFiel and readable"
         putStrLn .         show . isRegularFile $ status 
         putStrLn  . show $ readable 

         res1 :: String  <-  readFile  fn 
         putStrLn "tets_maps - result" 

The output is :

readProblem: /pro`c/1/task/1/maps: openFile: permission denied (Permission denied)`

Why a permission denied when testing before that the file has read access? I am aware that the file has size 0 (is emtpy) and I expect an empty string back, but not an error. What do I not consider?

Upvotes: 1

Views: 410

Answers (1)

Sibi
Sibi

Reputation: 48664

This is not a Haskell issue, but is a Linux specific thing. Even though it shows that you have the permission to read, it won't allow:

~ $ ls -lh /proc/1/task/1/maps
-r--r--r-- 1 root root 0 May  5 16:56 /proc/1/task/1/maps
~ $ cat /proc/1/task/1/maps
cat: /proc/1/task/1/maps: Permission denied

The files inside /proc are kernel related and they have special cases in it. You can read this discussion to get more details on this.

Upvotes: 4

Related Questions