Archive for January, 2007

UNIX malloc() debugging

Tuesday, January 30th, 2007

If you suspect memory leaks or corruption in your program due to the improper use of pointers, here are several things you can use, ordered by simplicity:

  • GNU libc’s malloc check. Set the MALLOC_CHECK_ environment variable to 1 to spam stderr when corruption is detected, or 2 to call abort() when corruption is detected.
  • Dmalloc. A debug malloc library that works as a drop-in replacement for the system malloc(), realloc(), and free() functions.
  • Electric Fence. This is a replacement malloc library similar to Dmalloc. Its manner of operation is to use the hardware memory protection of the processor to cause an immediate segmentation fault if the process attempts to write to memory preceding or following an allocated region.
  • Valgrind‘s memcheck tool. Valgrind runs the program under an emulated CPU, so the execution is quite a bit slower. However, it catches all memory corruption and pointer errors at runtime and prints a backtrace. It also prints memory leak and other statistics at the program exit. Invoke with valgrind –tool=memcheck <program>.

Here is another list I ran across.

AFS file locking with Perl

Tuesday, January 30th, 2007

Recent versions of OpenAFS obey advisory locks. This means that lockf() and fcntl() locking will work properly across the network (flock() will never work since it does not support network filesystems). The historical catch with AFS locking is that attempts to lock a locked file will return EAGAIN in every case, not only in the “try lock” case.

You can use the Fcntl module of Perl to accomplish the locking, but File::lockf (available on CPAN) makes it much simpler if all you need is exclusive locking. The main difference is that lockf will not lock a file that is opened readonly since it only supports exclusive locks. If you need specific reader/writer locks you must use Fcntl instead.

Since lockf won’t function on a file opened readonly, we open read-write, and use O_APPEND and omit O_TRUNC to ensure that we do not accidentally overwrite any part of the file through the file handle used for the locking. You must also ensure that the file’s current position points to 0, otherwise the lock will be placed past the end of the file and be nonfunctional.

use Fcntl ':seek';
use File::lockf;
use IO::File;

my $lockfh = IO::File->new;
#Alternate method:
#sysopen($lockfh, "$fname", O_WRONLY|O_APPEND|O_CREAT) or die "Couldn't open $fname: $!";
$lockfh->open(">>$fname") or die "Couldn't open $fname: $!";
seek $lockfh,0,SEEK_SET or die "Couldn't seek: $!";

my $lock = new File::lockf(\$lockfh);
my $locked = 0;

while (1) {
        my $now_string = localtime;
        if ($lock->tlock == 0) {
                if ($locked) {
                        warn "$now_string: lock on $fname acquired\n";
        else {
                warn "$now_string: can't immediately write-lock $fname ($!), trying again in 60 seconds...\n";
        if ($locked > 10) {
                die "$now_string: giving up after $locked tries to lock $fname\n";

my $fh = IO::File->new;
$fh->open(">$fname") or die "Couldn't open $fname: $!";

... rest of program here ...

close $fh;
if ($lock->ulock != 0) { warn "Failed to release lock on $fname" };
close $lockfh;
exit 0;

Apache filters, gzip, and WordPress

Sunday, January 28th, 2007

Don’t enable WordPress’s gzip function unless:

  • you have a good reason not to use mod_deflate instead, such as wanting to compress RSS files
  • you are not using any filters of type AF_FTYPE_RESOURCE or higher in the Apache filter chain to modify the HTML afterwards.

As soon as WordPress has gzipped the content, it is impossible for any other filter to modify it. It will also be very easy to forget that you have set this option, and then you will go nuts like me trying to figure out why mod_deflate seems to be invoked so early in the filter chain — when in fact the content is being compressed by PHP itself.

Why can’t I use Kerberos TGT forwarding when SSH’ing from a remote network?

Sunday, January 28th, 2007

A few possibilities:

  • Clock skew. Make sure ntp is installed.
  • OpenSSH versions differ too greatly, install the same version on both ends
  • You do not have both ‘GSSAPIAuthentication yes’ and ‘GSSAPIDelegateCredentials yes’ on the client ssh_config
  • The server does not have a /etc/krb5.keytab containing the key for host/
  • The server does not have ‘GSSAPIAuthentication yes’ and ‘GSSAPIKeyExchange yes’ in sshd_config
  • The client has a different reverse-DNS idea of the server’s fully-qualified domain name than the server does. Very common if the server is using a private DNS server but the client is using a public one. In this case, the client needs a ticket for host/ but due to the reverse DNS lookup, requests one for host/ instead. Either the same name server needs to be used by both server and client, or client needs to add server to his hosts file.
  • The server has an incorrect ordering on the line in the hosts file. It should be followed by FQDN followed by hostname followed by localhost.

Oil pressure versus oil level

Thursday, January 25th, 2007

Why do many people report a lower oil pressure reading when their crankcase oil level is low?

We have to ignore the possibility of the pump displacing anything but oil in order to have a sound theoretical discussion. But it is possible that there is a large proportion of air trapped in the oil from frothing as it returns to the pan, or that the pickup tube — or its seal to the pump or block — has a pinhole in it that is covered when the oil level is higher (this should produce a relatively sudden change in pressure as the pinhole is uncovered, as opposed to a gradual change).

We also have to assume that the pump is not allowing oil to drain back out. But this is known to be false; an oil pump turned slowly enough will pump nothing but air. So there is not a linear correlation between pump speed and the volume of fluid pumped, because the faster the pump turns, the smaller the time period in each rotation where drainback could occur. The drainback effect will be more severe when the oil that wants to drain back is far above the level of the oil in the pan. So it would be expected that a worn oil pump allowing more drainback will produce lower output pressure when the level of the pan is low.

If we can assume that we are only pumping oil (a non-compressible liquid) and that a fixed volume of oil is pumped per rotation, assuring us that the same volume of oil is pumped through the engine no matter what the oil level is, then we must attribute a change in the oil pressure reading to lower oil viscosity.

What can lower oil viscosity?

  • compositional breakdown.

Compositional breakdown occurs as the oil is contaminated with moisture and hydrocarbons from blow-by, and as the oil polymers themselves break down from heat and shear.

The same amount of blow-by per firing cycle on a hot motor enters the crankcase regardless of the oil level, and the motor relies on being run hot in order to evaporate the moisture (but not the hydrocarbon contamination) from the crankcase. The oil is thus broken down and contaminated by time spent normally driving, and the effect of this contamination is worse when the engine spends a greater proportion of its total operating time warming up from cold. When the oil level is low, a smaller volume of oil must bear the same amount of contamination and thermal/shear stress that causes its breakdown.

It is expected then that a low level of used oil will produce lower oil pressure than a low level of new oil. It is also expected that the effect on oil pressure of being low a certain amount of oil will be more pronounced on engines with a smaller crankcase capacity. But it is also expected then that on any engine, being low on oil will not produce an effect on oil pressure if the oil in the crankcase is new (not contaminated or broken down in any way).

  • Temperature.

Oil viscosity is reduced when its temperature is raised. Circulated oil accumulates heat as it is sprayed on the cylinder walls, but radiates it through the oil cooler and oil pan, until an overall thermal equilibrium is reached. It is important to note that if a particular volume of oil was constantly recirculated, it would reach equilibrium at the temperature of the engine, while if it was seldom recirculated, it would reach equilibrium at the external temperature of the oil pan. Thus, the temperature of the oil has a high negative correlation with the time that the oil spends sitting in the oil pan.

The average time that a just-circulated volume of oil spends in the oil pan is determined by the amount of other oil that is in the pan below it. The other oil is cooler than the oil that has just circulated, so the just-circulated oil radiates its heat through the other oil and through the pan while it waits to be recirculated.

So, it appears safe to state that a low oil level is a causal factor in the increased temperature of the oil, and thus lower overall viscosity and lower oil pressure. This effect, unlike the effect of contamination and breakdown, should be present even if the crankcase is filled with new oil. Again, it should be predictable that the effect will be more pronounced in a smaller crankcase for a given volume of missing oil. But, while it is a causal factor, how much of a factor is it really? After all, the cold pressure and warm pressure in a full crankcase hardly varies enough to register on the needle. It seems like the temperature variance just doesn’t cause that big of a hit on the oil pressure.

Revisiting the air trapped in the oil, we would expect the same proportion of air trapped in the oil if the oil level is low, because the same surface area where air could potentially escape exists no matter what the oil level is. (Of course, if the oil level is far too high, a disproportionate amount of air will be trapped, but that is due to the crankshaft frothing the oil and not due to normal oil circulation.)

So if an engine is filled with a too-low level of new oil, we should expect a lower than normal oil pressure reading at all times due to:

  • Small leaks in the oil pickup that would be masked by a higher oil level
  • An amount of drainback relative to how worn the oil pump is

We should expect a lower than normal oil pressure reading at operating temperature — but possibly a vanishingly small difference — due to:

  • Temperature modification of the oil viscosity

We should expect a lower than normal oil pressure reading after a fixed number of miles of usual driving due to:

  • Accelerated contamination of the total volume of oil (occurring at a rate proportional to the percentage of time that the engine spends running below operating temperature)
  • Accelerated rate of polymer chain breakdown from shear and thermal stress. Thermal stress may not be a major issue. Since a low oil level causes an increased equilibrium oil temperature when the engine is running at operating temperature, we can also predict that the thermal breakdown of the total volume of oil when running at operating temperature will occur at a faster rate when the oil level is low. But as reasoned above, the equilibrium temperature probably will not rise enough to significantly affect the oil viscosity, so the effects of any increased thermal stress on the long-term viscosity of the oil may be negligible.

Selling a lot of IBM / Lexmark Model M keyboards

Tuesday, January 16th, 2007

These keyboards are awesome.

If you ever see a bunch of them being tossed out as surplus, pick them up. They can be sold $20 apiece on Ebay easily. Shipping is quite easy via USPS Priority Mail: Simply take two of the long type flat rate boxes, and insert one into the other. Surround the keyboard with peanuts and it’s good to go! Use any keyboards which are physically broken or missing lots of keys as donors for key caps, key blocks and controller boards. The spring mechanism itself is difficult to replace properly so don’t sell any keyboards with stuck or mushy springs, use them as parts donors.

They clean up with laundry detergent and a rag. Do not simply put in the dishwasher unless you open it up and allow to completely rinse and dry the controller board. The buyer can be responsible for this.

If they have AT cords, AT-PS2 adapters can be had for $1-2 ( or the buyer can purchase his own PS2 cable ( You could also make your own cord, the weird connector is a SDL “Shielded Data Link” jack and is available at Mouser and friends.

Some Pentium 4 and faster systems require resistors to be added to the data and clock lines:

Unicomp provides warranted repair services: and also sells newly manufactured buckling spring keyboards:

Clickykeyboards sells new old-stock Model M keyboards:

Chevy Cavalier 97 oil light flickering

Monday, January 15th, 2007

This might be the worst of the worst fears. You change your oil before a long road trip. You get off the highway and tooling around the neighborhood, there goes your oil light when idling at a stop sign. Kill the car and restart it, and all seems well for a few minutes and there it is again. Get out, check the oil which is full, and wonder what the heck is going on. Assume the new oil filter must have been bad and change the filter, only to end up with the same intermittent oil light. Well, I shouldn’t say intermittent, because it happens at low RPM and at high engine temperature, the same preconditions that would give you low oil pressure if you had a weak oil pump or excessive bearing clearance.

Well before you start fearing the worst, if you are sure the oil level is fine, the oil is not contaminated and that there can be nothing wrong with the filter, the next place to look is the oil pressure switch that sends the signal to the oil light. The way the switch works is that oil pushes on it until it turns off. Sufficient push on the “inside” of the switch from high oil pressure will turn off the light, while insufficient push from low oil pressure will allow it to remain on. Well, if the pressure switch is dripping oil from the plug side, that is your first sign something is wrong with it. The diaphragm internal to the switch is supposed to keep the oil on the engine side. If the oil gets through onto the electrical side, it will foul the connectors and give an intermittent connection. Coincidentally, the conditions for the oil getting through the diaphragm into the plug are the same as those that would give you an oil light on a tired engine: fresh oil and a hot engine at idle. Also, when the diaphragm begins to leak, that means that some of the pressure on the engine side is escaping through the diaphragm instead of pushing on it, meaning that the oil pressure will be sensed consistently lower than its actual value, and so the oil light will come on at times when the real pressure is actually fine.

The fix is to replace the switch. It costs 12 bucks at a parts store. You need a 1 1/16 inch deep socket to remove the old sender and install the new one. If the new sender did not come with teflon or similar sealer on the threads, use appropriate plumbing thread sealer before installing. Also, clean out the electrical plug with solvent and blow clear with compressed air to clear any oil from it that might foul the connection.

Honda wishbone suspension

Monday, January 15th, 2007

I was surprised to find out that on the “wishbone” front suspension in the 90-93 Accord among other Honda cars, the caster and camber are not adjustable. Hence, you do not have to worry about having an alignment done after replacing struts, ball joints, control arms, bushings, etc. If you want to replace the tie rod ends, you can mark their position on the tie rod to avoid needing the toe set. All in all, a very convenient suspension to work on, and since mine had a broken spring and two bad upper ball joints, that is a good thing.

An economical way to have the struts done is to buy all the replacement parts yourself (preferably brand name struts such as Monroe or Gabriel at the low end and KYB at the high end, and brand name suspension components with new fasteners and a lifetime warranty from Moog). I buy all of this stuff from O’Reilly who also offers a farm discount if you are into that sort of thing. Pack the new parts in a sturdy cardboard box with the old struts and start calling around the “Mom n pop” repair shops. Eventually you will find one that will allow you to bring in the box’o’struts and they will rebuild them with the new hardware for 20-25 dollars a piece.

So instead of a $1000+ suspension and brake job, I got
$93 – 2x Monroe strut cartridges
$25 – 2x Monroe upper strut mounts 901940 (cheap because these struts have no bearings as in a normal MacPherson suspension)
$40 – 2x Moog strut boot kit K9306
$72 – Moog front springs 9258
$174 – 2x Moog upper ball joint K9815,K9816
$20 – Brakebest front pads
$13 – Brakebest rear rebuilt shoes
$12 – Brakebest rear hardware kit
$5 – Brake fluid
$45 – Strut labor
Free – Working on a day off
$499 less than half price, and lifetime replacement on everything except the strut cartridges.

Loaned a 2 jaw “pitman arm” puller from Autozone to separate the ball joints from their respective spindles.

The stabilizer bar bushings were close to new already from previous owner. I did not replace the lower control arm bushings, lower ball joints, or tie rod ends. Those can all be done at a later point without disturbing the alignment. Smear the ball joint boots, strut boots, tie rod ends, axle boots and bushings with non-petroleum grease and everything should be good to go another 100,000 miles at least.

Compressed file archives

Monday, January 15th, 2007

Would like all of the following:
– Fast listing of archive contents with file metadata
– Fast retrieval of an individual file
– Fast retreival of a specific address in the input stream

Each compressed block should have metadata such as starting block address, filename and MIME type, and length of block.

Christianity and its patriarchy

Tuesday, January 2nd, 2007

Christian doctrine asserts that woman was created from man, and therefore concludes woman should be subservient to man.

What if it were pointed out that all egg cells have a XX pair of chromosomes, making every zygote by default female? The only thing that can be done to change this default state is for a male to contribute the Y chromosome that could potentially lead to the embryo becoming male at some indeterminate point after conception.

So, using the same logic that derives patriarchy from the creation myth of Adam and Eve, we can instead utilize known scientific fact to construct an argument for matriarchy!

Sexual Differentiation @ Wikipedia