|
|
Subscribe / Log in / New account

The /proc/sequence module source

/*
 * Simple demonstration of the seq_file interface.
 *
 * $Id: seq.c,v 1.1 2003/02/10 21:02:02 corbet Exp $
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/fs.h>
#include <linux/seq_file.h>
#include <linux/slab.h>

MODULE_AUTHOR("Jonathan Corbet");
MODULE_LICENSE("Dual BSD/GPL");


/*
 * The sequence iterator functions.  We simply use the count of the
 * next line as our internal position.
 */
static void *ct_seq_start(struct seq_file *s, loff_t *pos)
{
	loff_t *spos = kmalloc(sizeof(loff_t), GFP_KERNEL);
	if (! spos)
		return NULL;
	*spos = *pos;
	return spos;
}

static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
{
	loff_t *spos = (loff_t *) v;
	*pos = ++(*spos);
	return spos;
}

static void ct_seq_stop(struct seq_file *s, void *v)
{
	kfree (v);
}

/*
 * The show function.
 */
static int ct_seq_show(struct seq_file *s, void *v)
{
	loff_t *spos = (loff_t *) v;
	seq_printf(s, "%Ld\n", *spos);
	return 0;
}

/*
 * Tie them all together into a set of seq_operations.
 */
static struct seq_operations ct_seq_ops = {
	.start = ct_seq_start,
	.next  = ct_seq_next,
	.stop  = ct_seq_stop,
	.show  = ct_seq_show
};


/*
 * Time to set up the file operations for our /proc file.  In this case,
 * all we need is an open function which sets up the sequence ops.
 */

static int ct_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &ct_seq_ops);
};

/*
 * The file operations structure contains our open function along with
 * set of the canned seq_ ops.
 */
static struct file_operations ct_file_ops = {
	.owner   = THIS_MODULE,
	.open    = ct_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};
	
	
/*
 * Module setup and teardown.
 */

static int ct_init(void)
{
	struct proc_dir_entry *entry;

	entry = create_proc_entry("sequence", 0, NULL);
	if (entry)
		entry->proc_fops = &ct_file_ops;
	return 0;
}

static void ct_exit(void)
{
	remove_proc_entry("sequence", NULL);
}

module_init(ct_init);
module_exit(ct_exit);



to post comments

The /proc/sequence module source

Posted Aug 15, 2018 14:39 UTC (Wed) by sanasrikar@lwn.net (guest, #126364) [Link] (2 responses)

Sub: Request- update this article
i am a complete beginner.
While searching in the INTERNET i came across this lwn article.
As some API's used are outdated i request you to update this article so this will be useful
for lot of beginners like me as lwn is a place where every person who wants to learn linux
comes across

Hope you consider my concern
Thank you very much
Srikar sana

The /proc/sequence module source

Posted Sep 21, 2018 8:39 UTC (Fri) by draganal28 (guest, #96616) [Link]

I don't think it is possible to keep all the articles updated and in sync with all the updates in the kernel. In the end, they have the date when they are posted and so refer to the state as per that date.

You say you are a complete beginner. I'd suggest you continue visiting LWN as a very good reference which will, I am sure, help you to learn and understand the Linux kernel code and keep up with the latest trends. However, for the exact API, I'd suggest you visit kernel documentation, best for the exact kernel version you are working with. For the example this article looks into, check Documentation/filesystems/seq_file.txt.

The /proc/sequence module source

Posted Jul 5, 2023 21:00 UTC (Wed) by julesb (guest, #139334) [Link]

check the below link

https://shorturl.at/pKLUY

Jules


Copyright © 2003, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds