Gentoo Archives: gentoo-user

From: Enrico Weigelt <weigelt@×××××.de>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] march in /etc/make.conf
Date: Fri, 21 Jul 2006 11:59:46
Message-Id: 20060721115125.GF4971@nibiru.local
In Reply to: Re: [gentoo-user] march in /etc/make.conf by Alexander Skwar
1 * Alexander Skwar <listen@×××××××××××××××.name> wrote:
2
3 Hi,
4
5 > Well, depends on how you define "open files are overwritten". On
6 > Linux, it is like you say. But on Windows and HP-UX, you CANNOT
7 > replace a file, if it's still opened somewhere. Eg. you cannot
8 > replace /bin/sh. Instead, a new file will be created and after
9 > a reboot, the new file will be moved in place (that's how it
10 > works on HP-UX, on Windows you cannot overwrite opened files.).
11 >
12 > What I mean: On Linux, you can replace /bin/sh even if it used.
13 > You cannot overwrite the used inodes/blocks, that's absolutely
14 > correct, but that's not what I meant.
15
16 ACK.
17
18 I'll try to explain the logic behind a little bit more detailed:
19
20 On Linux (and probably other Unix'ish kernels), files are not
21 identified by names, but inode-id's. The name is just an pointer
22 to the file, just like an DNS-name->IP-addr mapping ;-)
23 Many such pointers to some file may exist. Only when all pointers
24 are removed (open fd's also considered as an pointer) the file
25 gets actually removed. That's why the syscall used for removing
26 a file is called unlink(): it just removes the given name but
27 does not actually delete it.
28
29 When you intend to replace some file, you've got two choices:
30 (from the kernel's view)
31
32 a) open the existing file, probably truncate it and write new the
33 data. if someone has opened this file, he will see the changes
34 you made. If the file has been mmap'ed to some process, it will
35 see the changes immediately in its address space. therefore files
36 should be locked (at least the used regions), so an accidental
37 overwrite (which may cause ugly crashes) can be prevented. AFAIK
38 on Linux, .TEXT segments are always locked when the get mapped in
39 (you you get "busy" when trying to write there)
40
41 b) create a new file under the old name. either by renaming or
42 unlink'ing the old file. here you've got no problem w/ other
43 processes holding the file opened, since you actually have
44 two different files. the new file only gets accessed when
45 you (re)open the file and thus let the kernel do an new
46 name->inode lookup.
47
48
49 BTW: when coding installers for running systems (which in fact
50 is the case in >90%). Do not use cp (at least GNU coreutils),
51 since it *overwrites* the existing file (case a). This will
52 fail on used .TEXT (=executable code) files, since they're locked
53 and most likely produce problems with other open files.
54 If writing the file does not run almost immediately you should
55 first write to some temporary file (on the same filesystem!)
56 and then do a quick rename (unlink(..) ; rename(..)) so nobody
57 tries to use unfinished files.
58
59 And be very careful you reinstall you (running) installer !
60 Several years ago, glibc had a critical problem, which screwed up
61 your system on install over the running system: it first removed
62 the /lib/ld.so and /lib/libc* symlinks and then recreated them by
63 separate calling /bin/rm and /bin/ln binaries. If they weren't
64 linked statically, it killed itself - /bin/ln coulnd'n be executed
65 since libc was unusable in this moment. Using -s flag to ln instead
66 of calling rm did the trick.
67
68 I have no idea how careful emerge is here ...
69
70
71 cu
72 --
73 ---------------------------------------------------------------------
74 Enrico Weigelt == metux IT service - http://www.metux.de/
75 ---------------------------------------------------------------------
76 Please visit the OpenSource QM Taskforce:
77 http://wiki.metux.de/public/OpenSource_QM_Taskforce
78 Patches / Fixes for a lot dozens of packages in dozens of versions:
79 http://patches.metux.de/
80 ---------------------------------------------------------------------
81 --
82 gentoo-user@g.o mailing list