Posts Tagged ‘Linux’

Difference between dup(0) and open(“/dev/fd/0″,…);

I believe APUE (2nd ed.; Sec. 3.16) is not correct.

APUE says fd = open("/dev/fd/0", mode); is equivalent to fd = dup (0);, and mode is completely ignored. It seems this is the case in Solaris, but wrong in Linux. (I don’t have access to other Unices at this moment.)

A test program:

01 #include <unistd.h>
02 #include <fcntl.h>
03
04 int main ()
05 {
06     close (0);
07     printf ("%dn", open ("a.txt", O_RDONLY)); // Should be 0
08     //int f2 = open ("/dev/fd/0", O_WRONLY);
09     int f2 = dup(0);
10     printf ("%dn", f2);
11     write (f2, "Hello worldn", 12);
12     return 0;
13 }

Let’s run the program with an empty a.txt. Certainly the write function in Line 11 is going to fail.

Now, let’s comment out Line 9 and uncomment line 8 and try it again.

First I ran it in Solaris, the write call still failed. The behavior is like what APUE tells us.

Try it again in Linux – It was successful!

It seems that in Linux, /dev/fd/0 is considered by open as nothing but a normal symlink to a.txt. So it returns a completely new descriptor instead of a duplicate of the old.

Let’s try it again with a shell script:

rm -f a.txt
touch a.txt
exec 0<a.txt
exec 3>/dev/fd/0
echo 'Hello world' >&3
cat a.txt

Run it in Linux (with DASH or BASH): Both outputed ‘Hello world’.

Run it in Solaris (with Bourne shell and BASH): Both failed, outputting nothing (Bourne shell) or failing with ‘Bad file number’ (BASH).

Conclusion:

(1) Solaris handles /dev/fd/.. specially, as APUE tells us;

(2) Linux simply consider /dev/fd/0 a symlink to the actual file.

(I’ll try later how Linux handles open("/dev/fd/0",mode) if the descriptor is an anonymous pipe or socket or something else that a normal symlink is unable to link to.

Kernels used in the above tests:

Linux: Linux desktop 2.6.28-gentoo #4 SMP Mon Jan 12 17:39:23 CST 2009 x86_64 Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz GenuineIntel GNU/Linux

Solaris: SunOS caesar 5.8 Generic_117350-51 sun4u sparc SUNW,Ultra-80 Solaris

Tags: , ,

gspca in Linux 2.6.27

only works with v4l2, but not v4l. So it can lead to problems – programs using v4l gives strange pictures as well as annoying error messages.

My webcam worked with gspcav1 and Linux 2.6.26, but it failed in Linux 2.6.27 (with its in-kernel gspca drivers):

>>cmcapture err -1
cvsync err
: Invalid argument
cmcapture: Invalid argument

Solution: Install libv4l, and use a command like this:LD_PRELOAD=/usr/lib/libv4l/v4l2convert.so skype.

Reference
Linux kernel bug #11860.

Tags: ,

Migrating to EXT4

Ext4, the successor to ext3 which was formerly known as ext4dev, is marked stable in Linux kernel 2.6.28, meaning the Linux kernel team now recommends using ext4 in production.

To convert a file system from ext3 to ext4, use

tune2fs -O extents /dev/DEV

and remount the file system as ext4. (Two e2fsck runs are recommended before and after tune2fs.) Some documentations also include the -E test_fs option. This is not necessary now since ext4 is no longer experimental.

Finally do not forget to modify /etc/fstab.

An ext4 file system created this way is not a “true” ext4 – the extents feature, the main advantage of ext4 comapred to ext3, is not automatically applied to old files. New files created afterwards are in the extents format.

Unlike the 100% backward compatibility of ext3 with ext2, an ext4 file system can no longer be mounted as if it were an ext3, unless the extents feature is disabled. (If you want to disable extents, why not simply use ext3?)

Tags: , ,

su without password

Google returns a lot of meaningful results for “sudo without password” and “ssh without password.” I don’t know why googling “su without password” gives no useful info.

To allow a user to become root with su without entering the password, edit /etc/pam.d/su.

For example, in Gentoo (should be the same or similar in other distros) uncommenting the following line allows users in group wheel to su without password:

auth sufficient pam_wheel.so use_uid trust

Tags: ,

html2text

I use an init script to log in my ISP. The script is like this

#!/bin/sh
wget -O – –post-data ‘username=…&password=…’ ‘http://….’ |
html2text | tee /tmp/$$.txt
fgrep -q ‘already logged in’ /tmp/$$.txt || exit 1
unlink /tmp/$$.txt

(The username and password are public in this building, so I need not worry about the clear-text password.)

It was strange that /tmp/$$.txt apparently contained the string “already logged in” but the script always exited abnormally (fgrep fails).

I finally found the reason by opening /tmp/$$.txt with bvi (a vi-style hexadecimal editor): the string in the file was actually like this: “You abare alrbreadbdy loggedbd in.” (“b” stands for backspace character.) That’s why fgrep could not find the string..

html2text sucks.

Tags:

Scilab

In my Core 2 running 64-bit Linux, it insists on compiling one of the Fortran sources with “gfortran -march=athlon64 -mfpmath=sse -msse2 -m3dnow …”, regardless of the FFLAGS environment variable. (Other Fortran files honor FFLAGS, nevertheless…)

It does not matter much if you insist on use “-march=athlon64”, but “-m3dnow” is really a problem since Intel never ever supported 3DNow!

Tags: , ,

Yuking resumes FCITX development

Wonderful!

FCITX (Free Chinese Input Toy for X) was one of the two major Chinese input methods in Linux. Yuking mentioned in his recent post that he had just learned to use SVN. It was amazing he had created such a successful tool without using any version control system… (The source files contain approximately 30k lines)

The codes were criticized to be “amateur” – mainly based on his use of Chinese configuration files and Chinese comments (both in GBK, not UTF-8). These criticism directly caused Yuking to stop development last year. But I still have no idea why it is not reasonable to use Chinese character in a Chinese input method…

Tags: ,

Why VirtualBox window is transparent…

I have been using Compiz Fusion and VirtualBox for a while. They were both working well except I could not switch VirtualBox to fullscreen or seamless mode. I did not change any setting either in Compiz Fusion or VirtualBox, but these two days all black pixels have become transparent. What’s the problem?… (If I shut down Compiz Fusion, VirtualBox displays coreectly.)

Tags: , ,

Shrink an LVM partition

Gentoo’s Documentation says it is easier to increase the size of an LVM partition than to shrink it, so they recommend starting with smaller partitions and then increase them as needed.

Of course they are not saying shrinking is impossible, but only less easy. The most important thing is the FS must be reduced BEFORE the volume, or the FS will get damaged.

Example: Shrink partiton /dev/vg/home with ext3 filesystem from 30G to 12G:
e2fsck /dev/vg/home (resize2fs requires a check before resizing)
resize2fs /dev/vg/home 10G
(resize FS to 10G)
lvreduce -L12G /dev/vg/home
(reduce partition)
resize2fs /dev/vg/home (extend FS to the whole partition)

Someone (I’m not sure who..) recommended doing two resizes like this claiming it is safer.

Tags: , ,

Another VMware problem

Forgot about this when writing the previous post..

I had an error like this when trying to start VMware Workstation in a Linux host:

*** attempt to put segment in horiz list twice
/opt/vmware/workstation/lib/bin/vmware: symbol lookup error: /usr/lib/libglade-2.0.so.0: undefined symbol: g_return_if_fail_warning

The workaround is to export an environment variable before running vmware:

export VMWARE_USE_SHIPPED_GTK=’yes’

The GTK libraries in my system is binary incompatible with the ones used by the VMware team, so VMware must use the library files shipped with it instead of those in my system. Similar compatibility problems are not rare when it comes to a closed-source application that is dynamically linked against an open-source library…

Reference
[1] HOWTO: Install vmware workstation 6.0.3 build 80004 (March 14) on Sabayon x86-3.5 Loop2-r2
[2] VMWARE_USE_SHIPPED_GTK=force vmware

Tags: ,