Archive for category Linux

Ohio Linux Fest

I will be at Ohio Linux Fest all day Saturday, hope to see a lot of Ubuntu people there!

I am looking forward to “Python for Linux System Administration”, “Linux Boot Process”, “Introduction to GNOME 3.0″, “The Ubuntu Kernel”, “Understanding Debian” and “Building a Community Around Your Project”.

Should be a good time, official schedule is: http://www.ohiolinux.org/schedule.html

Tags: , ,

SLES / SLED 10 with Intel 965 Graphics

If you need to update your installation to work with Intel graphics, there is an RPM you can install that will likely resolve your issues:

http://download.opensuse.org/repositories/home:/mhopf:/hp/SLE_10/i586/intel-i810-xorg-x11-6.9.0.2-8.1.i586.rpm

Install that, then run Sax or edit xorg.conf, and make sure to pick the i810 driver. (This post is in response to the number of forum posts on this subject offering no solutions)

Tags: , , , ,

Creating a virtual machine using ubuntu-kvm-builder

I was playing with ubuntu-kvm-builder, and used apt-cacher for the mirror, and the build time is right around 2:30 which is pretty impressive. Using recordmydesktop I made a quick video of it, wanted to test the embedded video:

If you can’t see the video directly above this, you are not running a web browser that supports embedded videos (or you are seeing this on a planet).

Encrypted Swap

This post was spawned from my own misconception that my swap partition contained no sensitive data on systems with a lot of ram.

All of my systems I work with have atleast 4GB of ram, so my swap usage is usually under 2 megabytes. Why should I worry what’s in my swap partition?

Instead of going into it, just try it yourself. My swap partition is /dev/sda5. Run the command:

$ sudo strings /dev/sda5 | more

What came up was a ton of interesting data, from files I had looked at, print jobs, and bash scripts. So yes, even if you have enough ram, your swap is still very vulnerable to storing a lot of data about you.

Good news is Ubuntu 9.10 / Karmic will have the option to encrypt swap, which is on the wiki.

Tags: , ,

Manpages

Its almost the end of 2009, and I read on Planet Debian today a entry on writing man pages.

So man pages… where do we start? We have HTML / CSS / XML / RST which are all generally globally accepted formats of communication. Unfortunately, man pages do not use those.

And quite frankly, I am a fairly busy person, and have quite enough things to accomplish in a day. Why would I learn yet another formatting language? What justifies these files being in their own little world, when the whole world as a courtesy is supposed to provide them with their packages?

There really needs to be a movement to kill man pages in their current form, and bring them up to speed. A dialect for the sake of having a dialect really isn’t worth it when we have abundant alternatives.

Xorg / X11 programming update

So I have been looking into Xorg development. I posted about it a few months ago, but little to no help came of asking where to start looking. After asking several places without any useful help, I actually found a great book:

However, in my haste to order that one, I ordered 2 of these:

So if you are looking to get started with X11 / Xlib / Xorg these are handy to have around. Since I have 2 copies of the pink reference manual, if you want one, email me at sharms at ubuntu period com and I will send you my extra copy for free. Or if you want I can bring it to Ohio Linux Fest 2009 and give it to you there.

Update: Book claimed already

Tags: , , , ,

Programming in C: Converting an Integer to Binary (int to bin)

Engineyard Competition
Recently I saw on some news site a competition to calculate SHA1 collisions. Naturally, I tried it out, but used Python, my preferred language. Unfortunately, my Python implementation was significantly slower than others using C (which was smoot since both C and Python implementations were completely dwarfed by CUDA implementations). I entered the competition after it was already in progress, and managed to get a pretty good score, but that was with an insane amount of computers crunching it.

So that leads me back to the topic of this post. Convinced that I had more hardware than anyone else in the competition, and that even though I started with less than 12 hours to beat everyone, I was pretty sure I should have won (before I saw the CUDA performance). So I started rewriting my hastily written Python program in C. The competition was already over, so this was purely for self improvement.

A Little Rusty
My first programming language was C, and I was an expert in it when I was 13 or so (probably 1992). Since then, my attention was driven towards Python and PHP, as the defacto languages of the web. This made me realize exactly how much I had forgotten about plain old C (my C++ was not horrible, atleast I could still understand C++ code written by decent programmers). Anyhow, part of the competition included taking numbers and comparing how much each of the 160 bits varied (ie 1011 and 1001 differ by 1 bit, 1100 and 1111 differ by 2 bits etc). So I wanted to make a C function that converts any arbitrary integer to a binary string (which really wasn’t the goal of the exercise, as the hashes were in hex, but it made me try and remember how to convert an integer as it had been so long).

Conclusion to a Very Boring Story
So I ended up writing an int2binary function and a program that converts them and prints them out. It will break with very large number, but I included a function to estimate it’s size (always going to the next number divisible by 4 as I like that padding), so linking that to a malloc would be trivial. I don’t suggest this is run anywhere as it hasn’t been really tested or code reviewed, but I like my answer better than most of the answers I found on the net, so I will share it with you, and hope to get a higher google score than the ones I don’t like:

int2bin.c

#include <stdio.h>

void int2binary(unsigned int n, char *buffer, unsigned int buffer_size);
unsigned int buffer_sizer(unsigned int n);

int main(int argc, char **argv)
{
        char buffer[2048];
        unsigned int binaryNumbers[9] = { 1, 4, 6, 8, 10, 55, 123, 2000, 2048 };
        unsigned int i = 0;

        for(i = 0; i < 9; i++)
        {
                int2binary(binaryNumbers[i], buffer, (buffer_sizer(binaryNumbers[i]) + 1));
                printf("%4u: %s\n", binaryNumbers[i], buffer);
        }

        return 0;
}

void int2binary(unsigned int n, char *buffer, unsigned int buffer_size)
{
        unsigned int i = (buffer_size - 1);
        buffer[i] = '\0';

        while(i > 0)
        {
                if(n & 0x01)
                {
                        buffer[--i] = '1';
                } else
                {
                        buffer[--i] = '0';
                }

                n >>= 1;
        }
}

unsigned int buffer_sizer(unsigned int n)
{
        unsigned int x = 1;
        unsigned int i = 0;
        unsigned int lastFound = 0;

        // Takes care of n = 0, and we dont need to loop for 1st case
        if(n < 16)
        {
                return 4;
        }

        while(1)
        {
                i++;
                if(n / x)
                {
                        lastFound = i;
                } else
                {
                        break;
                }

                x <<= 1;
        }

        // Not a multiple of 4, how much do we need to add to it
        if(lastFound % 4)
        {
                lastFound += (4 - (lastFound % 4));
        }

        return lastFound;
}

As you can see I did a pretty poor job commenting it and naming variables, basically though in int2binary we need to start from the right hand side and work our way left, so we start variable i at the end of the string and work our way back. We use & to compare variable n and 0×01 (which means that if the rightmost position has a 1, we add a 1 to the buffer, else add a 0 to the buffer).

For buffer sizer, this just tried to take a guess at how long that string should be. If the integer is less than 16 it will always be 4, so no need to do anything else (I was in recursive mode for a different algorithm so I was cutting things off at beginning mode). Otherwise we loop through, and try to divide variable n by variable x. Each time it is ran, variable x is shifted to the left, which has the effect of squaring the number. This is also super fast on the cpu.

For people not in C development mode, didn’t make a make file, but to compile it:

gcc int2bin.c

Output should be:

[sharms@sparrow ~]$ ./a.out
   1: 0001
   4: 0100
   6: 0110
   8: 1000
  10: 1010
  55: 00110111
 123: 01111011
2000: 011111010000
2048: 100000000000

MMap to null

I was reading an lwn article about an exploit: http://lwn.net/Articles/341773/

Being that I am writing posts this week about programming, and about my Fedora run down, thought people might find this interesting.

I wrote a little test code that fails on Ubuntu but works on Fedora 11 (based off lwn post):

#include <stdio.h>
#include <sys/mman.h>

int main(int argc, char **argv)
{
    // Try to write to memory location 0
    void *mem;
    mem = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);

    if(mem != NULL)
    {
        printf("Could not write to memory position 0\n");
    } else
    {
        printf("We can write to memory location 0\n");
    }

    sprintf((char *) mem, " This is a test\n");
    printf("Memory contents: %s\n", (char *)(mem + (sizeof(char))));
    return 0;
}

Fedora 11 results:

./a.out
We can write to memory location 0
Memory contents: This is a test

Ubuntu 9.04 results:

./a.out
Could not write to memory position 0
Segmentation fault

What does this mean?
As far as I can understand it, userspace programs segfault when trying to access data in the NULL (or 0) memory region. The kernel does not have this limitation. The author of the exploit said this is because GCC optimises out the null check. So if there is kernel code which references a pointer to 0, then you can have it run whatever you want. And in atleast 2.6.30, there is kernel code that does that.

Ubuntu does not let the userspace programs write to 0, but in F11 you can. Interesting stuff.

Tags: , ,

Annoying people with code: A gentle introduction to C# and Mono Part 2 – Data Types

In part one of this series we covered making a basic “Hello World” plugin in Mono / C#. Today we are going to dive into different types of data, and how to define them. Show me part 1! Also if you thought part 1 was ugly, I added a new javascript syntax highlighter so I recommend you read it on my blog instead of planet.ubuntu.com.

Variables
A variable is something that stores a value. In different programming languages, these are handled differently. C# is called a static / strongly typed language as you have to tell it what variables are. To explain, I will use python in comparison:

Python Example

x = "This is a string"
y = 5

As you can see, we didn’t have to tell Python anything about what type of variables they are. In C# / Mono, we would do this like this:

C# Example

string x = "This is a string";
int y = 5;

I will tell you, after my years of development, that I program a ton of stuff in PHP / Python today. One of the worst and best parts of these languages is that they are dynamically typed. The fact that C# requires variables to be declared with a type before using makes life easier for testing and reading. There is a stack overflow post on what static typing gives you: Read the stack overflow post.

Variable Naming
Before we continue, you will notice in the top example I used the variable names x and y (to illustrate that the variable names really mean nothing to the compiler / interpreter. If I named them myString you might have been confused). If you do this in real life, I will find you and give you a thorough verbal lashing. I generally use camel case to name variables. Just do not abbreviate them as it really serves no purpose, and makes a lot of pain for people besides yourself maintaining code.

You can read about camel case at wikipedia.

The Source
For this program, I copied our HelloWorld.cs, and just modified it. I also have a modified makefile.

dataTypes.cs

using System;

// We create a class to contain it
class dataTypes
{
    static void Main(string[] args)
    {
        // The type 'bool' is always true or false
        bool isMonoTheOnlyPatentRisk = false;

        // To check if it is true or false in program code,
        // we can use an 'if statement'
        if(isMonoTheOnlyPatentRisk)
        {
            Console.WriteLine("isMonoTheOnlyPatentRisk was set to true");
        } else
        {
            Console.WriteLine("isMonoTheOnlyPatentRisk was set to false");
        }

        // Note how I named the variables -
        // lowercaseFirstThenCapitalizeEachWorld
        // this is called camel case -- I first read about it
        // in a Charles Petzold book, but I am sure it
        // probably didn't originate there.  Either way its
        // how I code, I recommend you name variables
        // as descriptive as possible. 

        // Note to only kids:  You will have the initial
        // reaction of saying: Why should I name stuff long?
        // That takes time and is stupid!
        // I used to be you 10 years ago, the problem is if
        // you need to use your code in several years of not
        // using it.  Your time will be completely wasted
        // figuring out what the heck is going on, despite
        // seeming so intuitive / easy to understand when you
        // did it

        // Integers store numbers
        int ourNumber = 5;

        // In most langauges we can evaluate these in
        // 'if statements' like bools above, but if we try in
        // C# this will fail.  In C# a bool can only be 0 for
        // false, 1 for true, or we can use the
        // Convert.ToBoolean function like this:
        if(Convert.ToBoolean(ourNumber))
        {
            Console.WriteLine("ourNumber was not 0!");
        } else
        {
            Console.WriteLine("ourNumber was 0");
        }

        // Lets see if we can compare this number
        if(ourNumber > 4)
        {
            Console.WriteLine("ourNumber was greater than 4");
        } else
        {
            Console.WriteLine("ourNumber was less than or equal to 4");
        }   

        // Make a string, and output it
        string helloWorldVariable = "Hello World!";
        Console.WriteLine(helloWorldVariable);
    }
}

Makefile

COMPILER=gmcs

all: helloWorld.exe dataTypes.exe

helloWorld.exe: helloWorld.cs
	$(COMPILER) helloWorld.cs

dataTypes.exe: dataTypes.cs
	$(COMPILER) dataTypes.cs
clean:
	rm -f helloWorld.exe dataTypes.exe

.PHONY: all clean

Output from our terminal

[sharms@sparrow mono]$ make
gmcs helloWorld.cs
gmcs dataTypes.cs
[sharms@sparrow mono]$ mono dataTypes.exe
isMonoTheOnlyPatentRisk was set to false
ourNumber was not 0!
ourNumber was greater than 4
Hello World!

Bonus Tip
Did you notice in the first part the way I named the bool? If you are programming, generally, your bool’s should start with ‘is’. This becomes very important once you start making complex ‘if statements’. Trust me on this one, it’s a habit you want.

Tags: , , ,

Best Linux Notetaking Application

keepnote-logo

I know a lot of people out there like to take notes with Linux, and probably didn’t come across this program yet, as it look me a bit googling to find it again. The program is called KeepNote, and is a fantastic program for taking notes. I use it with Dropbox, and store all of my notes there and that way it is synchronized to all of my systems. The program itself is open source and free, and you can support it by making a donation on the homepage.

keepnote1-small

I really like KeepNote, and the ease at which I can drag and drop media to it. One thing I did set in preferences was to only autosave every few minutes, otherwise Dropbox is constantly being updated:

keepnote2

The top toolbar is well thought out and very intuitive for formatting. It also supports all of the standard keyboard shortcuts, such as CTRL-B for bold etc. If you are looking to try it out, the author has a .deb package on the homepage that installs no problem (Ubuntu Jaunty AMD64 here).

Tags: ,