5

In my linux machine if i try to copy /proc/stat it is creating 0 byte file. but if i do cat /proc/stat it has data. but the size always shows as 0.

 cp /proc/stat statfile

is creating zero byte file. If i write a program to copy then it worked. why is it so ?

  int main() 
  {
    std::ifstream procFile("/proc/stat");
    std::ofstream outfile("statfile");
    char buf[1024];
    while (!procFile.eof() && procFile.is_open())
    {
            procFile.getline(buf, 1024);
            outfile << buf<<endl;
    }

    procFile.close();
    outfile.close();
    return 0;
  }
Claudio
  • 10,614
  • 4
  • 31
  • 71
Dinesh Reddy
  • 775
  • 1
  • 11
  • 25
  • 1
    This may be related to [this](http://lists.debian.org/debian-user/2009/04/msg01491.html) and [this](http://lists.gnu.org/archive/html/bug-coreutils/2009-04/msg00157.html) (apparently the result of a shortcoming in `cp`'s handling of short reads, not calls to `stat()` returning 0). – Frédéric Hamidi Nov 04 '13 at 12:49
  • 1
    On my Ubuntu 13.04 with cp (GNU coreutils) 8.20, `/proc/stat` is copied correcty. Could you post the output of `strace cp /proc/stat statfile`? – Michael Foukarakis Nov 04 '13 at 12:55
  • too big to paste it here @MichaelFoukarakis. – Dinesh Reddy Nov 04 '13 at 13:49
  • You can post the lines after `open('/proc/stat'...`, they shouldn't be too many. – Michael Foukarakis Nov 04 '13 at 13:50
  • stat("/proc/stat", {st_mode=S_IFREG|0444, st_size=0, ...}) = 0 stat("statfile", 0x7fff36cd33b0) = -1 ENOENT (No such file or directory) open("/proc/stat", O_RDONLY) = 3 fstat(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0 open("statfile", O_WRONLY|O_CREAT|O_EXCL, 0444) = 4 fstat(4, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0 close(4) = 0 close(3) = 0 close(1) = 0 close(2) = 0 exit_group(0) = ? – Dinesh Reddy Nov 05 '13 at 03:43
  • [Click Here](https://gist.github.com/DineshReddyK/9b2dbbe454daa5fa29eb) this would help. @Michael – Dinesh Reddy Nov 05 '13 at 18:03

2 Answers2

8

/proc is a pseudo-filesystem. It indicates a size of 0 for the file /proc/stat.

This is why copying doesn't work (cp does first look at the size of the file it has to copy), but you can easily read the information and write it back to a file.

$> cat /proc/stat > statfile

This was fixed in GNU coreutils 7.3.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Theolodis
  • 4,977
  • 3
  • 34
  • 53
  • @Theolodis : As Michael Foukarakis said, it is even working for me in my Ubuntu. if cp does first look at the size then how come it is working in ubuntu? could you please explain. – Dinesh Reddy Nov 05 '13 at 17:24
  • @Dinesh Ubuntu might use a custom version of cp, I am not aware of that... Those programs are open-source and anyone can modify them. Linux is not Linux :) – Theolodis Nov 06 '13 at 12:20
0

Use find to do a cp being as root user as shown,

find /proc/cpuinfo -type f -exec cp -iv '{}' /home/dir/ \;


Here is my attempt,

~/cpuinfo$ ls
~/cpuinfo$ sudo find /proc/cpuinfo -type f -exec cp -iv '{}' /home/esunboj/
  stackoverflow/cpuinfo/ \;
  `/proc/cpuinfo' -> `/home/esunboj/stackoverflow/cpuinfo/cpuinfo'
~/cpuinfo$ ls   
  cpuinfo

Also, as said /proc is a pseudo file system meaning the files are not stored on disk but have hooks to kernel. Probably you can read and/or write each entry from user space like a normal file operations. Usually different monitoring programs when they fopen a file, its now kernel job to find the appropriate driver which handles the task and assign it to that driver. Where in case of /proc its proc system in the kernel which will read the memory entities with the cost of system load involved.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46