28 June 2014

Gentoo configure error: C compiler cannot create executables

If configure phase failes with this message, then most likely the dev-libs/mpc package is removed. I removed it accidentally. Afterwards I was unable to emerge anything. Here is a simple solution for the issue.

Download stage3 tar ball from a Gentoo mirror, unpack it and copy usr/lib/libmpc.so* over /usr/lib. Then run ldconfig and try to build something.

Hopefully, the post will help another gentoer like me who will also wonder what's going wrong with the C compiler.

21 June 2014

How to capture audio stream using Pulseaudio and VLC

Some Internet radio stations offer URLs for streaming with popular software, some don't. I'll show a way to capture currently runnig audio stream to file using Pulseaudio and VLC.

Step 1. Pick an output

$ pactl list short | grep RUNNING | awk '{print $2}'
(The list of devices also be obtained via VLC GUI: View - Playlist - Audio capture.) Let's pick the second one.

Step 2. Write a Bash script

In the following script replace the value of pulseaudio_stream variable with 'pulse://YOUR_OUTPUT', where YOUR_OUTPUT is the output chosen in the first step.

#!/bin/bash -
set -o nounset


usage() {
  IFS= read -r errstr <<-errstr
  Captures current pulseaudio stream $pulseaudio_stream and saves it to a file
  Usage: $0 <output-path>
  Example: $0 ~/Music/radio.mp3

  echo >&2 "$errstr"
  exit 1

if [[ $# < 1 ]]; then


nvlc "$pulseaudio_stream" ':sout=#transcode{vcodec=none,acodec=mp3,ab=128,channels=2,samplerate=44100}:duplicate{dst=std{access=file,mux=raw,dst='$output'}}'

Save it to ~/scripts/bash/rec-audio.sh and make it executable.

Step 3. Use it

~/scripts/bash/rec-audio.sh ~/Music/radio.mp3

Currently running audio will be recorded to the file until you quit VLC.

31 March 2014

Slim and Systemd

I just want to drop in a warning from Arch linux documentation:
Warning: Currently SLiM is not fully compatible with Systemd, resulting in
various problems on a second login. See Display manager#Incompatibility with
systemd for an outline of these problems.
I had really experienced such kind of issues after switching from OpenRC to Systemd! Text artifacts in GTK-based applications, SSH keyring issues, system stall on reboot or shutdown is not full list of bugs I got with Slim running under Systemd. All of a sudden I see the Arch docs. Replacing Slim with LightDM fixed all of these issues at a time!

22 March 2014

An alternative to gksudo/kdesudo

The gksudo and kdesudo commands are okay, if we whether work in GNOME or KDE environments, or are satisfied with the number of required dependencies. However, these pacakges are pretty cumbersome, if we want just a lightweight GUI version of sudo, especially on non-GTK/non-KDE environments like Xfce. In this post I'll show a simple GUI front-end I use on my Xfce setup. Steps:
  • Install x11-ssh-askpass or similar.
  • Create ~/bin/xsudo executable with the following contents:
    #!/bin/bash -
    sudo -A $@
  • Set the $SUDO_ASKPASS environment variable in some initialization script (e.g. ~/.xinitrc, ~/.bash_profile, ~/.bashrc etc.):
    export SUDO_ASKPASS="/usr/bin/x11-ssh-askpass"
    Alternatively, put the following into /etc/sudo.conf file:
    Path askpass /usr/bin/x11-ssh-askpass
    (Make sure to adjust the path.)
Now we can use xsudo command just as if it were gksudo or kdesudo.

21 February 2014

Extracting unescaped part from Nginx location in UTF-8

The Nginx location matches unescaped URI. However, to capture a Unicode part of URI is somewhat tricky. First of all, we have to prepend our regular expression with (*UTF8), e.g:
location ~* "(*UTF8)^/img/(.+)\.(jpg|jpeg|gif|png))$" {
  # ...
Now we can use ordinary $1, $2, ..., $N variables for corresponding regular expression groups. In most cases they work well. For instance, we can successfully pass them to a proxy URI. The parts are getting url-encoded, and everything works fine..., except some cases when we use them as file/directory names, e.g.:
proxy_store $my_variable;
The problem is that a UTF-8 character can be converted to 3..12 characters! A 3-character sequence in Chinese like '艾弗吉' gets converted to 27-character long string '%E8%89%BE%E5%BC%97%E5%90%89'. Obviously, we can reach Ext4's maximum filename length limit of 255 characters with just 28 Chinese symbols. The following captures basename as unescaped UTF-8 string:
location ~* "(*UTF8)^/img/(?<basename>(.+)\.(jpg|jpeg|gif|png)))$" {
  # $basename is unescaped string in Unicode
Now we can pass $basename to directives accepting file/directory names. Hope this helps someone.

25 July 2013

Fixing PHP start-up error: «Unable to load dynamic library»

Suppose you have a PHP extension compiled as ext.so shared library. Let's assume that it depends on sockets extension(which is compiled as sockets.so library file). It means that PHP should load sockets.so before ext.so. Sometimes it doesn't, however. In this case running any PHP script you'd catch a startup error similar to the following:

/usr/lib64/php/modules/event.so: undefined symbol: php_sockets_le_socket in Unknown on line 0

Current version of PHP(at the time of writing 5.5.0 was the latest stable version) loads extensions from php.ini downwards line by line. So the order of "extensions" in php.ini specifies the order of loading of the shared libraries. Most GNU/Linux distributions use the --with-config-file-scan-dir= configure option, which specifies a directory where PHP looks for additional configuration. Usually this directory contains a file per extension. These files are loaded in alpha-numeric order. It means that our ext.ini will be loaded before sockets.ini, as well as ext.so will be loaded before sockets.so, which is not what we wanted!

People in the PHP Group promised to work on this issue. To fix this issue pro tem just rename the ini files in the config file scan directory. Thus our ext.ini has to be renamed to, z-ext.ini, for instance.

02 June 2013

How to factory reset Samsung Galaxy Note 2

I spent a day scraping my head in attempts to reset my phone to factory settings. Common instructions didn't help. So I'd like to share a very simple way to reset a Samsung Galaxy Note 2. It should work for other similar headsets, too.

The problem

Samsung Galaxy Note 2 GT-N7100 headset has been locked. Neither SIM card, nor Internet are available. There is no way to access menu/options. The only available options are: emergency calls and an invitation to enter Google account.

So the only way to factory reset is to use combinations mentioned in all these instructions like "hold power and volume up buttons until you see menu with 'factory reset' option" etc. But the boot-up menu has just "self-test" options without a tip on factory reset.

The fix

If you have no 'factory reset' option on the boot-up menu, and fully aware that the reset will remove all user data(contacts, custom apps, settings etc.); if the only thing you want is a working phone, then do the following:

  • Download and install fastboot utility. The simplest way is to download Zip file from this forum thread.
  • Move to the directory with fastboot and run:
    $ ./fastboot -w reboot
    < waiting for device >
  • Hold down the MENU button
  • Hold down the POWER button for 10..20 seconds until you see the Samsung logo with a red text: "fastboot mode". After you entered fastboot mode, release the phone buttons.
  • Hold down the VOLUME UP key while connecting the phone with computer using USB cable. Fastboot utility should detect the phone now. It resets the phone and reboots it:
    $ ./fastboot -w reboot
    < waiting for device >
    erasing 'userdata'...
    OKAY [  1.610s]
    erasing 'cache'...
    OKAY [  0.605s]
    finished. total time: 2.219s

I did it on Gentoo x86_64. I'm not sure if my udev rules helped. To be on the safe side, I'd put the following to /etc/udev/rules.d/51-android.rules file:

SUBSYSTEM=="usb", ATTR{idVendor}=="1782", MODE="0666", GROUP="plugdev" 
SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", MODE="0666", GROUP="plugdev"
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", MODE="0666", GROUP="plugdev"
SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", MODE="0666", GROUP="plugdev"
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", MODE="0666", GROUP="plugdev"
Don't forget to restart the udev daemon after creating/modifying the rules.

Done! :-) I hope this post saves someone's time.