Skip to contentSkip to conversation

net/socket: set socket inode times to current_time

PATCH In review Wenhua Zhao 2 participants 2 messages Feb 1, 2023 – Feb 2, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/socket: set socket inode times to current_time

Socket creation time are sometimes useful but not available becasue the socket inode times are not set when initializing the inode. This patch sets the socket inode times to current_time().

Before the fix, the socket inode times are at epoch, for example:

    $ stat -L /proc/383/fd/3
      File: /proc/383/fd/3
      Size: 0               Blocks: 0          IO Block: 4096   socket
    Device: 0,8     Inode: 15996       Links: 1
    Access: (0777/srwxrwxrwx)  Uid: ( 1000/    arch)   Gid: ( 1000/    arch)
    Access: 1970-01-01 00:00:00.000000000 +0000
    Modify: 1970-01-01 00:00:00.000000000 +0000
    Change: 1970-01-01 00:00:00.000000000 +0000

After the fix, the inode times are the socket creation time:

    $ stat -L /proc/254/fd/3
      File: /proc/254/fd/3
      Size: 0               Blocks: 0          IO Block: 4096   socket
    Device: 0,7     Inode: 13170       Links: 1
    Access: (0777/srwxrwxrwx)  Uid: ( 1000/    arch)   Gid: ( 1000/    arch)
    Access: 2023-02-01 03:27:50.094731201 +0000
    Modify: 2023-02-01 03:27:50.094731201 +0000
    Change: 2023-02-01 03:27:50.094731201 +0000
---
 net/socket.c | 1 +
 1 file changed, 1 insertion(+)
net/socket.c +1 -0
index 888cd618a968..c656c9599a92 100644
@@ -635,6 +635,7 @@ struct socket *sock_alloc(void)
 	inode->i_uid = current_fsuid();
 	inode->i_gid = current_fsgid();
 	inode->i_op = &sockfs_inode_ops;
+	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
 
 	return sock;
 }
Signature · 1 line
2.39.1
#1 lore raw
From:   Wenhua Zhao <[email protected]>
Date:   Tue, 31 Jan 2023 20:30:19 -0800
Quoted 26 lines

Socket creation time are sometimes useful but not available becasue the socket inode times are not set when initializing the inode. This patch sets the socket inode times to current_time().

Before the fix, the socket inode times are at epoch, for example:

    $ stat -L /proc/383/fd/3
      File: /proc/383/fd/3
      Size: 0               Blocks: 0          IO Block: 4096   socket
    Device: 0,8     Inode: 15996       Links: 1
    Access: (0777/srwxrwxrwx)  Uid: ( 1000/    arch)   Gid: ( 1000/    arch)
    Access: 1970-01-01 00:00:00.000000000 +0000
    Modify: 1970-01-01 00:00:00.000000000 +0000
    Change: 1970-01-01 00:00:00.000000000 +0000

After the fix, the inode times are the socket creation time:

    $ stat -L /proc/254/fd/3
      File: /proc/254/fd/3
      Size: 0               Blocks: 0          IO Block: 4096   socket
    Device: 0,7     Inode: 13170       Links: 1
    Access: (0777/srwxrwxrwx)  Uid: ( 1000/    arch)   Gid: ( 1000/    arch)
    Access: 2023-02-01 03:27:50.094731201 +0000
    Modify: 2023-02-01 03:27:50.094731201 +0000
    Change: 2023-02-01 03:27:50.094731201 +0000
Quoted 1 line

Looks good, but we may want to use another example in changelog because you can get almost the same time without the -L option and the use case is bit confusing at least for me.

I guess you will use it in your application, not from the outside world by stat.

  $ python3
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

from socket import socket
from os import fstat

sk = socket()
fstat(sk.fileno())

os.stat_result(..., st_atime=0, st_mtime=0, st_ctime=0)

Quoted 18 lines
---
 net/socket.c | 1 +
 1 file changed, 1 insertion(+)
net/socket.c +1 -0
index 888cd618a968..c656c9599a92 100644
@@ -635,6 +635,7 @@ struct socket *sock_alloc(void)
 	inode->i_uid = current_fsuid();
 	inode->i_gid = current_fsgid();
 	inode->i_op = &sockfs_inode_ops;
+	inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
 
 	return sock;
 }
Signature · 1 line
2.39.1