Posts Tagged Linux

5 Things I Have Learned About Corporations

When I was younger I would email successful people and ask how they got where they were, and how I could get there. Now I get paid to work with / on Linux, and figured I would share a few things. Age is a funny thing, and as much as I thought I knew when I was 18, 10 years later gives you a much better perspective.

  1. You can’t be a expert in every field
    My whole life I have been a “computer scientist”. That meant I ran pretty much every operating system, and tried to program in as many languages possible. However, in the professional world, your advice outside of your realm is seldomly used or even asked for. Even if it is technically correct, team x does not want to have team y telling them how to do their job. Swallow it up, not everything will go the way you want it.
  2. Communication is more important than technical expertise
    If you look through the ranks of a corporation, you will notice that seldomly does the most technicially proficient employee ever even get to the ‘C’ level (CTO, CIO etc). In smaller companies, being the expert is important because everything is riding on you. When your company has thousands of employees, communicating efficiently is worth much more. Learn how to talk to non-technical people.
  3. Your manager is always right
    Never be a martyr for a specific technical feature. If your company needs something done a specific way, and you are against it, politely state your position. If they are not interested, do it their way. If the manager was wrong, they will take heat for it. However, if you make a big issue about it, your manager is still the one at the end of the day who evaluates you on your performance review.
  4. Appearance matters
    You are selling a complete package to a corporation. It is important that your outward appearance is in line with the position you are going for. If there are two applicants of similar skill levels, the one who interviews that is well dressed and clean will get that position. First impressions count, especially during interviews.
  5. Everyone has their reasons
    No matter how bad you think a choice or decision was, everyone had a reason for doing it. They thought it out, they presented it, and weighed the factors that matter to them. It is very easy to point out how dumb you think something is, but likely you don’t have all the facts of the entire scenario. If you do have all the facts, calling them out on it won’t fix anything and will likely create resentment.

Tags: ,

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: , ,

Annoying people with code: A gentle introduction to C# and Mono Part 3: Creating a GUI (Graphical) Mono / C# Program

In this post we will cover how to make a basic GUI application using Mono along with Monodevelop. If you haven’t already, you will want to read part 1 and part 2 of this series before this article.

Monodevelop
Monodevelop is the tool we are using as our editor. This is Linux’s equivalent of Visual Studio. It can be used to code, debug and design your application, and can link in with revision control systems. This article was written using version 2.0, if your version is not 2.0 you may see slight differences.

Create your project
After starting Monodevelop, go to File -> New -> Solution. You will be presented with a prompt asking which kind of solution we are starting. Click on C# -> Gtk# 2.0 Project as shown below:

new-solution

Read the rest of this entry »

Tags: , , ,

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: , , ,

Annoying people with code: A gentle introduction to C# and Mono

This is meant to be an introduction to C# and Mono. First, we need to download the mono runtime and compiler:

sudo apt-get install monodevelop mono-devel

For the purpose of this post, we are just going to create a simple program which prints out the text “Hello World”. Go ahead and make a directory for our project so we don’t make our home directory entirely too messy. I just make a directory called ‘mono’ under /home/sharms/mono for the duration of this post.

Go ahead and create a file called ‘helloWorld.cs’, and paste the following:

// Declare which namespace we want to use.
// This allows us to use Console.WriteLine
// instead of System.Console.WriteLine
// Basically like a python 'import' statement, or a
// php 'include' statement
using System;

// We create a class to contain it
class HelloWorld
{
    // Must have a main function as this is what is
    // first called when executing this
    static void Main()
    {
         // C# requires ; after statements
         // Actually output "Hello World"
        Console.WriteLine("Hello World");
    }
}

To compile it, run:

gmcs helloWorld.cs

This will generate a file called ‘helloWorld.exe’. To run it, type:

mono helloWorld.exe
Hello World

And there you have it. I also stumbled on a Makefile tutorial today, so we can make a simple make file (I won’t describe all of this, just look at how it’s used). The tutorial is at: http://www.wlug.org.nz/MakefileHowto

[sharms@sparrow mono]$ cat Makefile
COMPILER=gmcs

all: helloWorld.exe

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

clean:
	rm -f helloWorld.exe

.PHONY: all clean
[sharms@sparrow mono]$ make
gmcs helloWorld.cs
[sharms@sparrow mono]$ make clean
rm -f helloWorld.exe

Tags: , ,

Fedora 11 vs. Ubuntu 9.04

Put Fedora 11 on my laptop just out of boredom, some notes:

  • Fedora 11 SELinux by default: Cool but confusing
  • Fedora 11 repositories: Better selection than previous releases, still not as many choices as Ubuntu
  • Ubuntu still wins on the default menu organization for new users (just a bit easier to navigate)
  • Fedora bootup vs. Ubuntu bootup is about a wash, they both look good and are fast
  • Default themes: Neither will win a competition on looks, Linux Mint is much better looking than both
  • Yum vs. Apt: Yum was fast, but a lot of 404’s on the repositories (which is more of an issue of Fedora’s mirror infrastructure)
  • PPAs vs. ???: This is where Fedora appears (correct me if I am wrong) to have absolutely no answer to OpenSUSE and Ubuntu. In Ubuntu we can get up to date packages that were not yet officially released using PPAs. OpenSUSE users can download packages from the build service. Fedora really has nothing this fun (I am aware OpenSUSE can build Fedora packages, but the selection is not even close).
  • Support: Fedora is a distro that is on the cutting edge. It will never compete in support, but this is intentional.
  • Community: Ubuntu community is simply the biggest Linux community on the internet. Nobody is even in the same ball park. This also means that Ubuntu has much more “noise” than Fedora (ie people who contribute nothing and are generally factually inaccurate). Experts may like Fedora more because of the lack of this noise.

So if you are looking to try out Fedora, I don’t think you will gain or miss much. Personally I am going to put Ubuntu back on as I really love software from PPAs, and I love using apt just out of habit. But hope that helps someone who wonders what the differences are or what they are missing.

I have added a screenshot which is the default screen with Gnome-Do with docky theme, but this obviously works in Ubuntu also:

Fedora11

Tags: , ,

Python threads

Frequently I need to launch sub processes from Python. Sometimes these processes don’t work as expected, and end up blocking the program. If you want to launch a command from python, and cut it off after a certain amount of time, I recommend the threading module.

Here is how to limit the command to running for roughly 5 seconds:

#!/usr/bin/env python
import commands
import datetime
import time
import threading

class runCommand(threading.Thread):
    def run(self):
        status, output = commands.getstatusoutput('sleep 8')

if __name__ == "__main__":
    startTime = datetime.datetime.now()
    maximumRunTime = datetime.timedelta(seconds = 5)
    mythread = runCommand()
    mythread.start()

    while 1:
        if mythread.isAlive():
            print "Thread has not returned yet..."
        else:
            print "Thread is all done."
            break

        if (datetime.datetime.now() - startTime) > maximumRunTime:
            print "Thread has ran too long, giving up."
            break

        time.sleep(1)

    print "Program done"

So when we enter main, I assign the current time to startTime. Then I define maximumRunTime as a difference of 5 seconds. The thread is started, and every second we check to see if it is still running. If it is, we go ahead and check how much real time has elapsed.

Tags: , ,

NCPFS

NCPFS each release generally is sent out completely not working until I fix it. If you have C experience, you can help me fix up this source. I have started a github.com project called ‘ncpfs-ng’. Here I will be committing my bug fixes. There are about 2 trillion compiler warnings that need to be addressed, so any help is welcome. If you are interested or have a patch, you can reach me at sharms at ubuntu dot com.

Immediate goal is fixing the package on 32-bit systems Jaunty and up, as GCC changed a lot since the Intrepid release, causing issues:
https://bugs.launchpad.net/ubuntu/+source/ncpfs/+bug/328020

My debdiff there does make it work for amd64, but i386 users still have no luck. Github page is at: http://github.com/sharms/ncpfs-ng/tree/master

Tags: , ,

Interesting Blogs

With respect to my last blog post, apparently everyone cares about boot speed :)

Blogs
My Google Reader has a ton of feeds, figure I would highlight some of the more interesting ones I have found:

Tags: , ,