Wednesday, December 14, 2011

PostgreSQL 'C' Function to execute shell command

How to run a shell command from a PostgreSQL function?

A typical example is to send sms through 'gnokii' with AT drivers.

1) Install gnokii with the following command
sudo apt-get install gnokii


2) Gnokii config shall be in /etc/gnokiirc with appropriate contents. I have used the following for Nokia 6681 connected to server by USB cable. Most of the mobiles will work with this configuration.
[global]

port = /dev/ttyACM0
model = AT
connection = serial


3) For compiling the PostgreSQL 'C' function, install the server development package by issuing the command
sudo apt-get install postgresql-server-dev-8.4


4) Create a shell script to Send SMS through gnokii, make it executable and place it in the common PATH preferably in /usr/local/bin/
  Contents of send_sms.sh:
#!/bin/bash

echo $2 | /usr/bin/gnokii --config /etc/gnokiirc --sendsms $1

if [ $? -eq 0 ]  # Test exit status of "gnokii" command.
then
  exit 0
else  
  exit -1
fi

5) Create a PostgreSQL C function to execute any shell command:
   Contents of exec_shell_command.c

/* POSTGRESQL C Function to execute shell command */
#include <string.h>
#include "postgres.h"
#include "fmgr.h"

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

/* by value */

PG_FUNCTION_INFO_V1(exec_shell_command);

Datum
exec_shell_command(PG_FUNCTION_ARGS)
{
    text *p1 = PG_GETARG_TEXT_P(0); 
    char *command = VARDATA(p1);
    int retVal = system(command);
    PG_RETURN_INT32(retVal);
}

6) Compile the above c file to generate object code.
gcc -c -fpic -I/usr/include/postgresql/8.4/server exec_shell_command.c -o exec_shell_command.o
The above code is a single line command.

7) Generate shared library from the object code generated in step 6.
gcc -shared -o exec_shell_command.so exec_shell_command.o 
The above code is a single line command.

8) Copy the shared object to PostgreSQL library path.
sudo cp -f exec_shell_command.so /usr/lib/postgresql/8.4/lib/

The above code is a single line command.

9) Add the user 'postgres' to the group 'dialout' to get permission to access USB serialport
sudo usermod  --groups dialout --append postgres

10) Create a function in Postgresql to use the 'C' function just we have created.

CREATE OR REPLACE FUNCTION exec_shell_command(text)
  RETURNS integer AS
'$libdir/exec_shell_command.so', 'exec_shell_command'
  LANGUAGE c STRICT;

Test the function by running the following command in PostgreSQL shell.
SELECT exec_shell_command('send_sms.sh 9894835581 "Hello Rampro, How are you?"');

OR Any other shell command.

That's it.  This function can be called from any procedure to execute any arbitrary shell command.

Monday, November 21, 2011

User Friendly vs User Reliable

On various occasions, I heard some people saying 'Linux is not user friendly'.

I asked them which Linux distribution they used.

Most of them said that they have not at all used Linux. Then how could they come to the conclusion that 'Linux is not user friendly'?

Only because of gossips by their peers.

I took some of them to see the Desktop of latest UBUNTU (10.4 to 11.10) versions. There were impressed on the desktop features and application collection.

I took one of them to one of my customer place, who runs Mandriva Linux version 10, since 2004. For the past seven years he was using it without any interruption, without any virus, without any reformatting of hard disk partitions. From the words of my customer, the gossiper came to know , how reliable Linux is!

I told him Linux is not only 'USER FRIENDLY' but also 'USER RELIABLE'.

Just a beginning

On 1st October 2004,  we started developing and deploying Linux based desktop solutions to various business domains. Initially we wanted to provide a solution to local grocery stores.

In Super Markets and Malls, the consumers are allowed to pick the commodity according to their choice and finally the bill is prepared. Bar code system is helping here much for speedy billing.

But in traditional grocery stores, first the bill is prepared and then the corresponding commodities are picked and delivered by the staff of the shop.

Normally more than 2000 commodities are available in a medium to higher end  grocery store. If a busy grocery shop wants to use computers for billing, selection of the required item among the available items is a key factor in deciding the bill preparation speed.

Also the staff of the grocery shop are not well educated to read English. So, the computers in grocery store shall use local language. The printer shall be able to print in local language at high speed.

Some of the consumers may not understand local language, so there need to be English printing also.

Keeping the above points in mind, we had created a completely Bi-Lingual application on Linux having interfaces and printing facility in both Tamil & English.

We created a mapping for the Unicode Tamil font in Linux to ISCII Tamil fonts for TVSE 450/455 Champion Dot Matrix Printers. This facilitated our application to print in Tamil at the speed of printing English.

Also we have provided multiple selection methods for easily selecting the items required by the customer.

We installed the software in one of a reputable customer in Dharmapuri, Tamil Nadu. We followed up the requirements for the next six months and incorporated the changes needed. Then most of the reputed stores in and around Dharmapuri installed our application and reaped its benefits.

And we had created an awareness about Linux and its strength in our area.