A Database Query Problem

A database query problem for those who are interested:

There is a table with two columns: userId, time (many to many relationship)
For every pair of userIds, if any two time difference of two users is less than 20 minutes, then the pair will get a point.
Now need to find out the pair with highest points.

For example:

userId time
a 20
a 120
a 230
b 30
b 110
b 260
c 30
c 120
c 245

Then points of pair:
ab = 2
ac = 3
bc = 3

PS: This query may be used for data mining and/or artificial intelligence.

PS: It can also be used for trolling 3:)

PS: Idea courtesy – Nipa Afroz

How to open a process as daemon on Linux

Sometime we may need to open a process as daemon (the process should not die when the child process die). One of the easiest way to do that is to run the process from command line and add a & at the example. For example if you want Firefox to run from shell and don’t want to wait for Firefox to be closed, run “firefox &”.
But the problem with the approach is if you close the shell terminal, it will also close the process. You need to execute “exit” command to exit from shell properly. But if you use execute the process with “nohup” the problem is solved. For example, if you execute “nohup firefox &”, firefox will not close even if you close the shell.

It is very helpful if you want a process to run as a simple daemon. This is not the right way to make a process daemon, but this is a good workaround to get close to a daemon. So if you want a python script to run as daemon just run
nohup python /path/to/script.py &
and your process will run in background :-)

How to Search Faster In Firefox With I’m Feeling Lucky

Firefox address bar can act as a search box. You can type here and press enter to search.
Sometimes some plugin/add-on change this default search from Google to Ask or something else.
Also you want to add “I’m feeling lucky” feature to this search. The benefit is when you will search some site and Google is confident enough that it has found the site you are looking for, it will directly take you to the site.
To fast search in Firefox you can do it manually by going to about:config and adding/changing the key “keyword.url” to this value “http://www.google.com/search?btnI=I’m+Feeling+Lucky&q=”.
Or you can install this add-on : https://addons.mozilla.org/en-US/firefox/addon/fastawesomebarsearch/ (no restart required)

Hope it will save some time :-)

-Enzam

How to add a system call in linux kernel (Ubuntu OS)

This is just a brief description, so read at your own risk.

I have tested & used it. So it should be working. Please install a fresh install of Ubuntu.

This tutorial is for both 32 and 64 bit x86 processors and operating system. I have assumed that you are working in Ubuntu 10.10 and using kernel version 2.6.37.3 . If you are using any other kernel version just replace 2.6.37.3 with your version. I am also assuming you have extracted the source code.

Now let the new system call’s name be “add2″.

1. Now you will find a “arch” folder in the source code folder. Open the file arch/x86/kernel/syscall_table_32.S in a text editor. Go to the end of the document and add this line -

	.long sys_add2		/* my code */

2. Now open arch/x86/include/asm/unistd_32.h and find out
#define __NR_prlimit64 340

Add a new line after this:

#define __NR_add2		341

Don’t just close yet. After 3-4 lines, you will find a line like
#define NR_syscalls 341
Change it to

#define NR_syscalls		342

4. Now edit arch/x86/include/asm/unistd_64.h
Find out:
#define __NR_prlimit64 302
__SYSCALL(__NR_prlimit64, sys_prlimit64)

Now after these two lines, add these two lines

#define __NR_add2				303
__SYSCALL(__NR_add2, sys_add2)

5. Now again in the source folder you will find a folder named include. Open the file include/linux/syscalls.h and go to the end of the file. Before the line
#endif
write this prototype definition line:

asmlinkage long sys_add2(int i,int j);

6. Now find out the kernel folder in the source directory. Create a new empty file in the kernel folder with the name “mysysteamcalls.c” . Add the following codes in the file:

#include<linux/linkage.h>
asmlinkage long sys_add2(int i,int j)
{
    return i+j;
}

7. Now open the Makefile in this folder(/kernel/Makefile) and find out
obj-y += groups.o
Add a new line before this line :

obj-y += mysysteamcalls.o

Ok, this is the edit you need to do to add a new system call. Now compile or recompile the source code and enjoy your new system call.

Here is a sample code to call the system call :

#include <stdio.h>
#include <linux/unistd.h>
#include <sys/syscall.h>

//comment the following line if you are using 64 bit, this number is the same used previously
#define sys_add2 341

//comment the following line if you are using 32 bit, this number is the same used previously
#define sys_add2 303

int main(void)
{
    int a,b,c;
    printf("Adding Two Numbers in Kernel Space\n");
    printf("Input a: ");
    scanf("%d",&a);
    printf("Input b: ");
    scanf("%d", &b);
    c = syscall(sys_add2, a, b);
    printf("System call returned %d\n", c);
    return 0;
}

Important note: To add a new system call, you don’t need to create a new file, you can just add a new function in the same “mysysteamcalls.c” file. And if you don’t create a new file you don’t have to do the step 7.

-Enzam

How to compile & recompile linux kernel in Ubuntu (generic way)

This is just a brief description, so read at your own risk.

I have tested & used it to compile & recompile (11th time till now) linux kernel. So it should be working. Please install a fresh install of Ubuntu.

I have assumed that you are working in Ubuntu 10.10 and using kernel version 2.6.37.3 . If you are using any other kernel version just replace 2.6.37.3 with your version.

You need some applications to compile the linux source code. You can install the required applications by the following code:

sudo apt-get update
sudo apt-get install build-essential initramfs-tools

If you have downloaded a compressed source like .tar.bz2, extract it first. You can extract it anywhere, you don’t need to put the source code in any specific folder.

To compile linux kernel for the first time, open a terminal & go to the source directory in the terminal using “cd”. The source directory should contain some folders like “arch”, “block”, “crypto”, etc. Now type the following commands:

mkdir ../linux-build
yes ''|make --jobs=`getconf _NPROCESSORS_ONLN` O=../linux-build config
make --jobs=`getconf _NPROCESSORS_ONLN` O=../linux-build
sudo make --jobs=`getconf _NPROCESSORS_ONLN` O=../linux-build modules_install install
cd /boot
sudo mkinitramfs -o initrd.img-2.6.37.3 2.6.37.3
sudo update-grub

This will take a lot of time. This steps are required for the first time only.

Recompilation:

If you want to recompile & install it, you need to run the following commands in terminal from the source directory:

sudo rm /boot/*2.6.37.3*
make --jobs=`getconf _NPROCESSORS_ONLN` O=../linux-build
sudo make --jobs=`getconf _NPROCESSORS_ONLN` O=../linux-build modules_install install
cd /boot
sudo mkinitramfs -o initrd.img-2.6.37.3 2.6.37.3
sudo update-grub

This recompilation part should take a lot less time than the previous. After each compilation and recompilation, you must restart Ubuntu to see the changes.

If you are editing the same system call function again and again, then you can do the following (it will reduce the compile time a lot, changes in 3rd line only, no modules_install step):

sudo rm /boot/*2.6.37.3*
make --jobs=`getconf _NPROCESSORS_ONLN` O=../linux-build
sudo make --jobs=`getconf _NPROCESSORS_ONLN` O=../linux-build install
cd /boot
sudo mkinitramfs -o initrd.img-2.6.37.3 2.6.37.3
sudo update-grub

It will reduce the compile time to 5 min or less.

-Enzam

P.S. : To add a system call, follow here.

Sample GP SMS API Use With PHP

Below there is a sample code of using GP SMS Api with PHP.

<?php
function send_sms($mobile_no, $text) {
//creating a soap client
$soap=new SoapClient(“SMS WSDL.wsdl”);
//setting the variables of SendSMSRequest type
$req_vars=array(
“registrationID”    =>  “YOUR USER ID”,
“password”          =>  “YOUR PASSWORD”,
“sourceMsisdn”      =>  “88017********”,
“destinationMsisdn” =>  “88″.$mobile_no,
“smsPort”           =>  7424,
“msgType”           =>  4,
“charge”            =>  0.00,//If you want you can give some value, but if you are charged, I’m not responsible for that :P
“chargedParty”      =>  “88017********”,
“contentArea”       =>  “gpgp_psms”,
“msgContent”        =>  $text,
);
//The request variable
$request=array(“SendSMSRequest”=>$req_vars);
//requesting now to send sms
$response=$soap->sendSMS($request);
return $response;
}

?>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>GP SMS Api Testing</title>
</head>
<body>
<pre>
<?php
//calling the function
$phone_no=”017********”;
$text=”Hi people! Just testing out! :-) “;
$response=send_sms($phone_no, $text);
print_r($response);
?>
</pre>
</body>
</html>

P.S. : If you get any SSL related error, go to your php.ini file,find out

“;extension=php_openssl.dll”

then delete the ; from the beginning

Update : A new updated post (with updated code) in this link: http://blog.muktosource.com/enzam/send-sms-example-with-gp-aloashbei-api-php.html

How To Make GP API Calls From Java Using Netbeans

How To Make GP API Calls From JAVA (Netbeans)

IDE : Netbeans 6.8(Will work on 6.7+ i hope)

1. First Create A New Java Desktop Application(File -> New Project -> Java -> Java Desktop Application). click next, give it a suitable name (eg Test), click finish.

Adding The WSDL:

1. Goto https://www.aloashbei.com.bd/API/GP.ADP.BizTalk.LBS.Orchestration/WebService_GP_ADP_BizTalk_LBS_Orchestration.asmx?wsdl. Save the page locally.

2. Then edit the 86th & 89th line. The new lines will be -

Line 86 :

<soap:address location=”https://www.aloashbei.com.bd/API/GP.ADP.BizTalk.LBS.Orchestration/WebService_GP_ADP_BizTalk_LBS_Orchestration.asmx”/>

Line 89 :

<soap12:address location=”https://www.aloashbei.com.bd/API/GP.ADP.BizTalk.LBS.Orchestration/WebService_GP_ADP_BizTalk_LBS_Orchestration.asmx”/>

3. Now go to Netbeans. In the Projects explorer(If you can’t find it, press Ctrl+1 or Window->Projects), select the new Project, right click it. Go to New->Other. Select Web Services from categories, select Web Service Client. Press Next. Select Local File: and set the locally saved file as the location. Click Finish

You are done adding the WSDL :-) .

2. Design view will come up. Take a label from the Pallete(if you can’t find pallete you can press ctrl+shift+8 or go to Window->Pallete) and put it in the form. Then take a textfield and put it beside it. Now the view should be something like this

3. Change the jLabel1 text to “Please Enter The Mobile No: 017″ and jTextField1′s Text to “xxxxxxxx”.

4. Select jTextField1, press right button, goto Events->Action->actionPerformed.

5. Write this code in the actionPerformed function:

WebServiceGPADPBizTalkLBSOrchestration service = new WebServiceGPADPBizTalkLBSOrchestration();

WebServiceGPADPBizTalkLBSOrchestrationSoap port = service.getWebServiceGPADPBizTalkLBSOrchestrationSoap();

LBSRequest req = new LBSRequest();

req.setRegistrationID(“YOUR ID”);

req.setPassword(“YOUR PASS”);

req.setMsisdn(“88017″+jTextField1.getText());

LBSResponse res = port.requestLocation(req);

String response =

“Status = ” + res.getStatus()+”\n”+

“Latitude = ” + res.getLatitude()+”\n”+

“Longitude = ” + res.getLongitude()+”\n”+

“Time = ” + res.getTimestamp()+”\n”;

System.out.print(response);

JOptionPane.showMessageDialog(null, response);

6. Import these classes:

import
com.grameenphone.playground.WebServiceGPADPBizTalkLBSOrchestration;

import
com.grameenphone.playground.WebServiceGPADPBizTalkLBSOrchestrationSoap;

import
lbsresponse.schemas.lbs.biztalk.adp.gp.LBSResponse;

import
lbsrequest.schemas.lbs.biztalk.adp.gp.LBSRequest;

import javax.swing.JOptionPane;

7. Now execute with F6. This should show a prompt for mobile no, write it down, and press Enter.

8. Hopefully after some time you will get the result printed on console and in a dialog.

Happy coding :-)