Commit c2471014 authored by Kirill Smelkov's avatar Kirill Smelkov

internal/syscall: New package

This package provides wrappers to OS system calls.
Minimal functionality that will be used to implement os and os/signal pacakges.

/reviewed-on nexedi/pygolang!17
parent 3a131a51
...@@ -6,6 +6,8 @@ include golang/pyx/runtime.h ...@@ -6,6 +6,8 @@ include golang/pyx/runtime.h
include golang/pyx/runtime.cpp include golang/pyx/runtime.cpp
include golang/pyx/testprog/golang_dso_user/dsouser/dso.cpp include golang/pyx/testprog/golang_dso_user/dsouser/dso.cpp
include golang/runtime/internal.h include golang/runtime/internal.h
include golang/runtime/internal/syscall.h
include golang/runtime/internal/syscall.cpp
include golang/context.h include golang/context.h
include golang/context.cpp include golang/context.cpp
include golang/cxx.h include golang/cxx.h
......
...@@ -174,6 +174,7 @@ def _with_build_defaults(kw): # -> (pygo, kw') ...@@ -174,6 +174,7 @@ def _with_build_defaults(kw): # -> (pygo, kw')
dependv.extend(['%s/golang/%s' % (pygo, _) for _ in [ dependv.extend(['%s/golang/%s' % (pygo, _) for _ in [
'libgolang.h', 'libgolang.h',
'runtime/internal.h', 'runtime/internal.h',
'runtime/internal/syscall.h',
'context.h', 'context.h',
'cxx.h', 'cxx.h',
'errors.h', 'errors.h',
...@@ -207,6 +208,8 @@ def Extension(name, sources, **kw): ...@@ -207,6 +208,8 @@ def Extension(name, sources, **kw):
dependv.extend(['%s/golang/%s' % (pygo, _) for _ in [ dependv.extend(['%s/golang/%s' % (pygo, _) for _ in [
'_golang.pxd', '_golang.pxd',
'runtime/_libgolang.pxd', 'runtime/_libgolang.pxd',
'runtime/internal/__init__.pxd',
'runtime/internal/syscall.pxd',
'__init__.pxd', '__init__.pxd',
'context.pxd', 'context.pxd',
'_context.pxd', '_context.pxd',
......
// Copyright (C) 2021-2022 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
#include "golang/runtime/internal/syscall.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <string>
// golang::internal::syscall::
namespace golang {
namespace internal {
namespace syscall {
// ---- Errno ----
_Errno::_Errno() {}
_Errno::~_Errno() {}
void _Errno::incref() {
object::incref();
}
void _Errno::decref() {
if (__decref())
delete this;
}
error NewErrno(__Errno syserr) {
_Errno* _e = new _Errno();
_e->syserr = syserr;
return adoptref(static_cast<_error*>(_e));
}
string _Errno::Error() {
_Errno& e = *this;
char ebuf[128];
#if __APPLE__
int x = ::strerror_r(-e.syserr, ebuf, sizeof(ebuf));
if (x == 0)
return string(ebuf);
return "unknown error " + std::to_string(-e.syserr);
#else
char *estr = ::strerror_r(-e.syserr, ebuf, sizeof(ebuf));
return string(estr);
#endif
}
// ---- syscalls ----
// TODO better call syscalls directly, instead of calling libc wrappers and saving/restoring errno
int Read(int fd, void *buf, size_t count) {
int save_errno = errno;
int n = ::read(fd, buf, count);
if (n < 0)
n = -errno;
errno = save_errno;
return n;
}
int Write(int fd, const void *buf, size_t count) {
int save_errno = errno;
int n = ::write(fd, buf, count);
if (n < 0)
n = -errno;
errno = save_errno;
return n;
}
__Errno Close(int fd) {
int save_errno = errno;
int err = ::close(fd);
if (err < 0)
err = -errno;
errno = save_errno;
return err;
}
__Errno Fcntl(int fd, int cmd, int arg) {
int save_errno = errno;
int err = ::fcntl(fd, cmd, arg);
if (err < 0)
err = -errno;
errno = save_errno;
return err;
}
__Errno Fstat(int fd, struct ::stat *out_st) {
int save_errno = errno;
int err = ::fstat(fd, out_st);
if (err < 0)
err = -errno;
errno = save_errno;
return err;
}
int Open(const char *path, int flags, mode_t mode) {
int save_errno = errno;
int fd = ::open(path, flags, mode);
if (fd < 0)
fd = -errno;
errno = save_errno;
return fd;
}
__Errno Pipe(int vfd[2]) {
int save_errno = errno;
int err = ::pipe(vfd);
if (err == -1)
err = -errno;
errno = save_errno;
return err;
}
__Errno Sigaction(int signo, const struct ::sigaction *act, struct ::sigaction *oldact) {
int save_errno = errno;
int err = ::sigaction(signo, act, oldact);
if (err < 0)
err = -errno;
errno = save_errno;
return err;
}
}}} // golang::internal::syscall::
#ifndef _NXD_LIBGOLANG_RUNTIME_INTERNAL_SYSCALL_H
#define _NXD_LIBGOLANG_RUNTIME_INTERNAL_SYSCALL_H
// Copyright (C) 2021-2022 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
// Package syscall provides low-level interface to OS.
#include "golang/libgolang.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
// golang::internal::syscall::
namespace golang {
namespace internal {
namespace syscall {
// errors returned by system calls are represented as negative error codes, for example -ENOENT.
// Those error codes could be converted to error via NewErrno.
typedef int __Errno;
struct _Errno final : _error, object {
__Errno syserr;
private:
_Errno();
~_Errno();
friend error NewErrno(__Errno syserr);
public:
void incref();
void decref();
public:
// Error returns string corresponding to system error syserr.
string Error();
};
typedef refptr<_Errno> Errno;
error NewErrno(__Errno syserr); // TODO better return Errno directly.
// system calls
int/*n|err*/ Read(int fd, void *buf, size_t count);
int/*n|err*/ Write(int fd, const void *buf, size_t count);
__Errno Close(int fd);
__Errno Fcntl(int fd, int cmd, int arg);
__Errno Fstat(int fd, struct ::stat *out_st);
int/*fd|err*/ Open(const char *path, int flags, mode_t mode);
__Errno Pipe(int vfd[2]);
__Errno Sigaction(int signo, const struct ::sigaction *act, struct ::sigaction *oldact);
}}} // golang::internal::syscall::
#endif // _NXD_LIBGOLANG_RUNTIME_INTERNAL_SYSCALL_H
# cython: language_level=2
# Copyright (C) 2021-2022 Nexedi SA and Contributors.
# Kirill Smelkov <kirr@nexedi.com>
#
# This program is free software: you can Use, Study, Modify and Redistribute
# it under the terms of the GNU General Public License version 3, or (at your
# option) any later version, as published by the Free Software Foundation.
#
# You can also Link and Combine this program with other software covered by
# the terms of any of the Free Software licenses or any of the Open Source
# Initiative approved licenses and Convey the resulting work. Corresponding
# source of such a combination shall include the source code for all other
# software used.
#
# This program is distributed WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See COPYING file for full licensing terms.
# See https://www.nexedi.com/licensing for rationale and options.
from posix.fcntl cimport mode_t
from posix.stat cimport struct_stat
cdef extern from "golang/runtime/internal/syscall.h" namespace "golang::internal::syscall" nogil:
int Close(int fd)
int Fcntl(int fd, int cmd, int arg)
int Fstat(int fd, struct_stat *out_st)
int Open(const char *path, int flags, mode_t mode)
int Read(int fd, void *buf, size_t count)
int Write(int fd, const void *buf, size_t count)
...@@ -197,6 +197,7 @@ setup( ...@@ -197,6 +197,7 @@ setup(
x_dsos = [DSO('golang.runtime.libgolang', x_dsos = [DSO('golang.runtime.libgolang',
['golang/runtime/libgolang.cpp', ['golang/runtime/libgolang.cpp',
'golang/runtime/internal/syscall.cpp',
'golang/context.cpp', 'golang/context.cpp',
'golang/errors.cpp', 'golang/errors.cpp',
'golang/fmt.cpp', 'golang/fmt.cpp',
...@@ -207,6 +208,7 @@ setup( ...@@ -207,6 +208,7 @@ setup(
depends = [ depends = [
'golang/libgolang.h', 'golang/libgolang.h',
'golang/runtime/internal.h', 'golang/runtime/internal.h',
'golang/runtime/internal/syscall.h',
'golang/context.h', 'golang/context.h',
'golang/cxx.h', 'golang/cxx.h',
'golang/errors.h', 'golang/errors.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment