[erlang-questions] where is os:getuid() ?

zxq9 zxq9@REDACTED
Tue Nov 24 09:26:19 CET 2015

On Tuesday 24 November 2015 08:52:36 Daniel Abrahamsson wrote:
> Rather than to try to find a generic interface for something that is
> inherently OS specific, perhaps it would be better to create a "posix"
> module that exports some common posix operations. That way it is obvious to
> whoever use it that you can't expect it to work on non-posix OSes.

That is certainly one possibility. Which feels more inconvenient?

do_some_os_thing() ->

perform_os_action({unix, _}) ->
    {ok, NodeUid} = posix:getuid(),
    do_action(posix, NodeUid);
perform_os_action({win, _}) ->
    {ok, NodeUid} = windows:getuid(),
    do_action(win, NodeUid);
% ...

do_action(posix, NodeUid) ->
   % stuff...;
do_action(win, NodeUid) ->
   % slightly different stuff...;
% ...


do_some_os_thing() ->

perform_os_action({{unix, _}, NodeUid}) ->
    % stuff... ;
perform_os_action({{win, _}, NodeUid}) ->
    % slightly different stuff...;
% ...


do_some_os_thing() ->
    % Inventing an os:get_feature_module/1 here...
    OSType = os:type(),
    OSMod = os:get_feature_module(OSType)
    {ok, NodeUid} = OSMod:getuid(),
    perform_os_action(OSType, NodeUid)

%% etc...

I can't really see a clear balance of pros and cons either way -- the
difference will *probably* have to be tackled head-on in the related code
eventually, at least if that code is intended to work across platforms.
I'm not sure that an OS-specific module will do much to make the calling
code less annoying to write -- though it may make building ERTS against a
given platform easier (you only need the module relevant to the target
platform -- but I think this has to happen in any case underneath?). What
I do at the moment is match on the result of os:type/0, and execute an
os-specific script or command (shell, batchfile, some natively built code,

As you say, one real benefit of making a 'posix', 'windows', etc. modules
would be that determining exactly what features are supported on a given
platform becomes as simple as checking the docs for that platform's module.
That's better than having a generic interface that only works the same way
on most but not all platforms. (I *really* hate hunting through docs for
caveats like "super_important_function/3 only works on platform X; does
nothing and returns 'ok' on Y"). On the other hand, checking for API
compatibility would then require looking through each platform module to
make sure you didn't miss anything. :-/

Any thoughts? I write a fair amount of cross-platform code in Erlang these
days. This sort of thing is so annoying that I will do cheetah flips to
avoid the weird places where os differences start mattering (if the need is
non-trivial I'll write entirely separate native programs I can call from
Erlang). Fortunately cases where this can't be avoided are rare.


More information about the erlang-questions mailing list