Skip to content

Commit

Permalink
Added OS.getPID, .uName and .getHostByName.
Browse files Browse the repository at this point in the history
Note: OS.uName currently is not supported in Windows.
  • Loading branch information
kennytm committed Jun 24, 2013
1 parent ab83279 commit c54016b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*~
*.swp
*.swo
*.o
*.a

9 changes: 9 additions & 0 deletions vm/boostenv/lib/OS.oz
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ export
tcpConnectionShutdown: TCPConnectionShutdown
tcpConnectionClose: TCPConnectionClose

GetHostByName
UName

% Process management
SpawnProcess
SpawnProcessAndPipe
Expand All @@ -81,6 +84,8 @@ export
PipeConnectionShutdown
PipeConnectionClose

GetPID

% Compatibility
open: CompatOpen
fileDesc: CompatFileDesc
Expand Down Expand Up @@ -189,6 +194,9 @@ define
TCPConnectionShutdown = Boot_OS.tcpConnectionShutdown
TCPConnectionClose = Boot_OS.tcpConnectionClose

GetHostByName = Boot_OS.getHostByName
UName = Boot_OS.uName

%% Process management

SpawnProcess = Boot_OS.exec
Expand All @@ -208,6 +216,7 @@ define

PipeConnectionShutdown = Boot_OS.pipeConnectionShutdown
PipeConnectionClose = Boot_OS.pipeConnectionClose
GetPID = Boot_OS.getPID

%% POSIX-like file descriptor management

Expand Down
82 changes: 82 additions & 0 deletions vm/boostenv/main/modos.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
# include <unistd.h>
# include <sys/time.h>
# include <sys/resource.h>
# include <sys/utsname.h>
#endif

#ifndef MOZART_GENERATOR
Expand Down Expand Up @@ -1180,6 +1181,87 @@ public:
}
};
#endif // BOOST_ASIO_HAS_LOCAL_SOCKETS

class GetPID: public Builtin<GetPID> {
public:
GetPID(): Builtin("getPID") {}

static void call(VM vm, Out pid) {
#ifdef MOZART_WINDOWS
pid = build(vm, GetCurrentProcessId());
#else
pid = build(vm, getpid());
#endif
}
};

class GetHostByName: public Builtin<GetHostByName> {
public:
GetHostByName(): Builtin("getHostByName") {}

static void call(VM vm, In name, Out res) {
// gethostbyname(3) is deprecated. Emulate getaddrinfo(3) instead?
typedef boost::asio::ip::tcp tcp;

size_t nameBufLength = ozVSLengthForBuffer(vm, name);
std::string nameString;
ozVSGet(vm, name, nameBufLength, nameString);

auto& environment = BoostBasedVM::forVM(vm);
tcp::resolver resolver (environment.io_service);
tcp::resolver::query query (nameString, "0");
boost::system::error_code ec;
auto it = resolver.resolve(query, ec);
if (ec) {
raiseOSError(vm, "getHostByName", ec);
}

OzListBuilder addrListBuilder (vm);

decltype(it) end;
while (it != end) {
auto addr = it->endpoint().address().to_string();
auto addrString = String::build(vm, newLString(vm, addr));
addrListBuilder.push_back(vm, addrString);
++ it;
}

auto arity = buildArity(vm, "hostent", "addrList", "aliases", "name");
res = buildRecord(vm, std::move(arity), addrListBuilder.get(vm),
vm->coreatoms.nil,
name);
}
};

class UName: public Builtin<UName> {
static UnstableNode buildString(VM vm, const char* value) {
return String::build(vm, newLString(vm, value));
}

public:
UName(): Builtin("uName") {}

static void call(VM vm, Out res) {
#ifdef MOZART_WINDOWS
// TODO: Implement uname(2) on Windows. Perhaps we can take some ideas
// from http://hg.python.org/cpython/file/3.3/Lib/platform.py#l1043.
raiseError(vm, "notImplemented", "OS.uName on Windows");
#else
struct utsname result;
if (uname(&result) != 0) {
raiseLastOSError(vm, "uname");
}

auto arity = buildArity(vm, "utsname", "machine", "nodename", "release",
"sysname", "version");
res = buildRecord(vm, std::move(arity), buildString(vm, result.machine),
buildString(vm, result.nodename),
buildString(vm, result.release),
buildString(vm, result.sysname),
buildString(vm, result.version));
#endif
}
};
};

}
Expand Down

0 comments on commit c54016b

Please sign in to comment.