Year 2015 in review

The WordPress.com stats helper monkeys prepared a 2015 annual report for this blog.

Here’s an excerpt:

A San Francisco cable car holds 60 people. This blog was viewed about 790 times in 2015. If it were a cable car, it would take about 13 trips to carry that many people.

Click here to see the complete report.

Linux file attributes and creation time :)

I didn’t know about the Linux ‘chattr‘ command which allows you to display and set file attributes. Using this, I learnt a neat trick on how to make a file immutable, even by the root user.

To make a file immutable, do:

chattr +i <filename>

To remove the immutable attribute, do:

chattr -i <filename>

To list the current file attributes, do:

lsattr foo/bar

It’s that simple 😎

This will change only the ctime for the file, and not its mtime. (details here: ctime vs mtime). Another interesting thing, in th Raspberry Pi context, is the atime file information. Essentially, atime should reflect the last time a file has been accessed. However, this is not very useful and only serves to increase the number of writes to a partition, every time a file is accessed. This is a dampener for performance esp. in embedded systems. Hence, on the Raspberry Pi, we mount partitions with the “noatime” attribute within fstab.

In Linux, the file creation time didn’t exist until the ext4 filesystem appeared. ctime originally did mean creation time, but it always reflected the time when a file’s metadata was changed (attributes, permissions, etc). With ext4, the creation time was stored in the file’s inode, under the field ‘crtime’. In order to get a file’s creation time we need to lookup the file’s inode, and the use stat and debugfs to find the required field. debugfs is required since there still doesn’t exist any kernel API to get crtime from stat. (birth time)

ls -i <filename>
debugfs -R 'stat <inode_number>' /dev/sdNN

(replace sdNN with the actual path to the device where the file resides). Look the the ‘crtime‘ field in the output.

Linux Scheduler

The default Linux process scheduler is the Completely Fair Scheduler (CFS). To change the default for Raspberry Pi, change the ‘elevator’ kernel parameter within /boot/cmdline.txt.

By default, it’ll have “elevator=deadline”. You can change deadline to noop or cfq. However, the default seems to be the best for interactive performance.

Things I’ve been reading about

C++11:

  • Rvalue reference and move semantics
  • perfect forwarding, noexcept
  • ‘auto’, for-loop by range
  • initializer lists, lambdas

Javascript:

  • asynchronous calls, callbacks, events
  • AJAX requests, JSONP, Cross-origin Resource Sharing (CORS)
  • closures

Google Apps Script, CSS/Bootstrap

ABI, vtables, stack padding, C++ name mangling  …

My head is spinning 😱

 

Apple Macbook Multi-output audio

multi-output-audio

I was trying to watch John Oliver with my fiance today and we had the eternal problem of 2 people trying to share a pair of headphones. I had a Bluetooth headset too and I wondered if we could stream audio over both these. Voila! Someone had already worked out this neat trick here:

http://bit.ly/1LseGRo

The last part about about sending output to a particular device is done by going into System Preferences > Sound > Output and selecting the multi-output device there.

Who calls your main()?

I was curious as how to a C program in executed by the OS and came across this interesting article:

Create your C library
The file “crt0” is created during the linking stage, which sets up the working environment and finally calls the main() within the program. The  _start function is able to set up the standard library, call the global constructors, and call exit(main(argc, argv)).

Bash shell tricks

The bash shell has so many nifty tricks. They can come in handy in order to save time with repeated tasks, recover data, etc.

  1. My set of customizations for bash can be found on github. With those customizations, you can make find easier, colorize diff, man and prevent clobbering when using output redirection. I especially love the colored man pages.

  2. If you want to sudo-ize the previous command, use;

sudo !!
  1. rename utility: I never knew this! It makes renaming multiple files using regex patterns so much easier!

  2. In scripts, instead of the awkward test command ‘[‘, use ‘(( … ))’ and ‘[[ … ]]’ in its place. It makes logical and string comparison so much easier. (ref: modern bash test operators)

  3. To get your current shell name, do

ps -p $$

or

echo $0

 

Some more examples can be found here: bash trick commands

There are also some intricacies like the difference between rm and unlink: rm vs unlink

Feel free to suggest more in your comments.

Note: The file .bashrc is not loaded by bash at startup, by default. So it must be included in either the file .profile or .bash_profile.

ssh tricks

I must have used ssh for the past 15 years but I never knew this trick. So if you want to ssh into a remote server and run some command on it, and exit, you don’t actually need to login into the console and type the commands. It can be done by injecting commands through a ssh connection (and piping the output back too).

So if you want to ssh into the raspberry pi and do a simple ‘ls’, use this:

ssh  pi@host -p port#  'ls'

If you use key authentication and have saved the keys, your login can be automated without the need for password (more on that later).

If you’d like to get the output and store it on the local host, use regular linux redirection:

ssh  pi@host -p port# 'ls' > test.txt

Isn’t this great 🙂

More details can be found here:

Run commands through ssh

Pipe output from local to remote server using ssh