You are not logged in.

#1 2010-08-28 22:06:42

egan
Member
From: Mountain View, CA
Registered: 2009-08-17
Posts: 273

New to Linux Programming -- Need Checker

I have done tons of shell programming and other high level things for a few years now, but only recently have I looked into using system calls in low level programs (C and assembly). I have written a program to clear my RAM cache, and I would like to know of any potential dangers, or otherwise any mistakes I have made therein.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>

int main(void)
{
    const char *str = "3";
    uid_t uid;
    FILE *fp;

    uid = getuid();
    if (uid != 0) {
        fprintf(stderr, "Error: Not superuser\n");
        exit(EACCES);
    }

    fp = fopen("/proc/sys/vm/drop_caches","w");
    if (fp == NULL) {
        fprintf(stderr, "Error: Null file handle\n");
        exit(ENOENT);
    }
    sync();
    fwrite(str, 1, 1, fp);
    fclose(fp);
    
    return 0;
}

Any advice or consideration is greatly appreciated.

Offline

#2 2010-08-28 22:34:47

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: New to Linux Programming -- Need Checker

If you're going to use the POSIX error numbers, EBADF might be a better choice than ENOENT (which is typically what stat(3) returns on a failed call).

You could tighten things up a bit with variable naming -- if you don't plan on reusing the return of a call, don't save it.

if (getent() != 0)

This also avoids you having to include the sys/types.h header. Also, you might be interested in perror() instead of making your own error strings.

I could nitpick other minutia, but this looks fine for what it is.

Offline

#3 2010-08-28 22:39:27

daemotron
Member
Registered: 2010-08-27
Posts: 8
Website

Re: New to Linux Programming -- Need Checker

When doing low level stuff (which is system-specific anyway), I generally prefer to use the low level I/O functions. In your program this wouldn't make any difference since your program terminates very soon after having written to the FILE fp. In bigger projects dealing with system level stuff, I'd prefer to avoid the use of functions which imply any sort of buffering (as fwrite does by default). But in your case that's more a question of style... Btw. you could return EXIT_SUCCESS instead of 0 - just another cosmetic thing.

As a security measure, your program could check whether /proc/sys/vm/drop_caches is actually a regular file or a (potentially evil) symbolic link to something you'd better not write into. This check could be accomplished using lstat() from sys/stat.h.


“Some humans would do anything to see if it was possible to do it. If you put a large switch in some cave somewhere, with a sign on it saying 'End-of-the-World Switch. PLEASE DO NOT TOUCH', the paint wouldn't even have time to dry.” — Terry Pratchett, Thief of Time

Offline

#4 2010-08-29 20:36:03

wu.soldier
Member
From: Poland
Registered: 2006-12-19
Posts: 22

Re: New to Linux Programming -- Need Checker

falconindy wrote:

Also, you might be interested in perror() instead of making your own error strings.

There is also a strerror() function defined in string.h. It returns error string using current locale settings.

Offline

Board footer

Powered by FluxBB