Archive

Archive for the ‘C/C++’ Category

GCC tricks for the Linux Kernel

November 3, 2009 Leave a comment

While debugging some GCC specific linking problems, came across this very handy resource for gcc tips and tricks… very helpful indeed!

Check it out at –> http://www.ibm.com/developerworks/linux/library/l-gcc-hacks/index.html


Shoaib Mir
shoaibmir[@]gmail.com

Advertisements
Categories: C/C++ Tags: , ,

Building Boost under Solaris

August 12, 2009 2 comments

Boost provides free peer-reviewed portable C++ source libraries. I have been using another library “libiqxmlrpc” in my application and that used Boost, now when porting my application to Solaris I had to build Boost from source which I thought would be very simple but it wasnt…

Here is what I had to do:

– Once you have got the latest source for boost, build bjam (build environment setup scripts) from within the Boost source code folder…that is very straight-forward
– After bjam is successfully build, add it to your PATH to make things simpler
– No go to the Boost source root folder and run the following

bjam toolset=sun stdlib=sun-stlport instruction-set=i386 address-model=32 
--prefix=/opt/boost/ --libdir=/opt/boost/lib/ --includedir=/opt/boost/include/

Which is good for 32 bit Solaris and with Sun Studio installed. In case you are using 64 bit change the address-model accordingly and same goes for instruction-set that if you are on AMD use the option as “amd” instead of i386.

The build process will not work just with the default option so these options have to be added. Once the build is started it sure does take quite a while so enjoy a coffee during that time 🙂


Shoaib Mir
shoaibmir[@]gmail.com

Categories: C/C++ Tags: , ,

Removing trailing Slashes (‘/’)

March 30, 2009 Leave a comment

Just adding it up to the blog so that it might help with someone looking for the same….

I had to remove the trailing slashes from the directory paths as that was a requirement of a third party program we were using. Ended up doing it like this:

Where “conf->pg_backup” is the original string
and “newstback” is the changed string after removing trailing slashes…

for(i=0, j=0; conf->pg_backup[i]; ++i)
{
newstback[j++] = conf->pg_backup[i];
}

lenback = strlen(newstback) – 1;

for(;newstback[lenback] == ‘/’ || newstback[lenback] == ‘\t’; lenback–)
;
newstback[lenback +1] = ”;


Shoaib Mir
shoaibmir[@]gmail.com

Categories: C/C++ Tags: ,

string functions and buffer overflow

March 3, 2009 Leave a comment

strcpy(), strcat() and strcmp() are some really useful functions from standard C library and I have seen people using them quite a lot but something that needs to be taken care of is that at time they can end up in a buffer overflow condition.

They are dangerous because if the string to be copied or for concatenation is too long to fit in the destination buffer, it will overwrite adjacent memory, invoking undefined behavior. This can at times even result in a segmentation fault if the above mentioned scenario does happen, and a good hacker can actually use this buffer overflow to break into a system.

A good solution to that is using length checking version of string functions from standard C library like strncat(), strncpy() and strncmp().

Shoaib Mir
shoaibmir[@]gmail.com

Categories: C/C++ Tags: , , , ,

Gkeyfile and configuration files

February 20, 2009 2 comments

While looking for a configuration file parser I came across GkeyFile that is part of glib. It was very easy to use and met almost all the requirements that I wanted out of it. Here is a simple howto for GkeyFile…

Key files in GLib are more like .ini files that are mostly available on Windows. The key files data is divided into groups, where the name appears between square bracket characters, and comments are started by the # character. Key files, parsed by GKeyFile can handle strings, localized strings(internationalization support), Boolean values, integers, doubles, and arrays of each of these data types.

A simple example of a KeyFile can be:

#First Group
[Student1]
FirstName= Joe
Age=20

Now in order to get values for FirstName and Age for the group “Student1” you can use the following code in C…

#include <glib.h>

typedef struct
{
gchar *FirstName;
int Age;

} Student;

int main ()
{
Settings *conf;
GKeyFile *keyfile;
GKeyFileFlags flags;
GError *error = NULL;
gsize length;

keyfile = g_key_file_new ();
flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;

if (!g_key_file_load_from_file (keyfile, “Students.conf”, flags, &error))
{
g_error (error->message);
return -1;
}

conf = g_slice_new (Student);

conf->FirstName = g_key_file_get_string             (keyfile, “Student1”,
“FirstName”, NULL);
conf->Age = g_key_file_get_integer (keyfile, “Student1”,
“Age”, NULL);
return 0;
}

You can find more help for Gekyfile in  Glib Reference Manual

Shoaib Mir
shoaibmir[@]gmail.com

Categories: C/C++ Tags: , ,

child process and setsid()

January 8, 2009 6 comments

I was trying to run a new process within the child process after the fork was successful and when ever I tried to run the process using execlp, as soon as the parent exited the child also did quit as well. This was getting very annoying and so I started googling the problem out and “setsid()” function was my friend in this case. With my final code looking like this:

pid_t pid;
pid = fork ();

if (pid == 0)
{
/* Child process. */
setsid();
execlp (“/bin/sh”, “sh”, “-c”, program, NULL);
exit (1);
}

-Shoaib Mir
shoaibmir[@]gmail.com

Categories: C/C++ Tags: , ,

xmlrpc over https implementation

January 8, 2009 Leave a comment

On a project we were using SSH2 module of PHP, but it was turning out to be a total mess due to compatibility issues on different operating and for the reason that it cant be built on all the operating systems easily. This made us to think about other alternatives but there are not many option in PHP where you can get much more control when using SSH.

The solution was to write our own Agent that could perform all kinds of functions we were using from the SSH library of PHP. The two functions we were using the most were:

– SSH function to run a program or service on a remote machine
– SSH function to copy a file to a remote

When writing our own agent we made a choice to go with xmlrpc as it is a pretty simple library and it will not be months of work using xmlrpc to implement our own server.

Now when looking for xmlrpc libararies for C/C++ you do not have a lot of choices available when you want to go for a HTTPS standalone server (because we can not install web server on an Agent machine). So in the end we went with “libiqxmlrpc” which is a C++ based library and very easy to use.

While implementing the server functionality the only problem that I didnt like was when ever you run a new process through libiqxmlrpc based server the process basically attaches it self to the sane port the server was running so even if the server quits, the server port is still blocked. For that we had to modify the library code in way to get the file descriptor for server socket and then close that socket using the file descriptor ID in the Server code when forking a child to run the process.

Other then this libiqxmlrpc library has been flawless for me and it integrated very nicely with PHP side as well using cURL that supports HTTPS server through its OpenSSL support.

Anyone doing xmlrpc over HTTPS, I will highly recommend libiqxmlrpc.

-Shoaib Mir
shoaibmir[@]gmail.com

Categories: C/C++, PHP Tags: ,