about the arch-handbook

Soeren Straarup xride at x12.dk
Fri Aug 15 04:28:30 PDT 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



Hi

I have tried to compile the echo psuedo-driver written by Murray Stokeley
under 5.X, first i compared it with the dev/null src in the src tree and
found that the cdevsw where defined in a little different way.
But i cannot find where in the there is some thign wrong this is the
make output
<insert>
xride at ip8:/root/c/echo# make
Warning: Object directory not changed from original /root/c/echo
cc -O -pipe -mcpu=pentiumpro  -D_KERNEL -Wall -Wredundant-decls
- -Wnested-externs -Wstrict-prototypes  -Wmissing-prototypes -Wpointer-arith
- -Winline -Wcast-qual  -fformat-extensions -std=c99 -DKLD_MODULE -nostdinc
- -I-   -I. -I@ -I@/dev -I@/../include -fno-common  -mno-align-long-strings
- -mpreferred-stack-boundary=2 -ffreestanding -Wall -Wredundant-decls
- -Wnested-externs -Wstrict-prototypes  -Wmissing-prototypes -Wpointer-arith
- -Winline -Wcast-qual  -fformat-extensions -std=c99 -c echobuffer.c
echobuffer.c:91: conflicting types for `echo_open'
echobuffer.c:24: previous declaration of `echo_open'
echobuffer.c:100: conflicting types for `echo_close'
echobuffer.c:25: previous declaration of `echo_close'
echobuffer.c:48: warning: `len' defined but not used
*** Error code 1

</insert>

the .c file is attached.


Best regards Søren.

*----------------------------------------------------------------*
| Soeren Straarup                      Mobile: +45 20 27 62 44   |
| FreeBSD wannabe since 2.2.6-R        http://xforce.dk          |
| Also running OpenBSD and NetBSD      aka OZ2DAK aka Xride      |
*----------------------------------------------------------------*
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (FreeBSD)

iD8DBQE/PMPaXTGeGCdlN14RAr0ZAJ9x+vgM0v4zgQ+4GN/+wigh35liawCdEe9o
ngX8EvSbA0xDlRp/DXBmU8E=
=GoYA
-----END PGP SIGNATURE-----

-------------- next part --------------
/*
 * Simple `echo' pseudo-device KLD
 *
 * Murray Stokely
 */


#include <sys/types.h>
#include <sys/module.h>
#include <sys/systm.h> /* uprintf */ 
#include <sys/errno.h>
#include <sys/param.h>  /* defines used in kernel.h */
#include <sys/kernel.h> /* types used in module initialization */
#include <sys/conf.h>   /* cdevsw struct */
#include <sys/uio.h>    /* uio struct */
#include <sys/malloc.h>

#define BUFFERSIZE 256
#define CDEV_MAJOR      33
#define NULL_MINOR      33


/* Function prototypes */
static d_open_t      echo_open;
static d_close_t     echo_close;
static d_read_t      echo_read;
static d_write_t     echo_write;

/* Character device entry points */
static struct cdevsw echo_cdevsw = {
  .d_open	=	echo_open,
  .d_close	=	echo_close,
  .d_maj	=	CDEV_MAJOR,
  .d_name	=	"echo",
  .d_read	=	echo_read,
  .d_write	=	echo_write
};

typedef struct s_echo {
  char msg[BUFFERSIZE];
  int len;
} t_echo;

/* vars */
static dev_t echo_dev;
static int count;
static t_echo *echomsg;
static int len;


MALLOC_DECLARE(M_ECHOBUF);
MALLOC_DEFINE(M_ECHOBUF, "echobuffer", "buffer for echo module");

/*
 * This function acts is called by the kld[un]load(2) system calls to
 * determine what actions to take when a module is loaded or unloaded.
 */
      
static int
echo_loader(struct module *m, int what, void *arg)
{
  int err = 0;
  
  switch (what) {
  case MOD_LOAD:                /* kldload */
    echo_dev = make_dev(&echo_cdevsw,
            0,
            UID_ROOT,
            GID_WHEEL,
            0600,
            "echo");
    /* kmalloc memory for use by this driver */
    /*    malloc(256,M_ECHOBUF,M_WAITOK); */
    MALLOC(echomsg, t_echo *, sizeof(t_echo), M_ECHOBUF, M_WAITOK);
    printf("Echo device loaded.\n");
    break;
  case MOD_UNLOAD:
    destroy_dev(echo_dev);
    FREE(echomsg,M_ECHOBUF);
    printf("Echo device unloaded.\n");
    break;
  default:
    err = EINVAL;
    break;
  }
  return(err);
}

static int
echo_open(dev_t dev, int oflags, int devtype, struct proc *p)
{
  int err = 0;
  
  uprintf("Opened device \"echo\" successfully.\n");
  return(err);
}

static int 
echo_close(dev_t dev, int fflag, int devtype, struct proc *p)
{
  uprintf("Closing device \"echo.\"\n"); 
  return(0);
}

/* 
 * The read function just takes the buf that was saved via
 * echo_write() and returns it to userland for accessing.
 * uio(9) 
 */

static int
echo_read(dev_t dev, struct uio *uio, int ioflag)
{
  int err = 0;
  int amt;

  /* How big is this read operation?  Either as big as the user wants,
     or as big as the remaining data */
  amt = MIN(uio->uio_resid, (echomsg->len - uio->uio_offset > 0) ? echomsg->len - uio->uio_offset : 0);
  if ((err = uiomove(echomsg->msg + uio->uio_offset,amt,uio)) != 0) {
    uprintf("uiomove failed!\n");
  }

  return err;
}

/*
 * echo_write takes in a character string and saves it
 * to buf for later accessing.
 */

static int
echo_write(dev_t dev, struct uio *uio, int ioflag)
{
  int err = 0;

  /* Copy the string in from user memory to kernel memory */
  err = copyin(uio->uio_iov->iov_base, echomsg->msg, MIN(uio->uio_iov->iov_len,BUFFERSIZE));

  /* Now we need to null terminate */
  *(echomsg->msg + MIN(uio->uio_iov->iov_len,BUFFERSIZE)) = 0;
  /* Record the length */
  echomsg->len = MIN(uio->uio_iov->iov_len,BUFFERSIZE);

  if (err != 0) {
    uprintf("Write failed: bad address!\n");
  }

  count++;
  return(err);
}

DEV_MODULE(echo,echo_loader,NULL);


More information about the freebsd-hackers mailing list