CreateProcess for Linux

January 20th, 2011 Open Source, Safe For Seneca

By Andrew Smith

Do you remember the first time someone told you about fork()/exec() for Linux? Do you remember beeing completely confused? I do, and it’s an ongoing pain in the ass for me – every time I want to call something simple without blocking and without worrying about pipes or the structure of exec()/execl()/execlp() parameters, or the return of the fork() call.

Finally I had enough and wrote my own CreateProcess function. It will seem trivial for any Linux C programmer, but in my experience it’s not simple enough. This or something like it should have been part of the standard library, just like printf() is.

Feel free to use it for whatever you want, minding the GPL v2 licence:

/******************************************************************************
*   CreateProcess
*
*   Copyright (C) 2010 Andrew Smith
*
*   This program is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, version 2 of the License only, not
*   any earlier or later version.
*
*   You should have received a copy of the GNU General Public License
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/******************************************************************************
*  CreateProcess function: a simple way for Linux/Unix to call programs
* without blocking and without the complications of fork()/exec() which
* normal people shouldn't need to understand :)
*
*  Usage example:

#include "CreateProcess.h"

int main(void)
{
    // Call it like this:
    CreateProcess("ls", "-a -l --sort=size");

    // Or like this:
    CreateProcess("date", NULL);

    // Note that the second call does not wait for the first to finish
    // as system() would

    return 0;
}

* Enjoy!
* - Andrew Smith
*/

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

pid_t CreateProcess(const char* command, const char* parametersIn)
{
    const int maxNumArgs = 1024;
    const char* args[maxNumArgs];
    char* parameters = NULL;

    memset(args, 0, (sizeof(char*) * maxNumArgs));
    args[0] = command;

    if(parametersIn != NULL)
    {
        parameters = strdup(parametersIn);
        int strLen = strlen(parameters);

        int numParameters = 1;
        bool expectNextParam = true;
        int i;
        for(i = 0; i < strLen; i++)
        {
            if(parameters[i] == ' ' || parameters[i] == '\t' ||
               parameters[i] == '\n')
            {
                expectNextParam = true;
                parameters[i] = '\0';
            }
            else if(expectNextParam)
            {
                args[numParameters] = &(parameters[i]);
                numParameters++;
                expectNextParam = false;
            }
        }
    }

    pid_t pid = fork();
    if(pid == 0)
    {
        execvp(command, (char**)args);
        _exit(1);
    }

    if(parameters != NULL)
        free(parameters);

    return pid;
}

How to save 10$ a month on the power bill

January 16th, 2011 Safe For Seneca

By Andrew Smith

Once a year or two ago they were lending out power meters at the Richmond Hill library, the sort of thing that you put in between an electrical device and the power plug in the wall to see how much power the device uses. Trying to find it again.. PowerStream’s Watt Reader Loan Program, could be that you can still borrow one at the library.

I’ve measured just about everything in the house where I lived at that time (here’s the spreadsheet) and some things were just stupid, like my speaker system that would suck up half of it’s maximum power even when powered off.

That project was left on hold for a while, until recently when I started paying my own bills and was wondering how much of the idle computer stuff I have is costing. I have a large desk with two desktops and one or two laptops, a laser printer, some external harddrives, speakers, and random other things. Instead of measuring them all separately I plugged them all into one power bar (I’m oversimplifying of course), had that power bar sit on the desk, and turned it off when I was done with my computers for a while.

Some things like the DSL modem and Wireless Router were not powered through this circuit, and would continue to stay on 24×7.

It may have been a coincidence but it’s been 3 months now and my electric bill has gone down by 10$. I’m pretty sure I haven’t done anything else, though I don’t really understand how air conditioning works in a condo, that may have used some power the few times we turned it on.

The system is not that complicated. Just look under your computer desk at home and you’re likely to find over ten things plugged in all the time. Most of these are using power even when you’re not doing anything with them, and all together using a good hundred watts. You leave all that plugged in and it’s the same as having a big room lit non-stop. It’s a shame and a damned waste of time.

It’s kind of fun too, though maybe that’s just me, I guess not everyone will build a power bar like I have, but if it’s not fun just think of the bragging rights you’ll get and the 120$/year you’ll save :)

Two years of uptime

January 9th, 2011 Open Source, Safe For Seneca

By Andrew Smith

Check this out:

andrew@littlesvr:~$ uptime
00:20:16 up 730 days, 15:13,  1 user,  load average: 0.57, 0.22, 0.11

At 4AM this morning it was 730 days, that’s exactly two years!

The server would have been up for about 6 years if I didn’t decide once to upgrade the hardware, once to upgrade Slackware, and once to be hacked :) Ok that last one wasn’t a conscious decision, and it may happen again since I haven’t ever figured out what the vulnerability was.

Yeah, this little box has been running for this long, and yes, it does have spiderwebs on it. I didn’t take them off when I saw them because that is so cool and since I have no fans they really don’t cause any trouble.

The server is sitting on a UPS that I bought years ago. Today I tested it by cutting the power, expecting that it will die immediately. I was pleasantly surprised to see that the server (and DSL modem and hub) were still on after the 5 minute battery test. I didn’t want to test it for longer than that because I remember the spec for the UPS said 8 minutes, and I wonder if maybe a year from now I’ll be celebrating a 3-year uptime anniversary :)

Maybe in a year or two I’ll replace it with a faster, fancier, even smaller Arm box like one of those Chris Tyler is playing with. But for now I have no need.

Unfortunately my Slackware upgrade came with an Apache upgrade which means the log formats are different between the two periods, so I can’t say how much data has been transferred, but this webaliser screenshot will give you an idea of what’s typical:

(for the observant ones: legrand-sw is the workstation where I ran webaliser on the littlesvr logs)

That’s roughly 240GB of data uploaded per year (caching is on by default), and 9600 unique visitors per year.

I think it’s pretty good for a tiny hobby web server on a residential ADSL connection serving hobby content :)

And of course this is not just a web server but also my email server, which I don’t have stats for but it’s the only email I really use. With any luck it will also become an LDAP server, if I can set it up so that it’s compatible with Thunderbird and easily updateable.

Long live Slackware!

What’s digital broadcasting got to do with animated images?

January 7th, 2011 Open Source, Safe For Seneca

By Andrew Smith

I just finished my master’s so I’m allowed to quote Wikipedia again :)

In 2008 WorldDMB adopted APNG as a backward compatible extension to enable animation as part of the MOT SlideShow user application for Digital Radio. “APNG 1.0 Specification – Animated Portable Network Graphics” is included as normative Annex A in the ETSI standard TS 101 499 V2.2.1 (2008-07). In 2010 Commercial Radio Broadcasters in Sydney began to include APNG animations in DAB+ digital radio broadcasts. These APNG animations are carried by the “MOT slideshow” application which accompanies the audio services. It is expected that other cities in Australia will follow in early 2011.

That’s noteworthy because as far as I know they are the first real users of APNG. Mozilla has been using it for a long time and there are several decent applications that work with APNGs, but there has not to date been a solid creator of Animated PNGs.

I don’t know what a DAB+ digital radio broadcast is (and I don’t really care), but this is really cool!

The people want APNG!

October 19th, 2010 Open Source

By Andrew Smith

APNG has very slowly (but steadily) gaining popularity. One of the recent things I was told about is the current list of most popular ideas Ubuntu Brainstorm: APNG Support.

I’ll put a screenshot here for posterity:

That’s pretty cool. The thing that makes me feel best is the overwhelming positive votes. Looking at the rest of the ideas on the website – most of them don’t have even 50% of the votes positive.

Neat! Maybe there will be APNG in Ubuntu. Not that it will make any difference to libpng proper, those guys are too stuck up. But maybe given enough forks they will reconsider.

Dealing with stress – some truths to help

October 8th, 2010 Safe For Seneca

I’ve been having a really hard time on my new job. I had to help out on a project where I knew nothing about the technology or the code or even the basic concepts.

It was so bad at times I thought I would go crazy or else my head would explode. Then I got over it and I figured I’ll record my experience here – for myself and for students who might be reading and will end up in similar situations.

I always say that however hard one thinks that school is – work (if it’s a good job) is much harder. Not just in terms of time and energy required, but a number of other skills too. One of them is dealing with extreme requirements.

“Time management” does not do this concept justice. In the past I used stickies or bug tracking systems or email to prioritise all the work I had to do, and that helped me deal with too many requirements. Scope creep was my favourite thought when deciding what to do.

This time I was in a different situation. I could not do the same as usual because I had no idea how to do any of the work I needed to do, no idea how long it would take, and I was given no time to learn anything. The deadlines were very ambitions, the actual (or imagined) effect of potential failure very severe. Everything had to be done right now, with no hacks, fully functioning, etc.

This caused an enormous amount of stress. So much that often I nearly cried, and at other times I felt like a completely useless fool. Luckily I used to think of such situations when I had the time and energy to contemplate, and I was able to recall some of my conclusions. Those recollections brought me back to life.

Truth #1. You cannot do the impossible

Based on others’ expectations and beliefs you may be tempted to think that you aren’t smart enough or experienced enough or work hard enough and that’s why you’re not getting your assignment done. Through that thought out the window.

Whatever position you’re holding – it didn’t fall on your lap, you got it because you’ve proven you deserve it. If it requires more time than someone else thinks – that’s a fact. State it, and stick to it.

Truth #2. You must not overwork yourself

It is true that for a short stretch of time one can work really hard without any breaks, and deliver more in 48 hours than they would normally in two weeks. And you can do that in extreme cases, when it is warranted.

The problem is that after these 48 hours you are so burned out that the following two weeks you’re useless. Any benefit of the hard push is thus nullified. Furthermore there are other problems such as broken morale, damaged relationships, and physical damage that will take much longer to recover.

If you explain this to your boss doesn’t get this – tell him to kiss your ass. If you don’t have the balls or the opportunity to explain this – hold to this rule anyway, you will be better off in the end.

Truth #3. Usual time management techniques still help

Once you internalise truths #1 and #2 – get back to how you normally prioritise tasks. Handle blocking tasks first; neglect “requirements” that came out of someone’s ass; multitask when reasonable to do so; don’t hesitate to ask for advice; be proud of what you get done when you get it done; and state when you need help.

Will these truths apply to your experience? Maybe, maybe not. What helped me is that I had them in my head before the serious stress came, and when it did I just had to recall them. Once I did – I felt better right away and got back to getting things done to the best of my ability, instead of crying myself into a mess.

Suspend and hibernate in Linux

May 29th, 2010 Open Source

By Andrew Smith

Suspend and hibernate have been a sad story on Linux for a very long time. Typically the hardware makers are blamed, saying “oh they all do it differently none of it’s documented”. That may be true but it’s a bunch of horseshit anyway – hardly any of the hardware that works at all or perfectly in Linux is the same or is documented.

A couple of days ago I upgraded to Slackware 13.1. I left suspend as the last thing to set up, as it wasn’t working at all in 13.0 and barely working in 12.2. And you know what? It works now, it just works. I could hardly believe it. No messing with ACPI options, no hooks to remove my USB modules, no fancy keyboard configuration. I just press my FnF5 and it goes to sleep.

That was pretty impressive to begin with. And then I tried to hibernate. One try and I realised I needed an extra resume=/dev/sda5 in my lilo.conf. That one change.. and.. hibernate also works.

I am shocked. In a good way. Suspend is a must on a laptop and hibernate is really nice to have. Thanks, whomever fixed this!

For reference: I have an HP G50 and Slackware 13.1 comes with the kernel 2.6.33.4

Certificate expiry

March 24th, 2010 Open Source

By Andrew Smith

At some point in the distant past I’ve set up my own email server, running Sendmail for SMTP and the UWO imapd server for IMAP. Part of the setup process is creating certificates – so that you can have a secure tunnel for free, using self-signed certs.

Like a good boy I followed the instructions to the letter. Now I’m presented with a message box in Thuderbird saying “your certificate expired”. Eh? Why? Oh, I know – it’s because you’re supposed to have an expiry date on your own generated certs, because bla proper bla security bla I know better bla. I never got the bla bla part, never had any use for it. And now I ran into the problem I knew I would run into eventually.

Security, expiry? Did these guys think? I’ll tell you something – I can still get to my email using Thunderbird. It just puts up an annoying warning and makes me click continue every time. What sort of security is this expiration providing? If anything, security is worse now.

I must have created this setup a long time ago (likely 5 years) – because today I would never do something I believe is stupid just because someone “who knows better” said so. Oh I’m glad I made that decision.

Oh Google, WTF?

March 11th, 2010 Uncategorized

By Andrew Smith

I have to do research for school. I’ve been told many a time don’t use Google. Now Google is giving me a reason to be pissed off.

When the search results come back with PDF files – I don’t get to see the link to the PDF where it actually sits, the link I get is in this form:

http://www.google.ca/url?sa=t&source=web&ct=res&cd=3&ved=0CBcQFjAC&url=http%3A%2F%2Fwww.perforce.com%2Fperforce%2Fconferences%2Fus%2F2009%2FPresentations%2FCohen-CodeReview-slides.pdf&ei=XJWZS4-IMIGSNpSexHo&usg=AFQjCNE2L5ejvkMYs4RMS4mZu9v7nZO9iA

Are you joking? What is the purpose of this? Is it to make me google it every time? Is it to track when I click on the results? Did some asshole think this protects IP? Rather than reformatting that URL for my records I might decide using the library is easier.

Remember how good you are

January 12th, 2010 Safe For Seneca

By Andrew Smith

I’ve been reading about literate programming, and was reminded that most programmers don’t write comments in their code. Whether to write them or not is a question that’s asked all the time, in all circles – starting in school and all the way to teams of masters of the known universe like Linus (who I’m assuming is responsible for this).

In school I thought comments are good. I’ve seen a few people who felt the same change their mind once they started writing real software, and I was tempted to drop it myself, but I didn’t.

I’ve had the same arguments with myself that you’re thinking of now. Should I document design, exceptions, explain what complicated code does, go over the assumptions, etc.? This post is not about that, it’s about something I’ve realised a few days ago – a thought that may be slightly original.

Writing comments in my code makes me feel good. I’m not a design freek and my code goes through a couple of iterations before it’s even checked into version control, often followed by more restructuring when flaws are found. And then I write comments.

I look over a piece of code and think – what the hell is this doing, here? Oh – that’s to handle the weird case of an idiot unplugging the USB stick before it’s done. Comment. Man this function is long and hard to read. Several comments throughout telling a story (this being possible explains why it’s all in one function). Wow, at this moment I can actually grasp how the entire subsystem works – comments on top of the class and every function. Why is this a member variable and not a local one? Oh yeah, I remember – that’s bizarre but it works. Comment.

Did I manage to explain it? As I read my code I remember all the interesting, strange, unfortunate, and neat things I’ve done. And I write it down in plain english. I feel empowered when I can explain something that appears to be a terrible mess. I think this is why I still do write comments. And I know they’re useful because when I go back to my code a month later I can actually figure out what’s where using them.

Maybe it will work for you too?