1 /* -*- linux-c -*- --------------------------------------------------------- *
3 * linux/fs/devpts/inode.c
5 * Copyright 1998-2004 H. Peter Anvin -- All Rights Reserved
7 * This file is part of the Linux kernel and is made available under
8 * the terms of the GNU General Public License, version 2, or at your
9 * option, any later version, incorporated herein by reference.
11 * ------------------------------------------------------------------------- */
13 #include <linux/module.h>
14 #include <linux/init.h>
16 #include <linux/sched.h>
17 #include <linux/namei.h>
18 #include <linux/mount.h>
19 #include <linux/tty.h>
20 #include <linux/devpts_fs.h>
22 #define DEVPTS_SUPER_MAGIC 0x1cd1
24 static struct vfsmount
*devpts_mnt
;
25 static struct dentry
*devpts_root
;
33 } config
= {.mode
= 0600};
35 static int devpts_remount(struct super_block
*sb
, int *flags
, char *data
)
45 while ((this_char
= strsep(&data
, ",")) != NULL
) {
50 if (sscanf(this_char
, "uid=%i%c", &n
, &dummy
) == 1) {
53 } else if (sscanf(this_char
, "gid=%i%c", &n
, &dummy
) == 1) {
56 } else if (sscanf(this_char
, "mode=%o%c", &n
, &dummy
) == 1)
59 printk("devpts: called with bogus options\n");
63 config
.setuid
= setuid
;
64 config
.setgid
= setgid
;
72 static struct super_operations devpts_sops
= {
73 .statfs
= simple_statfs
,
74 .remount_fs
= devpts_remount
,
78 devpts_fill_super(struct super_block
*s
, void *data
, int silent
)
82 s
->s_blocksize
= 1024;
83 s
->s_blocksize_bits
= 10;
84 s
->s_magic
= DEVPTS_SUPER_MAGIC
;
85 s
->s_op
= &devpts_sops
;
92 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
94 inode
->i_blksize
= 1024;
95 inode
->i_uid
= inode
->i_gid
= 0;
96 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
| S_IWUSR
;
97 inode
->i_op
= &simple_dir_inode_operations
;
98 inode
->i_fop
= &simple_dir_operations
;
101 devpts_root
= s
->s_root
= d_alloc_root(inode
);
105 printk("devpts: get root dentry failed\n");
111 static struct super_block
*devpts_get_sb(struct file_system_type
*fs_type
,
112 int flags
, const char *dev_name
, void *data
)
114 return get_sb_single(fs_type
, flags
, data
, devpts_fill_super
);
117 static struct file_system_type devpts_fs_type
= {
118 .owner
= THIS_MODULE
,
120 .get_sb
= devpts_get_sb
,
121 .kill_sb
= kill_anon_super
,
125 * The normal naming convention is simply /dev/pts/<number>; this conforms
126 * to the System V naming convention
129 static struct dentry
*get_node(int num
)
132 struct dentry
*root
= devpts_root
;
133 mutex_lock(&root
->d_inode
->i_mutex
);
134 return lookup_one_len(s
, root
, sprintf(s
, "%d", num
));
137 int devpts_pty_new(struct tty_struct
*tty
)
139 int number
= tty
->index
;
140 struct tty_driver
*driver
= tty
->driver
;
141 dev_t device
= MKDEV(driver
->major
, driver
->minor_start
+number
);
142 struct dentry
*dentry
;
143 struct inode
*inode
= new_inode(devpts_mnt
->mnt_sb
);
145 /* We're supposed to be given the slave end of a pty */
146 BUG_ON(driver
->type
!= TTY_DRIVER_TYPE_PTY
);
147 BUG_ON(driver
->subtype
!= PTY_TYPE_SLAVE
);
152 inode
->i_ino
= number
+2;
153 inode
->i_blksize
= 1024;
154 inode
->i_uid
= config
.setuid
? config
.uid
: current
->fsuid
;
155 inode
->i_gid
= config
.setgid
? config
.gid
: current
->fsgid
;
156 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
157 init_special_inode(inode
, S_IFCHR
|config
.mode
, device
);
158 inode
->u
.generic_ip
= tty
;
160 dentry
= get_node(number
);
161 if (!IS_ERR(dentry
) && !dentry
->d_inode
)
162 d_instantiate(dentry
, inode
);
164 mutex_unlock(&devpts_root
->d_inode
->i_mutex
);
169 struct tty_struct
*devpts_get_tty(int number
)
171 struct dentry
*dentry
= get_node(number
);
172 struct tty_struct
*tty
;
175 if (!IS_ERR(dentry
)) {
177 tty
= dentry
->d_inode
->u
.generic_ip
;
181 mutex_unlock(&devpts_root
->d_inode
->i_mutex
);
186 void devpts_pty_kill(int number
)
188 struct dentry
*dentry
= get_node(number
);
190 if (!IS_ERR(dentry
)) {
191 struct inode
*inode
= dentry
->d_inode
;
199 mutex_unlock(&devpts_root
->d_inode
->i_mutex
);
202 static int __init
init_devpts_fs(void)
204 int err
= register_filesystem(&devpts_fs_type
);
206 devpts_mnt
= kern_mount(&devpts_fs_type
);
207 if (IS_ERR(devpts_mnt
))
208 err
= PTR_ERR(devpts_mnt
);
213 static void __exit
exit_devpts_fs(void)
215 unregister_filesystem(&devpts_fs_type
);
219 module_init(init_devpts_fs
)
220 module_exit(exit_devpts_fs
)
221 MODULE_LICENSE("GPL");