Пример #1
0
static void tidy_load_file(const char* file)
{
    int fd = rb->open(file, O_RDONLY);
    char buf[MAX_PATH], *str, *remove;
    char last_group[MAX_PATH] = "";
    if (fd < 0)
        return;

    while ((tidy_type_count < sizeof(tidy_types) / sizeof(tidy_types[0])) && rb->read_line(fd, buf, MAX_PATH))
    {
        if (!rb->settings_parseline(buf, &str, &remove))
            continue;

        if (*str == '\\') /* escape first character ? */
            str++;
        unsigned i = find_file_string(str, last_group);

        tidy_types[i].remove = rb->strcmp(remove, "yes");

        if (i >= tidy_type_count)
        {
            i = tidy_type_count;
            add_item(str, i);
            tidy_type_count++;
        }
        if (str[0] == '<')
            rb->strcpy(last_group, str);
    }
    rb->close(fd);
}
Пример #2
0
static ssize_t mychardrv_write (struct file * f, const char __user * address,
                                 size_t size, loff_t * offset)
{
   struct strings_representation * sr = f->private_data;
   ssize_t res = 0;
   long result;
   struct file_string * fstr = find_file_string(f);
   if (fstr ==NULL) {
      // add a new string
      fstr = kzalloc(sizeof(struct file_string), GFP_USER);
      if (fstr == NULL) {
         res = -ENOMEM;
         goto error;
      }
      fstr->string = kzalloc(size, GFP_USER);
      if (fstr->string==NULL) {
         kfree (fstr);
         res = -ENOMEM;
         goto error;
      }
      fstr->id = sr->current_id;
      fstr->next = sr->next;
      sr->next = fstr;
   }
   else {
      char * tmp = kzalloc(size, GFP_USER);
      if (tmp==NULL) {
         res = -ENOMEM;
         goto error;
      }
      kfree (fstr->string);
      fstr->string = tmp;
   }
   result = probe_kernel_read(fstr->string, address, size);
   printk (KERN_DEBUG "probe_kernel_write returns %ld\n", result);
   res = result;
   if (result < 0) {
      printk (KERN_DEBUG "string is nullified\n");
      fstr->string = NULL;
      goto error;
   }

   res = copy_from_user(fstr->string, address, size);
   if (res == 0) {
      printk(KERN_DEBUG "copied %ld bytes from the user at offset %ld\n", size, offset);
      res = size;
      *offset += size;
   }
   else {
      printk(KERN_DEBUG "error: %ld bytes could not be copied from user at offset %ld\n", res, offset);
      res = (ssize_t) -EFAULT;
   }

error:
   return res;
}
Пример #3
0
static ssize_t mychardrv_read(struct file * f, char __user * address,
                            size_t size, loff_t * offset)
{
   ssize_t res = 0;
   ssize_t copy_size;
   struct file_string * fstr = find_file_string(f);
   printk(KERN_DEBUG "device read to user address %p\n", address);
   printk(KERN_DEBUG "   requested size %ld bytes\n", size);
   if (offset==NULL)
      printk(KERN_DEBUG "   current file offset not provided (NULL)\n");
   else
      printk(KERN_DEBUG "   current file offset %lld\n", *offset);

   if (fstr!=NULL) {
    if (fstr->string!=NULL) {
      copy_size = min(size, strlen(fstr->string)+1);
      res = copy_to_user (address, fstr->string, copy_size);
      if (res == 0)
         printk(KERN_DEBUG "copied %ld bytes to the user\n", copy_size);
      else {
         printk(KERN_DEBUG "error: %ld bytes could not be copied to user\n", res);
         copy_size = (ssize_t) -EFAULT;
      }
    }
    else {
       copy_size = 0;
    }
   }
   else if (size < sizeof (num_opens)) copy_size = (ssize_t) -ENOBUFS;
   else {
      // implementation, copy the num_opens number into the user buffer
      copy_size = sizeof(num_opens);
      res = copy_to_user (address, &num_opens, copy_size);
      if (res == 0)
         printk(KERN_DEBUG "copied %ld bytes to the user\n", copy_size);
      else {
         printk(KERN_DEBUG "error: %ld bytes could not be copied to user\n", res);
         copy_size = (ssize_t) -EFAULT;
      }
   }

   return copy_size;
}