質問編集履歴

2

ソースコードの追加

2018/01/31 13:44

投稿

u_09
u_09

スコア18

test CHANGED
File without changes
test CHANGED
@@ -55,3 +55,115 @@
55
55
 
56
56
 
57
57
  回答よろしくお願いいたします.
58
+
59
+
60
+
61
+ 頂いた回答に対しての追記
62
+
63
+
64
+
65
+ ```
66
+
67
+ #include <net/inet_sock.h> // inet_sk()
68
+
69
+
70
+
71
+ static struct sock *get_sock(struct file *fd)
72
+
73
+ {
74
+
75
+ struct path f_path = fd->f_path;
76
+
77
+ struct dentry *dentry;
78
+
79
+ struct inode *inode;
80
+
81
+ struct socket *sock;
82
+
83
+
84
+
85
+ if (!(dentry = f_path.dentry))
86
+
87
+ return NULL;
88
+
89
+ if (!(inode = dentry->d_inode))
90
+
91
+ return NULL;
92
+
93
+ if (!(sock = SOCKET_I(inode)))
94
+
95
+ return NULL;
96
+
97
+ return sock->sk;
98
+
99
+ }
100
+
101
+
102
+
103
+ void __fd_install(struct files_struct *files, unsigned int fd, struct file *file)
104
+
105
+ {
106
+
107
+ struct fdtable *fdt;
108
+
109
+
110
+
111
+ might_sleep();
112
+
113
+ rcu_read_lock_sched();
114
+
115
+
116
+
117
+ while (unlikely(files->resize_in_progress)) {
118
+
119
+ rcu_read_unlock_sched();
120
+
121
+ wait_event(files->resize_wait, !files->resize_in_progress);
122
+
123
+ rcu_read_lock_sched();
124
+
125
+ }
126
+
127
+ /* coupled with smp_wmb() in expand_fdtable() */
128
+
129
+ smp_rmb();
130
+
131
+ fdt = rcu_dereference_sched(files->fdt);
132
+
133
+ BUG_ON(fdt->fd[fd] != NULL);
134
+
135
+ rcu_assign_pointer(fdt->fd[fd], file);
136
+
137
+ rcu_read_unlock_sched();
138
+
139
+
140
+
141
+ // 追加箇所
142
+
143
+ if (fdt->fd[fd]) {
144
+
145
+ struct sock *sk;
146
+
147
+ struct inet_sock *inet;
148
+
149
+ if (sk = get_sock(fdt->fd[fd])) {
150
+
151
+ if ((inet = inet_sk(sk))) {
152
+
153
+ // ここがエラーになる
154
+
155
+ __be32 addr = inet->inet_daddr;
156
+
157
+ __u16 port = ntohs(inet->inet_dport);
158
+
159
+ printk(KERN_EMERG "[%d] %08X:%04X\n", current->pid, addr, port);
160
+
161
+ }
162
+
163
+ }
164
+
165
+ }
166
+
167
+ }
168
+
169
+ ```

1

ポインタ確保のタイミングではなくデータ登録のタイミングということが分かりにくい文章だったのを修正

2018/01/31 13:44

投稿

u_09
u_09

スコア18

test CHANGED
File without changes
test CHANGED
@@ -50,7 +50,7 @@
50
50
 
51
51
 
52
52
 
53
- 質問としてはこの fd[i] のポインタがどこで登録されているのかということを教えていただきたいです. ソケット生成の (bind() とか sock_create() とか) あたりを辿っても task_struct がありませんし, どのタイミングで紐付けられているのかがわかりません. カーネルのバージョンは 4.4.0-87-generic を使用しています.
53
+ 質問としてはこの fd[i] のポインタが指す先のデータ (この場合はソケットの情報) がどこで登録されているのかということを教えていただきたいです (ポインタが指す先のメモリの確保のタイミングではなく, 情報が結び付けられるタイミング). ソケット生成の (bind() とか sock_create() とか) あたりを辿っても task_struct がありませんし, どのタイミングで紐付けられているのかがわかりません. カーネルのバージョンは 4.4.0-87-generic を使用しています.
54
54
 
55
55
 
56
56