kernel modules programming: struct proc question

Roman Bogorodskiy bogorodskiy at inbox.ru
Wed Mar 17 10:37:06 PST 2004


 Jacques wrote:

> That's only correct for machines with 32-bit ints.  In any case, POSIX
> only specifies that pid_t is a signed integer type... it could be any
> size supported by the implementation.  For portability, you probably
> want to cast to the `biggest' type and use the appropriate printf
> specifier, e.g.
> 
>    printf("%ld", (long)pid_t);
> or
>    printf("%jd", (intmax_t)pid_t); // C99


I've tried all of this ways, and I still have a wrong pid displayed...
I have no idea what I'm doing wrong. 

Here is the full code:

module.c

>---cut 8<---

#include <sys/types.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/module.h>
#include <sys/sysent.h>
#include <sys/types.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/sysproto.h>
#include <sys/syscall.h>
#include <sys/ucred.h>

static int new_open(struct proc *p, register struct open_args *);

static int offset = NO_SYSCALL;

static int 
new_open(struct proc *p, register struct open_args *uap)
{
	char name[NAME_MAX];
	size_t size;
	pid_t pid;

	pid = p->p_pid;

	if((const void*)copyinstr(uap->path, name, NAME_MAX, &size) == (const void*)EFAULT)
		return(EFAULT);

	
	if (name[0] == '/' && name[1] == 't' && name[2] == 'm' && name[3] == 'p' && name[4] == '/') {
		printf("open(2): %s pid: %jd\n", name, (intmax_t)pid);//, (char *)&p->p_comm);
	}
	
	return (open(p, uap));
}

#define AS(name) (sizeof(struct name) / sizeof(register_t))

static struct sysent new_open_sysent = {
	        AS(open_args),
	        (sy_call_t *)new_open
};


static int
load (struct module *module, int cmd, void *arg)
{
        int error = 0;

        switch (cmd) {
	        case MOD_LOAD :
			printf("syscall loaded\n");
			sysent[SYS_open] = new_open_sysent;
			break;
		case MOD_UNLOAD :
		        printf ("syscall unloaded\n");
			sysent[SYS_open].sy_call = (sy_call_t *)open;
		        break;
		default :
		        error = EINVAL;
		        break;
	
	}
	
	return error;
}

SYSCALL_MODULE(syscall, &offset, &new_open_sysent, load, NULL);

>---cut 8<---

Makefile:

>---cut 8<---

KMOD=   tmp_watch
SRCS=   module.c

.include <bsd.kmod.mk>

>---cut 8<---

And uname -rm
5.2.1-RELEASE i386


-Roman Bogorodskiy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 479 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20040317/1e7c8e3c/attachment.bin


More information about the freebsd-hackers mailing list