MMM: My Mad Motivation

Stop up a hole of my memory

Get the absolute path of current shell script at runtime

without comments

I needed to know the absolute path of the shell script at runtime.
$PWD was not enough because the script could be executed from other directories
Thus, I googled and I got it.

# $SCRIPT_PATH is the absolute path of the directory in which the script is located.
     SCRIPT_FILE_NAME=$(readlink -f "$0")
     SCRIPT_PATH=`dirname "$SCRIPT_FILE_NAME"`

How simple it is!

Written by admin

January 18th, 2012 at 2:49 pm

Posted in Linux

Tagged with , , ,

How to replace words in multiple files automatically on linux

without comments

When you have bunch of files (may be over 100?), and want to replace words, you don’t want to handle them one by one. (If you have too much time, you can do one by one to spend time with some snacks and music.)

Here is the way automating the replace on command line.

find . -type f \( -iname "*" ! -iname ".svn" \) -exec egrep -l "$search_string" {} \; -print | sed -e ‘s/\ /\\ /’ | xargs -i perl -pi -e "s/$search_string/$replace_string/g" {}

$search_string is the words you want to change to $replace_string. If you have spaces in words, you shoud type space with backslash.

I had to replace “esp sha1 aes aes aes” to “esp null” in 255 configuration files whoes file names start with pol, so that I execute like below.

find . -type f \( -iname "pol*" \) -exec egrep -l "esp\ sha1\ aes\ aes\ aes" {} \; -print | sed -e ‘s/\ /\\ /’ | xargs -i perl -pi -e "s/esp\ sha1\ aes\ aes\ aes/esp\ null/g" {}

You’re welcome.

Written by admin

November 17th, 2011 at 10:09 am

Posted in Linux

Tagged with ,

Seven social sins – Mahatma Gandhi

without comments

I suddenly felt I should remember this.

Mahatma Gandhi said,

  • Politics without principles (신념없는 정치)
  • Wealth without work (노동없는 부)
  • Pleasure without conscience (양심없는 쾌락)
  • Knowledge without character (인격없는 지식)
  • Commerce without morality (윤리없는 비즈니스)
  • Science without humanity (인성없는 과학)
  • Worship without sacrifice (희생없는 종교)
are the “Seven social sins”.

Written by admin

October 31st, 2011 at 9:39 am

Posted in tattoo in mind

A Brilliant Commercial

without comments

How brilliant!!

Written by admin

October 28th, 2011 at 1:34 pm

Posted in muttering

When you want Floating Point instruction in Kernel module.

without comments

CPU has FPU (Floating Point Unit), and some registers and instructions are related to FPU.
These don’t have any problem when operations are performed in user space.

BUT!!! How about in kernel module?
The registers related to FPU are not saved and restored when context switching occurs in kernel mode, so that many people suggest not to use FPU in kernel mode.

Unfortunately, there is inevitable situation.
I wanted to use AES-NI (AES New Instructions) which use xmm registers (related to FPU I guess) in kernel module.
Every time I called the function implemented with AES-NI, the kernel crashed with “device_not_available” exception and “math_state_restore” exception.

I did not know what happens with xmm registers in kernel module. After I googled, the solution poped up.
Here is the solution. (Extremely Simple!!)

Wrap the statements related to FPU with kernel_fpu_begin() and kernel_fpu_end().

  	//
  	kernel_fpu_begin();
  	/* STATEMENTS (AES-NI related functions in my case); */
  	kernel_fpu_end();
  	//

How simple it is!!

Written by admin

October 20th, 2011 at 10:54 am

When is __attribute__ ((regparm(0))) needed?

without comments

I wanted to use some assembly code into both a kernel module and a user space application.
A user space application worked find, but the kernel module was always crashed. (Oooops!!)
When I inspected the register, the argument value was wired.
The argument was the pointer value of a structure. It was obvious that the kernel crash because of the wrong argument value.

The reason was this.
1. The function implemented in assembly is implemented with getting argument within stack. (__cdecl calling convention)
2. The module passed the argument into the register directly. (__fastcall)

The solution is this.
1. When the caller declare the function (implemented in assembly), __attribute__ ((regparm(0))) is attached the very first of the declaration.
For example, __attribute__ ((regparm(0))) void a_functions(void* arg);

What does __attribute__ ((regparm(0))) mean?
1. This statement decides how many arguments pass in register directly, so that __attribute__ ((regparm(0))) means I do not want to the arguments passing with registers.
If you attach __attribute__ ((regparm(3))), the arguments passed with registers up to 3 of them. 4th argument is passed with the stack.

Until here, this is all I understood.

Written by admin

October 20th, 2011 at 10:14 am

When you want to delete files finded by “find” command

without comments

‘find’ command is very handy when I find files that I know only the file name (or partial of it).
‘rm’ command is to remove files as you know.

Let’s assume this situation. I want to remove every files recursively whose name are "FILE.NAME".
I wouldn’t be annoyed if there were under 10 files. I could type "rm" 10 times to remove files one by one.
How about more than 10, probably 50?

OK. Here is the answer. (I assume that I want to remove only files not directories.)

 # rm -f $(find ./ -name FILE.NAME) 

Written by admin

September 29th, 2011 at 12:45 pm

Posted in headache inductor,Linux

Tagged with , ,

Automatic typing passphrase for apache2 with SSL support

without comments

When I started to support SSL on my apache2 server, it didn’t start automatically after booting.
I checked the apache2 process with "ps -ef | grep apache2", the starting shell was alive.
Then, I realized that the apache2 server needs PassPhrase for SSL when it starts.
So, automatic typing the passphrase is needed for starting apache2 server right after the booting.

Here is the way.
First, create a shell script executing "echo your_passphrase". The script file has to have execution permission.

The example of the shell script is here.

#! /bin/bash
echo your_passphrase

Second, edit the apache config file. The config file is /etc/apache2/apache2.conf in my system.
You must edit this file with sudo.
Simply add this line at the last of the configuration file.

SSLPassPhraseDialog exec:/path/to/passphrase-file

Now, you may want to restart your apache2 server, and check it starts well.

Written by admin

September 16th, 2011 at 4:49 pm

getopt() function

without comments

Just the simple example of the getopt() function in C language.
unistd.h has to be included.
getopt_long() function can help for the options which use double dashes like “–help”.

#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>

/* possible options */
const char* options = "hvf:";
const char* help_string = "Providing example of getopt() function\n"
			"getopt_example [OPTIONS][FILE]\n"
			"-h\t\tHelp\n"
			"-v\t\tVersion\n"
			"-f [File Path]\tCheck if the \"File Path\" can be accessed.\n";
const char* version_string = "version 1.0\n";

void PrintHelp();
void PrintVersion();
void CheckFile(const char* file_name);

int main(int argc, char** argv)
{
	int opt = 0;
	bool have_option = false;

	while(-1 != (opt= getopt(argc, argv, options)))
	{
		/*printf("option : %c\n", opt);*/
		switch(opt)
		{
			case 'h':
				have_option = true;
				PrintHelp();
				break;
			case 'v':
				have_option = true;
				PrintVersion();
				break;
			case 'f':
				have_option = true;
				CheckFile(optarg);
				break;
			default:
				have_option = false;
				break;
		}
	}

	if (false == have_option)
	{
		PrintHelp();
	}

	return 0;
}

void PrintHelp()
{
	printf("%s", help_string);
}

void PrintVersion()
{
	printf("%s", version_string);
}

void CheckFile(const char* file_name)
{
	if (access(file_name, R_OK) == 0)
	{
		printf("%s can be accessed.\n", file_name);
	}
	else
	{
		printf("%s cannot be accessed.\n", file_name);
	}
}

Written by admin

September 12th, 2011 at 5:36 pm

Posted in C,headache inductor

Tagged with ,

2011.06 Singing at a wedding

without comments

The title of the song is “남과 여” meaning “A man and a woman”.
I sang at the wedding of my friend with Tmine. :)

Written by joong

June 14th, 2011 at 10:43 am

Posted in muttering