~ubuntu-branches/ubuntu/utopic/ejabberd/utopic

« back to all changes in this revision

Viewing changes to src/extauth.erl

  • Committer: Package Import Robot
  • Author(s): Gerfried Fuchs, Konstantin Khomoutov, Gerfried Fuchs
  • Date: 2011-09-11 16:16:55 UTC
  • mfrom: (1.1.18 upstream)
  • Revision ID: package-import@ubuntu.com-20110911161655-rz3jall453jo3zek
Tags: 2.1.8-1
[ Konstantin Khomoutov ]
* New upstream release.
* Remove patch fixing DSA-2248-1 (CVE-2011-1753)
  as the fix is now integrated upstream.
* Drop patches from OLPC project implementing @recent@ and @online@
  shared roster groups; support for @online@ is now intergated upstream.
* Add patch fixing version string where applicable (EJAB-1484).
* Bump standards version to 3.9.2

[ Gerfried Fuchs ]
* Add recommended targets build-arch and build-indep to debian/rules.
* Fix spelling error in manpage noticed by lintian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
%%% Created : 30 Jul 2004 by Leif Johansson <leifj@it.su.se>
6
6
%%%
7
7
%%%
8
 
%%% ejabberd, Copyright (C) 2002-2010   ProcessOne
 
8
%%% ejabberd, Copyright (C) 2002-2011   ProcessOne
9
9
%%%
10
10
%%% This program is free software; you can redistribute it and/or
11
11
%%% modify it under the terms of the GNU General Public License as
45
45
start(Host, ExtPrg) ->
46
46
    lists:foreach(
47
47
        fun(This) ->
48
 
            spawn(?MODULE, init, [get_process_name(Host, This), ExtPrg])
 
48
            start_instance(get_process_name(Host, This), ExtPrg)
49
49
        end,
50
50
        lists:seq(0, get_instances(Host)-1)
51
51
    ).
52
52
 
 
53
start_instance(ProcessName, ExtPrg) ->
 
54
    spawn(?MODULE, init, [ProcessName, ExtPrg]).
 
55
 
 
56
restart_instance(ProcessName, ExtPrg) ->
 
57
    unregister(ProcessName),
 
58
    start_instance(ProcessName, ExtPrg).
 
59
 
53
60
init(ProcessName, ExtPrg) ->
54
61
    register(ProcessName, self()),
55
62
    process_flag(trap_exit,true),
56
63
    Port = open_port({spawn, ExtPrg}, [{packet,2}]),
57
 
    loop(Port, ?INIT_TIMEOUT).
 
64
    loop(Port, ?INIT_TIMEOUT, ProcessName, ExtPrg).
58
65
 
59
66
stop(Host) ->
60
67
    lists:foreach(
108
115
        _ -> 1
109
116
    end.
110
117
 
111
 
loop(Port, Timeout) ->
 
118
loop(Port, Timeout, ProcessName, ExtPrg) ->
112
119
    receive
113
120
        {call, Caller, Msg} ->
114
 
            Port ! {self(), {command, encode(Msg)}},
 
121
            port_command(Port, encode(Msg)),
115
122
            receive
116
123
                {Port, {data, Data}} ->
117
124
                    ?DEBUG("extauth call '~p' received data response:~n~p", [Msg, Data]),
118
 
                    Caller ! {eauth, decode(Data)};
 
125
                    Caller ! {eauth, decode(Data)},
 
126
                    loop(Port, ?CALL_TIMEOUT, ProcessName, ExtPrg);
119
127
                {Port, Other} ->
120
128
                    ?ERROR_MSG("extauth call '~p' received strange response:~n~p", [Msg, Other]),
121
 
                    Caller ! {eauth, false}
 
129
                    Caller ! {eauth, false},
 
130
                    loop(Port, ?CALL_TIMEOUT, ProcessName, ExtPrg)
122
131
            after
123
132
                Timeout ->
124
133
                    ?ERROR_MSG("extauth call '~p' didn't receive response", [Msg]),
125
 
                    Caller ! {eauth, false}
126
 
            end,
127
 
            loop(Port, ?CALL_TIMEOUT);
 
134
                    Caller ! {eauth, false},
 
135
                    Pid = restart_instance(ProcessName, ExtPrg),
 
136
                    flush_buffer_and_forward_messages(Pid),
 
137
                    exit(port_terminated)
 
138
            end;
128
139
        stop ->
129
140
            Port ! {self(), close},
130
141
            receive
132
143
                    exit(normal)
133
144
            end;
134
145
        {'EXIT', Port, Reason} ->
135
 
            ?CRITICAL_MSG("~p ~n", [Reason]),
 
146
            ?CRITICAL_MSG("extauth script has exitted abruptly with reason '~p'", [Reason]),
 
147
            Pid = restart_instance(ProcessName, ExtPrg),
 
148
            flush_buffer_and_forward_messages(Pid),
136
149
            exit(port_terminated)
137
150
    end.
138
151
 
 
152
flush_buffer_and_forward_messages(Pid) ->
 
153
    receive
 
154
        Message ->
 
155
            Pid ! Message,
 
156
            flush_buffer_and_forward_messages(Pid)
 
157
    after 0 ->
 
158
            true
 
159
    end.
 
160
 
139
161
join(List, Sep) ->
140
162
    lists:foldl(fun(A, "") -> A;
141
163
                   (A, Acc) -> Acc ++ Sep ++ A