Code Tips and Snippets

Not yet member?
Click here to register.
Only members can post comments
VirtualBox bridged mode: automatically nic select and VM start
Posted by pk on Sunday, October 09 2016 - 10:47:11
2 stars levelC LanguageLinux operating systemWindows operating system

If like me, you use a VirtualBox VM for developing server applications, you certainly have configured your VM with your network interface in bridged mode.

When you have to move your VM on a Laptop PC, and after that, each time you switch from a wired connection to a Wireless one, you definitely have been annoyed, having to update the network adapter on which the network is bridged.

You will see in this post how to develop a mini C program that allows you to switch automatically to the correct network interface and then start the VM.

Optimizing your C/C++ code by using register!
Posted by pk on Saturday, November 07 2015 - 15:48:25
2 stars levelC Language

Monday april 06, 2020 Update: Obsolete article
Last tests had been done on ubuntu 12.04...
gcc Optimizer doesn't need anymore register
What has been true during 30 years is now no more true!

During the seventies, at the very beginning of the C language, the register keyword has been introduced in order to allow the programmer to give hints to the compiler, telling it that the variable would be used very often, and that it should be wise to keep it’s value in one of the processor’s internal register.

Nowadays, optimizers are much more efficient than programmers to determine  variables  that are more likely to be kept into registers, and the optimizer does not always take the programmer’s hint into account.

So many people wrongly recommend not to use the register keyword.

Let’s see why!

Allow only a single instance of a php script to run at the same time
Posted by pk on Sunday, September 21 2014 - 16:37:43
4 stars levelphp languageLinux operating system

What a good idea to administer a Linux server by writing php scripts instead of shell scripts ...

We have already seen how to run php scripts with suid root permission

Frequently, we need to run a script that must not be run several times at the same time...we say that we want to run only a single instance of the script at the same time.

We will use php classes to achieve that... especially the fact that when a script ends, the destructor of instancied objects is automatically called.

Fast database stored tree traversal
Posted by pk on Sunday, September 14 2014 - 15:23:22
5 stars levelphp languageMySQL

The simplest way to store a tree structure into a database is obviously the table structure in which each record represents a node, and where a column is used to specify the key of the parent node.

This is by far the most convenient way for tree operation handling (insert, delete a node, etc.).
The tree traversal is very easy and is done with a simple recursive micro-function.

The downside to this advantage (and yes, it is well known: each advantage  generates 1 or n disadvantages.)
lies in the fact that the recursive traversal requires the processing of an SQL query on each function call.

Fortunately, there is an enhanced database structure that allows fast tree traversal:
A single SQL query does the job!

Run php (or other) scripts with suid root permission
Posted by pk on Sunday, September 07 2014 - 12:45:15
5 stars levelC Languagephp languageLinux operating system

Who never had the need to run some php (or other) scripts, which require access to data with root privilege, in the context of unprivileged users ( apache for an example ).

In fact, it is very easy to achieve this with a C program.

But in recent Linux distributions, for a script (other than bash/sh/...), it is now forbidden for so-called security considerations.

Maybe this would be justified on a server where multiple users have local access, with permissions that are uncontrolled by the administrator…

On a personal server, where only services (web, email, etc.) are offered to the public, I think that it is completely unfounded!

Let's see how to bypass totalitarian restrictions of current distributions.

yakpro rulez!

Site has been updated on Wednesday, January 19 2022 - 09:43:57