Skip to content

Commit 4f2e2b4

Browse files
committed
unix sockets updates and fixes
1 parent 4fbb4e3 commit 4f2e2b4

4 files changed

Lines changed: 608 additions & 182 deletions

File tree

kernel/interfaces/lib/error.cppm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ export namespace lib
8787
connection_refused = ECONNREFUSED,
8888
message_too_long = EMSGSIZE,
8989
address_in_use = EADDRINUSE,
90-
connection_in_progress = EALREADY
90+
already_in_progress = EALREADY,
91+
operation_in_progress = EINPROGRESS
9192
};
9293

9394
template<typename Type>

kernel/interfaces/system/vfs/socket/base.cppm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ export namespace vfs::socket
325325
: protocol { protocol }, family { family }, type { type } { }
326326

327327
virtual auto bind(lib::maybe_uspan<const std::byte> addr) -> lib::expect<void> = 0;
328-
virtual auto connect(lib::maybe_uspan<const std::byte> addr) -> lib::expect<void> = 0;
328+
virtual auto connect(
329+
lib::maybe_uspan<const std::byte> addr, bool nonblock
330+
) -> lib::expect<void> = 0;
329331
virtual auto listen(int backlog) -> lib::expect<void> = 0;
330332
virtual auto accept(
331333
lib::maybe_uspan<std::byte> peer_addr_out,

kernel/source/system/syscall/vfs/socket.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace syscall::vfs
2424
return inode->stat.type() == stat::s_ifsock;
2525
}
2626

27-
auto get_socket(sched::process_t *proc, int sockfd, int *flags = nullptr)
27+
auto get_socket(sched::process_t *proc, int sockfd, bool *nonblock = nullptr)
2828
-> lib::expect<std::shared_ptr<socket::socket_t>>
2929
{
3030
const auto fdesc_res = detail::get_fd(proc, sockfd);
@@ -35,8 +35,8 @@ namespace syscall::vfs
3535
if (!is_socket(fdesc))
3636
return std::unexpected { lib::err::not_a_socket };
3737

38-
if (flags)
39-
*flags = fdesc->file->flags;
38+
if (nonblock)
39+
*nonblock = (fdesc->file->flags & o_nonblock) != 0;
4040

4141
return socket::from_file(*fdesc->file);
4242
}
@@ -52,14 +52,6 @@ namespace syscall::vfs
5252
return ret;
5353
}
5454

55-
int effective_flags(int fflags)
56-
{
57-
int ret = 0;
58-
if (fflags & o_nonblock)
59-
ret |= msg_dontwait;
60-
return ret;
61-
}
62-
6355
std::optional<frg::small_vector<
6456
lib::maybe_uspan<std::byte>, 8,
6557
frg::allocator<lib::maybe_uspan<std::byte>>
@@ -121,7 +113,8 @@ namespace syscall::vfs
121113

122114
const auto proc = sched::current_process();
123115

124-
auto sockres = get_socket(proc, sockfd);
116+
bool nonblock = false;
117+
auto sockres = get_socket(proc, sockfd, &nonblock);
125118
if (!sockres)
126119
return -lib::map_error(sockres.error());
127120
auto sock = std::move(*sockres);
@@ -130,7 +123,7 @@ namespace syscall::vfs
130123
if (!uspan)
131124
return -EFAULT;
132125

133-
if (const auto res = sock->connect(*uspan); !res)
126+
if (const auto res = sock->connect(*uspan, nonblock); !res)
134127
return -lib::map_error(res.error());
135128
return 0;
136129
}
@@ -147,11 +140,13 @@ namespace syscall::vfs
147140
{
148141
const auto proc = sched::current_process();
149142

150-
int fflags;
151-
auto sockres = get_socket(proc, sockfd, &fflags);
143+
bool nonblock = false;
144+
auto sockres = get_socket(proc, sockfd, &nonblock);
152145
if (!sockres)
153146
return -lib::map_error(sockres.error());
154147
auto sock = std::move(*sockres);
148+
if (nonblock)
149+
flags |= msg_dontwait;
155150

156151
auto bufuspan = lib::maybe_uspan<std::byte>::create(buf, len);
157152
if (!bufuspan)
@@ -177,7 +172,7 @@ namespace syscall::vfs
177172
.out_flags = 0
178173
};
179174

180-
const auto res = sock->sendmsg(hdr, flags | effective_flags(fflags));
175+
const auto res = sock->sendmsg(hdr, flags);
181176
if (!res)
182177
return -lib::map_error(res.error());
183178
return *res;
@@ -190,11 +185,13 @@ namespace syscall::vfs
190185
{
191186
const auto proc = sched::current_process();
192187

193-
int fflags;
194-
auto sockres = get_socket(proc, sockfd, &fflags);
188+
bool nonblock = false;
189+
auto sockres = get_socket(proc, sockfd, &nonblock);
195190
if (!sockres)
196191
return -lib::map_error(sockres.error());
197192
auto sock = std::move(*sockres);
193+
if (nonblock)
194+
flags |= msg_dontwait;
198195

199196
auto bufuspan = lib::maybe_uspan<std::byte>::create(buf, len);
200197
if (!bufuspan)
@@ -224,7 +221,7 @@ namespace syscall::vfs
224221
.out_flags = 0
225222
};
226223

227-
const auto res = sock->recvmsg(hdr, flags | effective_flags(fflags));
224+
const auto res = sock->recvmsg(hdr, flags);
228225
if (!res)
229226
return -lib::map_error(res.error());
230227

@@ -237,11 +234,13 @@ namespace syscall::vfs
237234
{
238235
const auto proc = sched::current_process();
239236

240-
int fflags;
241-
auto sockres = get_socket(proc, sockfd, &fflags);
237+
bool nonblock = false;
238+
auto sockres = get_socket(proc, sockfd, &nonblock);
242239
if (!sockres)
243240
return -lib::map_error(sockres.error());
244241
auto sock = std::move(*sockres);
242+
if (nonblock)
243+
flags |= msg_dontwait;
245244

246245
msghdr kmsg;
247246
if (!lib::copy_from_user(&kmsg, msg, sizeof(msghdr)))
@@ -282,7 +281,7 @@ namespace syscall::vfs
282281
.out_flags = 0
283282
};
284283

285-
const auto res = sock->sendmsg(hdr, flags | effective_flags(fflags));
284+
const auto res = sock->sendmsg(hdr, flags);
286285
if (!res)
287286
return -lib::map_error(res.error());
288287
return *res;
@@ -292,11 +291,13 @@ namespace syscall::vfs
292291
{
293292
const auto proc = sched::current_process();
294293

295-
int fflags;
296-
auto sockres = get_socket(proc, sockfd, &fflags);
294+
bool nonblock = false;
295+
auto sockres = get_socket(proc, sockfd, &nonblock);
297296
if (!sockres)
298297
return -lib::map_error(sockres.error());
299298
auto sock = std::move(*sockres);
299+
if (nonblock)
300+
flags |= msg_dontwait;
300301

301302
msghdr kmsg;
302303
if (!lib::copy_from_user(&kmsg, msg, sizeof(msghdr)))
@@ -337,7 +338,7 @@ namespace syscall::vfs
337338
.out_flags = 0
338339
};
339340

340-
const auto res = sock->recvmsg(hdr, flags | effective_flags(fflags));
341+
const auto res = sock->recvmsg(hdr, flags);
341342
if (!res)
342343
return -lib::map_error(res.error());
343344

@@ -400,7 +401,6 @@ namespace syscall::vfs
400401
return -lib::map_error(sockres.error());
401402
auto sock = std::move(*sockres);
402403

403-
backlog = std::clamp(backlog, 0, somaxconn);
404404
if (const auto res = sock->listen(backlog); !res)
405405
return -lib::map_error(res.error());
406406
return 0;
@@ -484,11 +484,11 @@ namespace syscall::vfs
484484
if (!pres)
485485
return -lib::map_error(pres.error());
486486

487-
auto res1 = socket::create_anon(std::move(pres->first), flags);
487+
auto res1 = socket::create_anon(std::move(pres->first), map_flags(flags));
488488
if (!res1)
489489
return -lib::map_error(res1.error());
490490

491-
auto res2 = socket::create_anon(std::move(pres->second), flags);
491+
auto res2 = socket::create_anon(std::move(pres->second), map_flags(flags));
492492
if (!res2)
493493
{
494494
proc->fdt->close(*res1);
@@ -555,8 +555,8 @@ namespace syscall::vfs
555555

556556
const auto proc = sched::current_process();
557557

558-
int fflags;
559-
auto sockres = get_socket(proc, sockfd, &fflags);
558+
bool nonblock = false;
559+
auto sockres = get_socket(proc, sockfd, &nonblock);
560560
if (!sockres)
561561
return -lib::map_error(sockres.error());
562562
auto sock = std::move(*sockres);
@@ -576,7 +576,7 @@ namespace syscall::vfs
576576
}
577577

578578
socklen_t out_len = in_len;
579-
auto ares = sock->accept(uspan, &out_len, fflags & o_nonblock);
579+
auto ares = sock->accept(uspan, &out_len, nonblock);
580580
if (!ares)
581581
return -lib::map_error(ares.error());
582582

0 commit comments

Comments
 (0)