error: expected declaration specifiers or '...' before syslog

When I compile this i get the following error

"error: expected declaration specifiers or '...' before syslog"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define __LIBRARY__ 

#include <linux/unistd.h>

/* define the system call, to override the library function */
_syscall3(int, syslog, int, type, char *, bufp, int, len);

int main(int argc, char **argv)
{
    int level;

    if (argc==2) {
	level = atoi(argv[1]); /* the chosen console */
    } else {
        fprintf(stderr, "%s: need a single arg\n",argv[0]); exit(1);
    }
    if (syslog(8,NULL,level) < 0) {  
        fprintf(stderr,"%s: syslog(setlevel): %s\n",
                argv[0],strerror(errno));
        exit(1);
    }
    exit(0);
}

You're using the wrong syslog, the kernel one instead of the libc one. The man page tells you to see man 3 syslog.

Will the below code do the same thing because this one compiles without problems

int syslog(int type, char *bufp, int len)
{
    return syscall(__NR_syslog, type, &bufp,len);
};

seems that you're writing user space program (if I refer to your include), so syslog() from the glibc would be probably "better".
man 3 syslog

Just my 2c,
Lo�c

You're writing a userspace program. Do you have a really good reason to be constructing your own system calls instead of using the ones glibc has already provided for this purpose?

I was trying to define a custom system call only as an experiment....

I see! In which case you might find man 2 syscall a better way to do arbitrary syscalls. I've certainly never managed to get _syscall3 and the like to work...

2 Likes