Thursday, 31 March 2016

Taming Configuration Files - general term processing

Overview

This instalment allows the matching of general Erlang terms including recursive structures such as filename and iolist.  Several weeks ago I did some work on matching Erlang terms.  This unpublished work has been extended.  

This extension involved the storing of parser state, and the change of the validation rules from two parameters to three parameters.  Code was broken in this process.  This state could have been stored in the process dictionary with a lot less rework, and perhaps where time is money, such as in a commercial environment it would have been done that way.  In this rework the existing tests became invaluable in ensuring that there was no loss of quality in the process.

Specifications

As per the earlier work, the intention is to check that an Erlang term conforms to a definition which is also an Erlang term.  This comparison is performed by term_defs:validate/2, which takes two parameters, the term specification, and the term respectively.  If the two do not match an error is thrown, listing both the rule and the data.

Syntactic Conventions

{} - denotes a tuple structure as per normal Erlang
Captialised - denotes a syntactic construction.  The details of which are covered in this article.
[] - denotes a list as per normal Erlang

Matching Atomic Values

The test for a given value is done by the {value,Value} term where the corresponding term in the expression to be tested must match Value.  Where a type test is required, the {builtin,TypeTest} test is used.  TypeTest is any unary function exported from the Erlang module that returns boolean.

Matching from a list of possible values

{options,[Item_Spec]}

In a options construction, one of the Item_Spec's in the list must match.  For example: {options,[{value,male},{value,female}]} indicates that the valid contents are one of the set male or female.

Matching a list

{list,TypeTest}

All items within the list must pass the type test.  For example to test that all the items in the list are integers use {list,{builtin,is_integer}}.

Defining a pattern

{define, Key, Pattern}

This associates a Pattern with a Key.  For example {define int {builtin is_integer}} associates the atom int with the check for an integer.  This symbolic referencing allows recursive data patterns to be defined.  Defining always returns true.

Matching all items in a list of tests

{match_all,[TypeTest]}

Although this could be used with several filters, this functionality is not usable because of the lack of user functions in this specification.  This is currently used to specify macros for recursive type tests using define.

Matching a tuple structure

{tuple, [Test]}

The number of tests in the test list must match the number of positions in the tuple, and each test must be passed for the tuple to be valid.  For example {tuple,[{value,employee},{list,{builtin,is_integer}]} will match {employee,"Joe Smith"}.

Matching a property list

{property_list, [ KeyValuePairTest ]}

The KeyValuePairTest is a tuple containing an optionality flag  which satisfies the rule {options,[{value,opt},{value,reqd}]}, a KeyName (which is an atom), and a data specification.  For every key with the reqd keyword, that item must exist in the data, and its associated value must be valid.  For every key with the opt keyword, if it exists then its associated value must be valid.  For example, a property list must contain gender, and the value must be either male or female.

{property_list [{reqd,gender,{options[{value,male},{value,female}]}]}

Although the property list construct could have been defined from other rules in this specification, it is a commonly used structure and was therefore given its own abstraction.

A complex example

{match_all,[
     {define,iolistmember,{options,[{builtin,is_integer},iolist]}},
     {define,iolist,{options,[{builtin,is_binary},{list,iolistmember}]}},
     iolist]}

This example tests that data is an iolist.  A valid iolist might be  [<<"This is a valid ">>,"iolist"].

An iolist is either a binary of a list of integers and io lists.  This example shows how recursive structures can be defined using a combination of match_all and define.  Notice how it is the last item in the match_all that causes the test to fire, and this item references back to the earlier defines.


The code


-module(term_defs).

-export ([validate/2,test/0]).

-author('Tony Wallace').
-purpose(  <<"Confirm that an erlang term matches a specification.",
  "The atom datadef matches any data definition",
  "Type testing is done by matching the erlang term to {builtin,functionanme}",
  "functionname is a unary function exported from the erlang module, for example"
  "{builtin,is_integer} will check that the matched term is an integer./n",
  "Where an erlang term must match a given term the {value,Term} pattern is used.",
  "The {value,Term} construction is valuable where there is a list of valid options",
           ", for example {options,[{value,option1},{value,option2}]}.  In this case the",
  "term can match either option1 or option 2./n",
  "A list is defined by the construction {list,DataDef}, where each item of the",
  "list must conform the the specification in DataDef.  For example a list of"
  "integers is defined as {list,{builtin,is_integer}}.\n",
  "Tuples are matched to {tuple,[DataDef]}.  Each term contained within the tuple",
  "must match its associated DataDef./n",
  "Property lists are given special treatment.  A property list is defined its contents",
  ".  The specification is:/n ",
  "     {property_list,[ ",
  "         {tuple, ",
  "             {options,{value,opt},{value,reqd}}, ",
  "             Key,Specification}]} ",
    " opt - this key is optional, reqd this key is required",
    "Key - is an erlang term, normally an atom\n ",
    "Specification a datadef that that key value must satisfy.">>).

validate(Def,Term) ->
    {R,_}=validate(Def,Term,dict:new()),
    R.
validate(Def,Term,State) ->
    %io:format("validate(~p,~p,~p)~n~n",[Def,Term,State]),
    case maybe_validate(Def,Term,State) of
{true,NewState} ->
   {true,NewState};
{false,_} ->
   not_valid(Def,Term)
    end.
%maybe_validate([Def],Term,State) ->
%    validate(Def,Term,State);
maybe_validate({define,Key,Value},_Term,State)  ->
    NewState = dict:store(Key,Value,State),
    {true,NewState};
maybe_validate(any,_,S) ->
    {true,S};
maybe_validate({property_list,KeyDefs},PL,State) ->
    Checked = [pl_entry(KeyDef,PL,State) || KeyDef <- KeyDefs],
    R=lists:foldl(fun(true,A) -> A;(_,_)->false end, true, Checked),
    {R,State};
maybe_validate({tuple,DefList},Term,S) 
  when is_tuple(Term) andalso (length(DefList) =:= tuple_size(Term)) ->
    TermList = tuple_to_list(Term),
    tuple_flds(DefList,TermList,S);
maybe_validate({tuple,_},_,S) ->
    %% tuple sizes do not match or Term is not a tuple
    {false,S};
maybe_validate({list,TermDef},Term,State) 
  when is_list(Term)  ->
    Valid=[validate(TermDef,X,State) || X <- Term],
    R=lists:foldl(fun({X,_},A) -> A and X end,true,Valid),
    {R,State};
maybe_validate(TermDef={builtin,Atom},Term,State)   ->
    case catch(apply(erlang,Atom,[Term])) of
{'EXIT',_} ->
   not_valid(TermDef,'undefined');
true -> {true,State};
false -> {false,State};
X -> 
   not_valid(TermDef,{'not_boolean',X})
    end;
maybe_validate({options,TermDef},Term,State) when is_list(TermDef) ->
    choices(TermDef,Term,State);
maybe_validate({match_all,[]},_,State)  ->
    {true,State};
maybe_validate({match_all,[H|T]},Term,State)  ->
    {R1,State1} = validate(H,Term,State),
    case R1 of
true ->
   validate({match_all,T},Term,State1);
false ->
   not_valid(H,Term)
    end;
maybe_validate({value,X},X,State) ->
    {true,State};
maybe_validate(datadef,{value,_},State) ->
    {true,State};
maybe_validate(datadef,{builtin,Fname},State)
  when is_atom(Fname)->
    EE = erlang:module_info(exports),
    case proplists:get_value(Fname,EE) of
1 -> {true,State};
_ -> {false,State}
    end;
maybe_validate(datadef,{list,ItemDef},State) ->
    validate(datadef,ItemDef,State);
maybe_validate(datadef,{property_list,[{Opt,_KeyName,DataDef}|KeyList]},State) ->
    validate([{value,opt},{value,reqd}],Opt,State),
    {true,NewState} = validate(datadef,DataDef,State),
    maybe_validate(property_list,KeyList,NewState);
maybe_validate(datadef,{property_list,[]},S) -> {true,S};
maybe_validate(datadef,{tuple,DefList},State) 
  when is_list(DefList)->
    Validated = [validate(datadef,X,State) || X <- DefList],
    R=lists:foldl(fun({true,_},A) -> A;(_,_) -> false end,true,Validated),
    {R,State};
maybe_validate(Key,Term,State) when is_atom(Key) ->
    case lookup(Key,State) of
undefined -> {false,State};
Pattern -> validate(Pattern,Term,State)
    end;
maybe_validate(_,_,State) ->
    {false,State}.

lookup(Key,Dict) ->
    lookup2(dict:is_key(Key,Dict),Key,Dict).
lookup2(false,_,_) ->
    undefined;
lookup2(true,Key,Dict) ->
    dict:fetch(Key,Dict).

pl_entry({Opt,Key,Def},PL,State) ->
    case proplists:get_value(Key,PL) of
undefined ->
   %% it is valid for an optional key to be undefined
   (Opt =:= opt);
Data ->
   %% if it exists it must be valid
   {true,_}=validate(Def,Data,State),
   true
    end.

tuple_flds([H1|T1],[H2|T2],State) ->
    case validate(H1,H2,State) of
{true,NewState} ->
   tuple_flds(T1,T2,NewState);
{false,_S} ->
   not_valid(H1,H2)
    end;
    

tuple_flds([],[],State) ->
    {true,State}.

choices([H|T],Term,State) ->
    %io:format("choices ([~p|~p],~p,~p)~n",[H,T,Term,State]),
    case maybe_validate(H,Term,State) of
{true,NewState} -> 
   {true,NewState};
{false,NewState} -> 
   choices(T,Term,NewState)
    end;

choices([],_,State) -> 
    {false,State}.


not_valid(Def,Term) ->
    throw({invalid,Def,Term}).
    
   
test() ->
    T=test([
   {datadef,{builtin,is_integer},true},
   {datadef,{value,value},true},
   {datadef,{tuple,[{value,employee}]},true},
   {datadef,{tuple,employee},invalid},
   {datadef,{list,{builtin,is_integer}},true},
   {datadef,{property_list,[]},true},
   {{builtin,is_integer},5,true},
   {{builtin,is_integer},atom,invalid},
   {{options,[{value,option1},{value,option2}]},option2,true},
   {{options,[{value,option1},{value,option2}]},option3,invalid},
   {{tuple,[{value,employee},{builtin,is_list}]},{employee,"Robert"},true},
   {{tuple,[{value,employee},{builtin,is_list}]},{emp,"Robert"},{invalid,{value,employee},emp}},
   {{tuple,[{value,employee},{builtin,is_list}]},{employee,"Robert",male},invalid},
   {{list,{builtin,is_integer}},[3,4,5],true},
   {{list,{builtin,is_integer}},[3,a,5],{invalid,{builtin,is_integer},a}},
   {{property_list,[]},[],true},
   {{property_list,[{opt,gender,{options,[{value,male},{value,female}]}}]},[],true},
   {{property_list,[{opt,gender,{options,[{value,male},{value,female}]}}]},[{gender,male}],true},
   {{property_list,[{opt,gender,{options,[{value,male},{value,female}]}}]},[{gender,transgender}],
    {invalid,{options,[{value,male},{value,female}]},transgender}},
   {{property_list,[{reqd,gender,{options,[{value,male},{value,female}]}}]},[],invalid},
   {{match_all,[{define,int,{builtin,is_integer}},int]},5,true},
   {{match_all,[{define,int,{builtin,is_integer}},int]},'hello',{invalid,{builtin,is_integer},hello}},
   {{match_all,[
     {define,iolistmember,{options,[{builtin,is_integer},iolist]}},
     {define,iolist,{options,[{builtin,is_binary},{list,iolistmember}]}},
     iolist]},
    <<"This is a valid iolist">>,true},
   {{match_all,[
     {define,iolistmember,{options,[{builtin,is_integer},iolist]}},
     {define,iolist,{options,[{builtin,is_binary},{list,iolistmember}]}},
     iolist]},
     [<<"This is a valid ">>,"iolist"],true},
   {{match_all,[
     {define,iolistmember,{options,[{builtin,is_integer},iolist]}},
     {define,iolist,{options,[{builtin,is_binary},{list,iolistmember}]}},
     iolist]},
     'This is not a valid iolist',
      {invalid,{options,[{builtin,is_binary},{list,iolistmember}]},'This is not a valid iolist'}},
   {{match_all,[
     {define,iolistmember,{options,[{builtin,is_integer},iolist]}},
     {define,iolist,{options,[{builtin,is_binary},{list,iolistmember}]}},
     iolist]},
     "This is a valid iolist",true}
 ]),
    Filtered=[{X,Y} || {X,Y} <- T, Y =/= pass],
    io:format("~p~n",[Filtered]).
test(L) when is_list(L) ->
    [test1(H) || H <- L].

test1(T={Def,Arg,_R}) ->
    ExpectedResult = make_result(T),
    case catch(validate(Def,Arg)) of
ExpectedResult -> {T,pass};
Failed -> {T,Failed}
    end.

make_result({_,_,true}) ->
    true;
make_result({Def,Arg,invalid}) ->
    {invalid,Def,Arg};
make_result({_Def,_Arg,Term}) ->
    Term.


Thursday, 17 March 2016

File contexts in Erlang

The problem

I decided to do some testing.  Each test was independent, and some were designed to throw errors.  As these processes throw errors, why not put them in separate processes and trap the exit signals.  Each test can be in its own directory with its own files this makes these tests independent of each other.  Finally now that they are independent run them in parallel and collect the results.

Pretty simple, what could go wrong?  Of course being computing nothing works as planned.  The tests worked individually but running in parallel they failed.  Tests were picking up files intended for the other test in the other directory.  Current directory was shared state, and what the state of that location is is determined by the last process to change it.  Relative file names were mapped against that global working directory.

The solution

The current directory was made part of process state, that is a change directory command that stored a value in the process dictionary.  Local versions of file operations, file:consult, file:write_file and others were written to execute in a local context.  Local io_format versions were written to output to the local directory to aid debugging.  Problem solved.

Thinking generally

This problem is not that different from the global name problem I wrote on earlier.  The context system should be extended with local directory and context sensitive file operations.  It is understandable why the Erlang runtime system is built the way it is, but as this experience shows, we can do better.

Friday, 11 March 2016

Taming Erlang Configuration files

Problem Outline

When building systems we normally have several configuration files.  We have these files for a number of reasons.
  • As part of the system boot.  The system may need to access various resources such as databases and webservers in order to start.  These resources in turn need data to be supplied to them for them to establish their function.
  • To cope with site specific conditions or business rules.
  • To set settings that tend to be static and considered unworthy of (or too sensitive for) a user interface.
Configuration files are an essential part of our delivered systems and are here to stay.

There are several aspects of configuration files that tend to be problematic.  
  • They are interpreted.  While it is true that the must conform to an overall syntax to be readable by the system, their contents are read and interpreted by the system in an adhoc and as needed basis.  There tends not to be the static analysis that would be performed on compiled code.
  • Their effects are dispersed throughout the system making it often difficult to associate the manifestation of the problem with its cause.
What I desire is a static analysis tool, one that says this is an invalid value, or this value is missing, at the time the configuration file is updated, rather than a strange fault of unknown origin occurring some time later.  Certainly there are tool such as XML parsers that can be used for this: putting configuration data in xml and parsing against a schema.  Once validated the data could become available.

Erlang systems tend to be configured with lists of erlang terms loaded in with file:consult.   This is a system understood and familiar to Erlang Programmers.  Besides is it really a good use of time rewriting existing erlang term configurations into XML?  What is needed is a way to assert certain properties of the configuration file.  By adding a definition file to existing Erlang configuration requirements, existing files can be used without modification, and the schema files are transferable between sites and projects.

Requirements

  • Everything is Erlang.  This is an Erlang shop.  Language choice is a strategic decision.
  • Do static analysis of configuration files.  Justification is the above section.
  • Keep it simple.  Rather a limited but useful tool new than a more sophisticated tool that is never delivered or is difficult to use or unreliable.
  • Sensible workflow/tool integration.  Static analysis should be part of deployment.  Tools like make should be able to detect out of date files and act accordingly.
  • Erlang configuration files tend to be property lists, which sometimes recursively embed property lists.  The system must be able to check that an arbitrary nested key exists.

Design

  1. Every configuration file has a schema entry which defines the definition to which the file conforms.  
  2. The definition file is a configuration file with a schema entry also.
  3. After checking a non human readable form of the configuration file is generated and it is this version that may be used by applications.
  4. By checking file timestamps, tools such as make can determine if a configuration file has been changed and needs rechecking.
  5. The Erlang module that provides check and compile facilities exports a "main/1" entrypoint so that is easy to integrate into tools like make using escript.
  6. Recursive processing of property lists to check key existence.
The master configuration file is as follows:

File conf_def.conf:

{schema,"conf_def.conf.etf"}.
{required_keys,[schema,required_keys]}.

As we can see this configuration file defines itself and validates against a compiled version of itself.  This is a chicken and egg situation resolved by the method conf:compile/1, which allows a compile without validation.  As we can see the value of required_keys is a list of keys, in this case the two keys that are required are schema and required, so this file conforms with its own definition.

A key can also be a list.  For example a key could be [webserver,[listen_port]] which says there should be a key in the configuration file called webserver, which has a property list for a value, and in that property list should be an entry with the key listen_port.

Code

-module (conf).
-export([main/1,compile/1,check/1,consult/1,checkcompile/1,test/0]).

main([]) ->
    ok;
main([H|T]) ->
    checkcompile(H),
    main(T).

checkcompile(F) ->
    check(F),
    compile(F).

consult(ConfFile) ->
    must_read(ConfFile++".etf").

compile(ConfFile) ->
    {ok,SrcData} = file:consult(ConfFile),
    BinData = erlang:term_to_binary(SrcData),
    FileName = ConfFile++'.etf',
    ok=file:write_file(FileName,BinData).

check(ConfFile) ->
    {ok,ConfData} = file:consult(ConfFile),
    ok=check_key(schema,ConfData),
    Schema = proplists:get_value(schema,ConfData),
    B=must_read(Schema),
    Spec = binary_to_term(B),
    Errors =
[
do_it(X,ConfData) || X <- Spec
],
    ErrorResults =
lists:filter(fun (X) -> ok =/= X end,Errors),  
    Status = (ErrorResults =:= []),
    case Status of
true ->
   ok;
false ->
   KL1 = [atom_to_list(X) || X <- ErrorResults],
   KL2 = lists:foldl(fun(X,Acc) -> X ++ ", " ++ Acc end,[],KL1),
   [$,,KL3] = KL2,
   Msg = "Errors for "++ConfFile++KL3,
   throw(Msg)
    end.

must_read(File) ->
    {A,B} = file:read_file(File),
    ok=match(A,ok,fun() -> ["Failed to read file ",File," reason ",atom_to_list(B)] end),
    B.
   
match(A,A,_) ->
    ok;
match(_,_,B) ->
    throw(iolist_to_binary(B())).

do_it({schema,_},_) ->
    %% already checked
    ok;
do_it({required_keys,KeyList},Data) ->
    MissingKeys = [check_key(X,Data)  || X <- KeyList],
    ErrorResults =
lists:filter(fun (X) -> ok =/= X end,MissingKeys),  
    Status = (ErrorResults =:= []),
    case Status of
true ->
   ok;
false ->
   KL1 = [atom_to_list(X) || X <- MissingKeys],
   KL2 = lists:foldl(fun(X,Acc) -> X ++ ", " ++ Acc end,[],KL1),
   [$,,KL3] = KL2,
   io_lib:format("~nMissing keys:~s~n",
[KL3])
    end.

check_key(Key,Data) when is_atom(Key) ->
    case proplists:is_defined(Key,Data) of
true ->
   ok;
false ->
   Key
     end;

check_key([],_Data) ->
    ok;
check_key(_L=[H|T],Data) ->
    %io:format("Checking key ~p in ~p~n",[_L,Data]),
    case check_key(H,Data) of
ok ->
   SubData = proplists:get_value(H,Data),
   check_key(T,SubData);
K -> K
    end.

test() ->
    WsSchemaString=
"{schema,\"conf_def.conf.etf\"}." ++ [$\r] ++
"{required_keys,[schema,webserver,[webserver,port]]}.",
    ok = file:write_file("webserver_schema.conf",WsSchemaString),    
    WsConfig = 
"{schema, \"webserver_schema.conf.etf\"}." ++ [$\r] ++
"{webserver, [{port,9600}]}.",
    ok = file:write_file("webserver.conf",WsConfig),  
    main(["webserver_schema.conf","webserver.conf"]).
    
   

Thursday, 3 March 2016

Organized "christianity" a work of the devil? John 10



In John chapter 9 we see the healing of the man born blind and the tension between Jesus and the religious elite climb a notch. Here we see Jesus take direct aim at the Pharisees and tell them what he thinks of them.

John 10:1 starts with:


"Very truly I tell you Pharisees"


He does not talk behind their back. He confronts them directly. Jesus is not a gossip.


"anyone who does not enter the sheep pen by the gate but climbs in some other way is a thief and a robber. The gatekeeper opens the gate for him, and the sheep listen to his voice. He calls his own sheep by name and leads them out. 4 When he has brought out all his own, he goes on ahead of them, and his sheep follow him because they know his voice. 5 But they will never follow a stranger; in fact, they will run away from him because they do not recognize a stranger’s voice.” 6 Jesus used this figure of speech, but the Pharisees did not understand what he was telling them."


At this point, if I were a Pharisee I would be feeling pretty good. I have been through the apprenticeship, and studied. I have done the hard yards and entered my profession correctly. The gatekeepers, the religious authorities have accepted me as one of there own. People listen to me, give me respect and honour. The good people, the sheep that is. As for the rabble they are not God's sheep so it is okay that they are not listening to me. Let me smugly relax, even my enemies compliment me!

This is a clever strategy. Get your audience onside before you hit them. Nathan does the same when confronting David.

Jesus has not finished with them yet however:


7 Therefore Jesus said again, “Very truly I tell you, I am the gate for the sheep. 8 All who have come before me are thieves and robbers, but the sheep have not listened to them. 9 I am the gate; whoever enters through me will be saved.[a] They will come in and go out, and find pasture. 10 The thief comes only to steal and kill and destroy; I have come that they may have life, and have it to the full.

The bit that hurts "all who come before me are thieves and robbers". Ouch! Is Jesus really calling the religious leaders of his time thieves and robbers? "but the sheep have not listened to them." Is he saying that those outside of the religious assembly, those who pay no attention to their teaching the real sheep, the real people of God?

Next Jesus identifies himself as the gate and the path to true life and blessing and compares himself to the thieves of organised religion.

11 “I am the good shepherd. The good shepherd lays down his life for the sheep. 12 The hired hand is not the shepherd and does not own the sheep. So when he sees the wolf coming, he abandons the sheep and runs away. Then the wolf attacks the flock and scatters it. 13 The man runs away because he is a hired hand and cares nothing for the sheep.
Now Jesus presses his point. He will die for his sheep. The hired hand will not. This is the problem with organised religion. They do it for money. As soon as money gets involved then there is the potential for divided loyalties. You cannot serve both God and money.

As for dogs attacking sheep have you seen it happen? The dog does attack all the sheep at once. Instead it selects one and puts itself between its target and the flock so that it can pick it off. The wolf tries to divide God's people, to cut them off from each other. If someone is getting singled out by gossip and being pushed out of the flock will a hired hand get involved on their behalf? Of course not he knows where his self interest lies and it is not taking on the wolf.

14 “I am the good shepherd; I know my sheep and my sheep know me—15 just as the Father knows me and I know the Father—and I lay down my life for the sheep. 16 I have other sheep that are not of this sheep pen. I must bring them also. They too will listen to my voice, and there shall be one flock and one shepherd. 17 The reason my Father loves me is that I lay down my life—only to take it up again. 18 No one takes it from me, but I lay it down of my own accord. I have authority to lay it down and authority to take it up again. This command I received from my Father.”

Jesus speaks of his love for us and his sacrifice. Those of faith will listen to his voice. In these days if we look at organised religion do we truly see one flock with one shepherd. It is getting better the animosity between various brands of ungodly religion is reducing as Jesus' sheep recognise Christ in each other and ignore these traditional barriers.

Tuesday, 1 March 2016

Global Erlang namespace wrecks modularity

Global Erlang namespace wrecks modularity

Many of the best properties of Erlang systems derive from the architecture of lightweight independent processes without shared memory solving a problem by message passing.  Messages are addressed to processes.  To send a process a message then the sender needs the destination address (process id) or the recipient.
To avoid the need for the sender to know the address of the recipient system processes register a global name in the erlang runtime system.  These processes can then be found by a lookup of the register.  A similar situation also arises with applications which again register a globally unique name.
In some situations it would be good to break up our system into disjoint sets of processes each operating in its own context.  Consider providing webservices, each customer has its own pages, its own database etc.  As soon as a global name is used, like a webserver application, or a mnesia database the structure is compromised.  The only answer is separate erlang instances.


Saturday, 3 November 2012

A simple erlang daemon

Hello gentle readers.

Recently I have been learning the erlang programming language.  One task I gave myself was to write a linux daemon.

As you probably already know, daemons are used to run unix services.  Services commonly controlled by daemons include database servers, web servers, web proxies etc.  In this example the server is very simple, the client calls the function "say_hi" and the server responds with "hello".

In the linux environment daemons are controlled by scripts that are stored in places such as /etc/init.d.  These scripts respond according to convention to the commands start, stop and restart.

 Let us start with the shell script:

#!/bin/sh

EBIN=$HOME/Documents/Erlang/Daemon

ERL=/usr/bin/erl

case $1 in

  start|stop|restart)
    $ERL -detached -sname mynode \
           -run daemon shell_do $1  >> daemon2.log
    ;;


  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac

exit 0


This has to be one of the simplest shell scripts that you have ever seen.  Daemon respond to three different commands, stop, start and restart.  In this script the command is simply passed through to the daemon.  One improvement would be to exit with the return code from the daemon execution.

So how about the daemon?  Here it is...

%% PURPOSE
%%
%% Manage an erlang daemon process as controlled by a shell scripts
%%   Allow standard daemon control verbs
%%      Start  - Starts a daemon in detached mode and exits
%%      Stop   - Attaches to the daemon, monitors it, sends an EXIT message and waits for it to die
%%      Restart - Calls stop and then start
%%   Log events
%%   Return UNIX compatible codes for functions called from shell scripts
%%   Exit shell script calls so as to not stop the scripts from completing
%%   Shell scripts expected to use shell_do to execute functions
%%
%% Allow interaction with daemon from other erlang nodes. 
%%   Erlang processes are expected to call functions directly rather than through shell_do
%%
%% MOTIVATION
%%   Erlang is great, but as an application it needs to be managed by system scripts.
%%   This is particularly for process that are expected to be running without user initiation.
%%
%% INVOCATION
%%   See daemon.sh for details of calling this module from a shell script.
%%
%% TO DO
%%   Define and use error handler for spawn call.

-module(daemon).
%-compile([{debug_info}]).
-export [start/0,start/1,stop_daemon/0,say_hi/0,kill/0,shell_do/1].
%%-define (DAEMON_NAME,daemon@blessing).
-define (DAEMON_NAME,list_to_atom("daemon@"++net_adm:localhost())).
-define (UNIX_OKAY_RESULT,0).
-define (TIMEOUT_STARTING_VM,1).
-define (VM_STARTED_WITHOUT_NAME,2).
-define (INVALID_VERB,3).
-define (COULD_NOT_CONNECT,4).
-define (TIMEOUT_WAITING_QUIT,5).
-define (TIMEOUT_STOPPING_VM,6).

wait_vm_start(_,0) -> ?TIMEOUT_STARTING_VM;
wait_vm_start(D,N) ->
   net_kernel:connect(D),
   Dl = lists:filter(fun(X) -> X==D end,nodes()),
   if Dl =:= [] ->
       receive after 1000 -> true end,
       wait_vm_start(D,N-1);
     Dl /= [] -> ?UNIX_OKAY_RESULT
   end.

wait_vm_stop(_,0) -> ?TIMEOUT_STOPPING_VM;
wait_vm_stop(D,N) ->
   net_kernel:connect(D),
   Dl = lists:filter(fun(X) -> X==D end,nodes()),
   if Dl /= [] ->
       receive after 1000 -> true end,
       wait_vm_start(D,N-1);
      Dl == [] -> ?UNIX_OKAY_RESULT
   end.

flush() ->
    receive
        _ ->
            flush()
    after
        0 ->
            true
    end.

sd(Hdl) ->
   MyNode=node(),
   if
      MyNode =:= nonode@nohost ->
         info(stdout,"~s","Error: Erlang not started with a name.  Use -sname <name>"),
         ?VM_STARTED_WITHOUT_NAME;
      MyNode /= nonode@nohost ->
         Atm_daemon = ?DAEMON_NAME,
         Connected = net_kernel:connect(Atm_daemon),
         case Connected of
            true ->
               info(Hdl,"~s",["daemon process already started"]),
               ?UNIX_OKAY_RESULT;
            false ->
               info(Hdl,"~s",["starting daemon process"]),
               StartString = "erl -detached -sname daemon",
               os:cmd(StartString),
               Vm_daemon = wait_vm_start(Atm_daemon,10),
               case Vm_daemon of
                  ?UNIX_OKAY_RESULT ->
                     info(Hdl,"~s",["spawning main daemon process"]),
                     spawn(Atm_daemon,?MODULE,start,[]), ?UNIX_OKAY_RESULT;
                  A -> A
               end
         end % case Connected %
   end.


say_hi() ->
   Daemon = ?DAEMON_NAME,
   Connected = net_kernel:connect(Daemon),
   if Connected ->
      {listener,Daemon} ! {hello,self()},
      receive
          Response -> Response
      after 10000 -> timeout end;
      not Connected -> could_not_connect
   end.


stop_daemon() ->
   Daemon = ?DAEMON_NAME,
   Connected = net_kernel:connect(Daemon),
   if Connected ->
         flush(),
         {listener,Daemon} ! {quit,self()},
         receive
         bye -> wait_vm_stop(Daemon,10)
     after 10000 -> ?TIMEOUT_WAITING_QUIT
         end;
      not Connected -> ?COULD_NOT_CONNECT
   end.

shell_do(Verb) ->
   {A,Hdl} = file:open('daemon_client.log',[append]),
   case A of
      ok ->
       info(Hdl,"~s",[Verb]);
      error  -> error
   end,
   Result = handle_verb(Hdl,Verb),
   info(Hdl,"Return status ~.10B",[Result]),
   init:stop(Result).

%%handle_verb(_,_) -> 0;

handle_verb(Hdl,["start"]) -> sd(Hdl);
handle_verb(_,["stop"]) ->  stop_daemon();
handle_verb(Hdl,["restart"]) ->
    stop_daemon(),
    sd(Hdl);
handle_verb(Hdl,X) ->   
    info(Hdl,"handle_verb failed to match ~p",[X]),
    ?INVALID_VERB.
    
kill() ->
    rpc:call(?DAEMON_NAME, init, stop, []).

start(Source) ->
    Source ! starting,
    start().
   
start() ->
   register(listener,self()),
   case {_,Hdl}=file:open("daemon_server.log",[append]) of
     {ok,Hdl}    -> server(Hdl);
     {error,Hdl} -> {error,Hdl}
   end.
  
info(Hdl,Fmt,D)->
  io:fwrite(Hdl,"~w"++Fmt++"~n",[erlang:localtime()] ++ D).

server(Hdl) ->
   info(Hdl,"~s",["waiting"]),
   receive
      {hello,Sender} ->
          info(Hdl,"~s~w",["hello received from",Sender]),
          Sender ! hello,
          server(Hdl);
      {getpid,Sender} ->
          info(Hdl,"~s~w",["pid request from ",Sender]),
          Sender ! self(),
          server(Hdl);
      {quit,Sender} ->
          info(Hdl,"~s~w",["quit recevied from ",Sender]),
          Sender ! bye,
          init:stop();
      _ ->
          info(Hdl,"~s",["Unknown message received"])
      after
          50000 ->
             server(Hdl)
   end.


For the reader not used to reading erlang, there some of this code is run as a result of the shell script we saw above.  Other code in this file is the daemon itself.  Referring back to the shell script we see that the script calls procedure shell_do.  Shell_do writes log entries, calls handle_verb and exits.  Handle_verb implements the different behaviours for each verb.  Starting the daemon is handled by function sd, which creates the daemon by an operating system call os:cmd, waits for the erlang virtual machine to initialise, and then spawns the server code called start, which in turn calls server.

This daemon code is quite straightforward and could form the basis of a generic server in the style of OTP.  I hope it will be useful to others who wish to build their own daemons in erlang.


Tony

Friday, 16 March 2012

911 Revisionism

Wikipedia defines historical revisionism as:

In historiography, historical revisionism is the reinterpretation of orthodox views on evidence, motivations, and decision-making processes surrounding a historical event. Though the word revisionism is sometimes used in a negative way, constant revision of history is part of the normal scholarly process of writing history.

According to this definition I am a revisionist.  I believe that currently the weight of scientific evidence supports 911 being an act of controlled demolition, and if it was an act of terrorism, it was an act of terrorism perpetrated by well connected people within the United States against the people of the United States.

For a number of revisionists, including myself, the point of entry into scepticism of the official story is the collapse of World Trade Center Building 7.  This was a 42 floor skyscaper across the street that decided to implode apparently because its mates across the street, buildings 1 and 2 decided to fall down.  The attribute of copy-cat suicide is not a well known property of skyscapers.

Not only did this building decide to collapse for no apparent reason, it did so in a manner that was identical to a professionally orchestrated controlled demolition.  There were a myriad  of ways it could have collapsed, example it could have toppled over if there were more fire on one side that on the other.  The floors could have collapsed leaving the central core which contains elevators etc.

Then there was the speed of collapse.  Anyone out there can obtain the video footage of the collapse, and from publicly available information of dimensions of the build measure its fall.  For over two seconds it falls with freefall acceleration.  This is difficult to explain with anything but controlled demolition.

To a person with an open mind the official story does not stack up, and doubts about arise.  Then one learns that the steel beams in building 7 showed signs of eutectic melting, that is when sulphur is added to steel to make it melt more easily.  Where did the sulphur come from, were firemen in there adding sulphur to the flames to bring the building down? I don't think so.

One learns of iron spheres in the dust. Then when a group of scientists find nano themite in the dust a lot of the other evidence begins to make sense.  Iron spheres are a by-product of the thermite reaction.  Sulphur is sometimes added to thermite to assist cutting and this mixture is called thermate.  The pools of molten steel found in the ruins make sense.  The building collapse makes sense. 

A couple of days ago I was made aware of a scientific report that claimed there was no thermite, and that the claimed thermite dust was merely an iron oxide pigmented, clay thickened epoxy primer paint.  They had lots of fancy equipment, but time and time again failed to do the obvious.  If you heat thermite enough it goes bang, if you heat paint it goes gooey and black.  The researchers appeared to avoid the ignition point of 430 C which had been identified in Harrit et al. even though it was well within the range of a NEY Vulcan furnace.  When one puts this report in context, it looks like another attempt to bolster the official con.  The avoidance of the obvious "heat it and thermite goes bang" test is a sign of either poor faith or poor science.

Some people ask "why would anyone stage a terrorist attack on the USA in such a was as to implicate Muslim radicals?"  If you want the answer to that one read "War is a racket" which I posted on this blog site this morning.

Kia kaha
God Is Good.

Tony

Note:
The report referred to specifies that they were using an NEY programmable furnace.  They did not specify the model.  The reference to a Vulcan furnace is my own, as I was attempting to identify the range of temperatures that an NEY furnace could produce.  My thought was maybe 400C was the highest temperature their equipment could reach?  When I found reference to NEY furnaces on the internet I found the Vulcan furnace could reach 2012F, which is well above 430C.

War Is A Racket

Sometimes better than displaying my own ignorance, it is better to find someone who has something to say, and give them the stand. This man died in 1940, but I believe what he says is as relevant today as it was in 1940.

Tony Wallace

War Is A Racket
By Major General Smedley Butler


Contents
        Chapter 1: War Is A Racket       
        Chapter 2: Who Makes The Profits?       
        Chapter 3: Who Pays The Bills?       
        Chapter 4: How To Smash This Racket!       
        Chapter 5: To Hell With War!       

        Smedley Darlington Butler

            Born: West Chester, Pa., July 30, 1881
            Educated: Haverford School
            Married: Ethel C. Peters, of Philadelphia, June 30, 1905
            Awarded two congressional medals of honor:
                capture of Vera Cruz, Mexico, 1914
                capture of Ft. Riviere, Haiti, 1917
            Distinguished service medal, 1919
            Major General - United States Marine Corps
            Retired Oct. 1, 1931
            On leave of absence to act as
            director of Dept. of Safety, Philadelphia, 1932
            Lecturer -- 1930's
            Republican Candidate for Senate, 1932
            Died at Naval Hospital, Philadelphia, June 21, 1940
            For more information about Major General Butler,
            contact the United States Marine Corps.




        | Top |

        CHAPTER ONE

        War Is A Racket

        WAR is a racket. It always has been.

        It is possibly the oldest, easily the most profitable, surely the most vicious. It is the only one international in scope. It is the only one in which the profits are reckoned in dollars and the losses in lives.

        A racket is best described, I believe, as something that is not what it seems to the majority of the people. Only a small "inside" group knows what it is about. It is conducted for the benefit of the very few, at the expense of the very many. Out of war a few people make huge fortunes.

        In the World War [I] a mere handful garnered the profits of the conflict. At least 21,000 new millionaires and billionaires were made in the United States during the World War. That many admitted their huge blood gains in their income tax returns. How many other war millionaires falsified their tax returns no one knows.

        How many of these war millionaires shouldered a rifle? How many of them dug a trench? How many of them knew what it meant to go hungry in a rat-infested dug-out? How many of them spent sleepless, frightened nights, ducking shells and shrapnel and machine gun bullets? How many of them parried a bayonet thrust of an enemy? How many of them were wounded or killed in battle?

        Out of war nations acquire additional territory, if they are victorious. They just take it. This newly acquired territory promptly is exploited by the few -- the selfsame few who wrung dollars out of blood in the war. The general public shoulders the bill.

        And what is this bill?

        This bill renders a horrible accounting. Newly placed gravestones. Mangled bodies. Shattered minds. Broken hearts and homes. Economic instability. Depression and all its attendant miseries. Back-breaking taxation for generations and generations.

        For a great many years, as a soldier, I had a suspicion that war was a racket; not until I retired to civil life did I fully realize it. Now that I see the international war clouds gathering, as they are today, I must face it and speak out.

        Again they are choosing sides. France and Russia met and agreed to stand side by side. Italy and Austria hurried to make a similar agreement. Poland and Germany cast sheep's eyes at each other, forgetting for the nonce [one unique occasion], their dispute over the Polish Corridor.

        The assassination of King Alexander of Jugoslavia [Yugoslavia] complicated matters. Jugoslavia and Hungary, long bitter enemies, were almost at each other's throats. Italy was ready to jump in. But France was waiting. So was Czechoslovakia. All of them are looking ahead to war. Not the people -- not those who fight and pay and die -- only those who foment wars and remain safely at home to profit.

        There are 40,000,000 men under arms in the world today, and our statesmen and diplomats have the temerity to say that war is not in the making.

        Hell's bells! Are these 40,000,000 men being trained to be dancers?

        Not in Italy, to be sure. Premier Mussolini knows what they are being trained for. He, at least, is frank enough to speak out. Only the other day, Il Duce in "International Conciliation," the publication of the Carnegie Endowment for International Peace, said:

            "And above all, Fascism, the more it considers and observes the future and the development of humanity quite apart from political considerations of the moment, believes neither in the possibility nor the utility of perpetual peace. . . . War alone brings up to its highest tension all human energy and puts the stamp of nobility upon the people who have the courage to meet it."

        Undoubtedly Mussolini means exactly what he says. His well-trained army, his great fleet of planes, and even his navy are ready for war -- anxious for it, apparently. His recent stand at the side of Hungary in the latter's dispute with Jugoslavia showed that. And the hurried mobilization of his troops on the Austrian border after the assassination of Dollfuss showed it too. There are others in Europe too whose sabre rattling presages war, sooner or later.

        Herr Hitler, with his rearming Germany and his constant demands for more and more arms, is an equal if not greater menace to peace. France only recently increased the term of military service for its youth from a year to eighteen months.

        Yes, all over, nations are camping in their arms. The mad dogs of Europe are on the loose. In the Orient the maneuvering is more adroit. Back in 1904, when Russia and Japan fought, we kicked out our old friends the Russians and backed Japan. Then our very generous international bankers were financing Japan. Now the trend is to poison us against the Japanese. What does the "open door" policy to China mean to us? Our trade with China is about $90,000,000 a year. Or the Philippine Islands? We have spent about $600,000,000 in the Philippines in thirty-five years and we (our bankers and industrialists and speculators) have private investments there of less than $200,000,000.

        Then, to save that China trade of about $90,000,000, or to protect these private investments of less than $200,000,000 in the Philippines, we would be all stirred up to hate Japan and go to war -- a war that might well cost us tens of billions of dollars, hundreds of thousands of lives of Americans, and many more hundreds of thousands of physically maimed and mentally unbalanced men.

        Of course, for this loss, there would be a compensating profit -- fortunes would be made. Millions and billions of dollars would be piled up. By a few. Munitions makers. Bankers. Ship builders. Manufacturers. Meat packers. Speculators. They would fare well.

        Yes, they are getting ready for another war. Why shouldn't they? It pays high dividends.

        But what does it profit the men who are killed? What does it profit their mothers and sisters, their wives and their sweethearts? What does it profit their children?

        What does it profit anyone except the very few to whom war means huge profits?

        Yes, and what does it profit the nation?

        Take our own case. Until 1898 we didn't own a bit of territory outside the mainland of North America. At that time our national debt was a little more than $1,000,000,000. Then we became "internationally minded." We forgot, or shunted aside, the advice of the Father of our country. We forgot George Washington's warning about "entangling alliances." We went to war. We acquired outside territory. At the end of the World War period, as a direct result of our fiddling in international affairs, our national debt had jumped to over $25,000,000,000. Our total favorable trade balance during the twenty-five-year period was about $24,000,000,000. Therefore, on a purely bookkeeping basis, we ran a little behind year for year, and that foreign trade might well have been ours without the wars.

        It would have been far cheaper (not to say safer) for the average American who pays the bills to stay out of foreign entanglements. For a very few this racket, like bootlegging and other underworld rackets, brings fancy profits, but the cost of operations is always transferred to the people -- who do not profit.

        

        | Top |

        CHAPTER TWO

        Who Makes The Profits?

        The World War, rather our brief participation in it, has cost the United States some $52,000,000,000. Figure it out. That means $400 to every American man, woman, and child. And we haven't paid the debt yet. We are paying it, our children will pay it, and our children's children probably still will be paying the cost of that war.

        The normal profits of a business concern in the United States are six, eight, ten, and sometimes twelve percent. But war-time profits -- ah! that is another matter -- twenty, sixty, one hundred, three hundred, and even eighteen hundred per cent -- the sky is the limit. All that traffic will bear. Uncle Sam has the money. Let's get it.

        Of course, it isn't put that crudely in war time. It is dressed into speeches about patriotism, love of country, and "we must all put our shoulders to the wheel," but the profits jump and leap and skyrocket -- and are safely pocketed. Let's just take a few examples:

        Take our friends the du Ponts, the powder people -- didn't one of them testify before a Senate committee recently that their powder won the war? Or saved the world for democracy? Or something? How did they do in the war? They were a patriotic corporation. Well, the average earnings of the du Ponts for the period 1910 to 1914 were $6,000,000 a year. It wasn't much, but the du Ponts managed to get along on it. Now let's look at their average yearly profit during the war years, 1914 to 1918. Fifty-eight million dollars a year profit we find! Nearly ten times that of normal times, and the profits of normal times were pretty good. An increase in profits of more than 950 per cent.

        Take one of our little steel companies that patriotically shunted aside the making of rails and girders and bridges to manufacture war materials. Well, their 1910-1914 yearly earnings averaged $6,000,000. Then came the war. And, like loyal citizens, Bethlehem Steel promptly turned to munitions making. Did their profits jump -- or did they let Uncle Sam in for a bargain? Well, their 1914-1918 average was $49,000,000 a year!

        Or, let's take United States Steel. The normal earnings during the five-year period prior to the war were $105,000,000 a year. Not bad. Then along came the war and up went the profits. The average yearly profit for the period 1914-1918 was $240,000,000. Not bad.

        There you have some of the steel and powder earnings. Let's look at something else. A little copper, perhaps. That always does well in war times.

        Anaconda, for instance. Average yearly earnings during the pre-war years 1910-1914 of $10,000,000. During the war years 1914-1918 profits leaped to $34,000,000 per year.

        Or Utah Copper. Average of $5,000,000 per year during the 1910-1914 period. Jumped to an average of $21,000,000 yearly profits for the war period.

        Let's group these five, with three smaller companies. The total yearly average profits of the pre-war period 1910-1914 were $137,480,000. Then along came the war. The average yearly profits for this group skyrocketed to $408,300,000.

        A little increase in profits of approximately 200 per cent.

        Does war pay? It paid them. But they aren't the only ones. There are still others. Let's take leather.

        For the three-year period before the war the total profits of Central Leather Company were $3,500,000. That was approximately $1,167,000 a year. Well, in 1916 Central Leather returned a profit of $15,000,000, a small increase of 1,100 per cent. That's all. The General Chemical Company averaged a profit for the three years before the war of a little over $800,000 a year. Came the war, and the profits jumped to $12,000,000. a leap of 1,400 per cent.

        International Nickel Company -- and you can't have a war without nickel -- showed an increase in profits from a mere average of $4,000,000 a year to $73,000,000 yearly. Not bad? An increase of more than 1,700 per cent.

        American Sugar Refining Company averaged $2,000,000 a year for the three years before the war. In 1916 a profit of $6,000,000 was recorded.

        Listen to Senate Document No. 259. The Sixty-Fifth Congress, reporting on corporate earnings and government revenues. Considering the profits of 122 meat packers, 153 cotton manufacturers, 299 garment makers, 49 steel plants, and 340 coal producers during the war. Profits under 25 per cent were exceptional. For instance the coal companies made between 100 per cent and 7,856 per cent on their capital stock during the war. The Chicago packers doubled and tripled their earnings.

        And let us not forget the bankers who financed the great war. If anyone had the cream of the profits it was the bankers. Being partnerships rather than incorporated organizations, they do not have to report to stockholders. And their profits were as secret as they were immense. How the bankers made their millions and their billions I do not know, because those little secrets never become public -- even before a Senate investigatory body.

        But here's how some of the other patriotic industrialists and speculators chiseled their way into war profits.

        Take the shoe people. They like war. It brings business with abnormal profits. They made huge profits on sales abroad to our allies. Perhaps, like the munitions manufacturers and armament makers, they also sold to the enemy. For a dollar is a dollar whether it comes from Germany or from France. But they did well by Uncle Sam too. For instance, they sold Uncle Sam 35,000,000 pairs of hobnailed service shoes. There were 4,000,000 soldiers. Eight pairs, and more, to a soldier. My regiment during the war had only one pair to a soldier. Some of these shoes probably are still in existence. They were good shoes. But when the war was over Uncle Sam has a matter of 25,000,000 pairs left over. Bought -- and paid for. Profits recorded and pocketed.

        There was still lots of leather left. So the leather people sold your Uncle Sam hundreds of thousands of McClellan saddles for the cavalry. But there wasn't any American cavalry overseas! Somebody had to get rid of this leather, however. Somebody had to make a profit in it -- so we had a lot of McClellan saddles. And we probably have those yet.

        Also somebody had a lot of mosquito netting. They sold your Uncle Sam 20,000,000 mosquito nets for the use of the soldiers overseas. I suppose the boys were expected to put it over them as they tried to sleep in muddy trenches -- one hand scratching cooties on their backs and the other making passes at scurrying rats. Well, not one of these mosquito nets ever got to France!

        Anyhow, these thoughtful manufacturers wanted to make sure that no soldier would be without his mosquito net, so 40,000,000 additional yards of mosquito netting were sold to Uncle Sam.

        There were pretty good profits in mosquito netting in those days, even if there were no mosquitoes in France. I suppose, if the war had lasted just a little longer, the enterprising mosquito netting manufacturers would have sold your Uncle Sam a couple of consignments of mosquitoes to plant in France so that more mosquito netting would be in order.

        Airplane and engine manufacturers felt they, too, should get their just profits out of this war. Why not? Everybody else was getting theirs. So $1,000,000,000 -- count them if you live long enough -- was spent by Uncle Sam in building airplane engines that never left the ground! Not one plane, or motor, out of the billion dollars worth ordered, ever got into a battle in France. Just the same the manufacturers made their little profit of 30, 100, or perhaps 300 per cent.

        Undershirts for soldiers cost 14¢ [cents] to make and uncle Sam paid 30¢ to 40¢ each for them -- a nice little profit for the undershirt manufacturer. And the stocking manufacturer and the uniform manufacturers and the cap manufacturers and the steel helmet manufacturers -- all got theirs.

        Why, when the war was over some 4,000,000 sets of equipment -- knapsacks and the things that go to fill them -- crammed warehouses on this side. Now they are being scrapped because the regulations have changed the contents. But the manufacturers collected their wartime profits on them -- and they will do it all over again the next time.

        There were lots of brilliant ideas for profit making during the war.

        One very versatile patriot sold Uncle Sam twelve dozen 48-inch wrenches. Oh, they were very nice wrenches. The only trouble was that there was only one nut ever made that was large enough for these wrenches. That is the one that holds the turbines at Niagara Falls. Well, after Uncle Sam had bought them and the manufacturer had pocketed the profit, the wrenches were put on freight cars and shunted all around the United States in an effort to find a use for them. When the Armistice was signed it was indeed a sad blow to the wrench manufacturer. He was just about to make some nuts to fit the wrenches. Then he planned to sell these, too, to your Uncle Sam.

        Still another had the brilliant idea that colonels shouldn't ride in automobiles, nor should they even ride on horseback. One has probably seen a picture of Andy Jackson riding in a buckboard. Well, some 6,000 buckboards were sold to Uncle Sam for the use of colonels! Not one of them was used. But the buckboard manufacturer got his war profit.

        The shipbuilders felt they should come in on some of it, too. They built a lot of ships that made a lot of profit. More than $3,000,000,000 worth. Some of the ships were all right. But $635,000,000 worth of them were made of wood and wouldn't float! The seams opened up -- and they sank. We paid for them, though. And somebody pocketed the profits.

        It has been estimated by statisticians and economists and researchers that the war cost your Uncle Sam $52,000,000,000. Of this sum, $39,000,000,000 was expended in the actual war itself. This expenditure yielded $16,000,000,000 in profits. That is how the 21,000 billionaires and millionaires got that way. This $16,000,000,000 profits is not to be sneezed at. It is quite a tidy sum. And it went to a very few.

        The Senate (Nye) committee probe of the munitions industry and its wartime profits, despite its sensational disclosures, hardly has scratched the surface.

        Even so, it has had some effect. The State Department has been studying "for some time" methods of keeping out of war. The War Department suddenly decides it has a wonderful plan to spring. The Administration names a committee -- with the War and Navy Departments ably represented under the chairmanship of a Wall Street speculator -- to limit profits in war time. To what extent isn't suggested. Hmmm. Possibly the profits of 300 and 600 and 1,600 per cent of those who turned blood into gold in the World War would be limited to some smaller figure.

        Apparently, however, the plan does not call for any limitation of losses -- that is, the losses of those who fight the war. As far as I have been able to ascertain there is nothing in the scheme to limit a soldier to the loss of but one eye, or one arm, or to limit his wounds to one or two or three. Or to limit the loss of life.

        There is nothing in this scheme, apparently, that says not more than 12 per cent of a regiment shall be wounded in battle, or that not more than 7 per cent in a division shall be killed.

        Of course, the committee cannot be bothered with such trifling matters.

        

        | Top |

        CHAPTER THREE

        Who Pays The Bills?

        Who provides the profits -- these nice little profits of 20, 100, 300, 1,500 and 1,800 per cent? We all pay them -- in taxation. We paid the bankers their profits when we bought Liberty Bonds at $100.00 and sold them back at $84 or $86 to the bankers. These bankers collected $100 plus. It was a simple manipulation. The bankers control the security marts. It was easy for them to depress the price of these bonds. Then all of us -- the people -- got frightened and sold the bonds at $84 or $86. The bankers bought them. Then these same bankers stimulated a boom and government bonds went to par -- and above. Then the bankers collected their profits.

        But the soldier pays the biggest part of the bill.

        If you don't believe this, visit the American cemeteries on the battlefields abroad. Or visit any of the veteran's hospitals in the United States. On a tour of the country, in the midst of which I am at the time of this writing, I have visited eighteen government hospitals for veterans. In them are a total of about 50,000 destroyed men -- men who were the pick of the nation eighteen years ago. The very able chief surgeon at the government hospital; at Milwaukee, where there are 3,800 of the living dead, told me that mortality among veterans is three times as great as among those who stayed at home.

        Boys with a normal viewpoint were taken out of the fields and offices and factories and classrooms and put into the ranks. There they were remolded; they were made over; they were made to "about face"; to regard murder as the order of the day. They were put shoulder to shoulder and, through mass psychology, they were entirely changed. We used them for a couple of years and trained them to think nothing at all of killing or of being killed.

        Then, suddenly, we discharged them and told them to make another "about face" ! This time they had to do their own readjustment, sans [without] mass psychology, sans officers' aid and advice and sans nation-wide propaganda. We didn't need them any more. So we scattered them about without any "three-minute" or "Liberty Loan" speeches or parades. Many, too many, of these fine young boys are eventually destroyed, mentally, because they could not make that final "about face" alone.

        In the government hospital in Marion, Indiana, 1,800 of these boys are in pens! Five hundred of them in a barracks with steel bars and wires all around outside the buildings and on the porches. These already have been mentally destroyed. These boys don't even look like human beings. Oh, the looks on their faces! Physically, they are in good shape; mentally, they are gone.

        There are thousands and thousands of these cases, and more and more are coming in all the time. The tremendous excitement of the war, the sudden cutting off of that excitement -- the young boys couldn't stand it.

        That's a part of the bill. So much for the dead -- they have paid their part of the war profits. So much for the mentally and physically wounded -- they are paying now their share of the war profits. But the others paid, too -- they paid with heartbreaks when they tore themselves away from their firesides and their families to don the uniform of Uncle Sam -- on which a profit had been made. They paid another part in the training camps where they were regimented and drilled while others took their jobs and their places in the lives of their communities. The paid for it in the trenches where they shot and were shot; where they were hungry for days at a time; where they slept in the mud and the cold and in the rain -- with the moans and shrieks of the dying for a horrible lullaby.

        But don't forget -- the soldier paid part of the dollars and cents bill too.

        Up to and including the Spanish-American War, we had a prize system, and soldiers and sailors fought for money. During the Civil War they were paid bonuses, in many instances, before they went into service. The government, or states, paid as high as $1,200 for an enlistment. In the Spanish-American War they gave prize money. When we captured any vessels, the soldiers all got their share -- at least, they were supposed to. Then it was found that we could reduce the cost of wars by taking all the prize money and keeping it, but conscripting [drafting] the soldier anyway. Then soldiers couldn't bargain for their labor, Everyone else could bargain, but the soldier couldn't.

        Napoleon once said,

            "All men are enamored of decorations . . . they positively hunger for them."

        So by developing the Napoleonic system -- the medal business -- the government learned it could get soldiers for less money, because the boys liked to be decorated. Until the Civil War there were no medals. Then the Congressional Medal of Honor was handed out. It made enlistments easier. After the Civil War no new medals were issued until the Spanish-American War.

        In the World War, we used propaganda to make the boys accept conscription. They were made to feel ashamed if they didn't join the army.

        So vicious was this war propaganda that even God was brought into it. With few exceptions our clergymen joined in the clamor to kill, kill, kill. To kill the Germans. God is on our side . . . it is His will that the Germans be killed.

        And in Germany, the good pastors called upon the Germans to kill the allies . . . to please the same God. That was a part of the general propaganda, built up to make people war conscious and murder conscious.

        Beautiful ideals were painted for our boys who were sent out to die. This was the "war to end all wars." This was the "war to make the world safe for democracy." No one mentioned to them, as they marched away, that their going and their dying would mean huge war profits. No one told these American soldiers that they might be shot down by bullets made by their own brothers here. No one told them that the ships on which they were going to cross might be torpedoed by submarines built with United States patents. They were just told it was to be a "glorious adventure."

        Thus, having stuffed patriotism down their throats, it was decided to make them help pay for the war, too. So, we gave them the large salary of $30 a month.

        All they had to do for this munificent sum was to leave their dear ones behind, give up their jobs, lie in swampy trenches, eat canned willy (when they could get it) and kill and kill and kill . . . and be killed.

        But wait!

        Half of that wage (just a little more than a riveter in a shipyard or a laborer in a munitions factory safe at home made in a day) was promptly taken from him to support his dependents, so that they would not become a charge upon his community. Then we made him pay what amounted to accident insurance -- something the employer pays for in an enlightened state -- and that cost him $6 a month. He had less than $9 a month left.

        Then, the most crowning insolence of all -- he was virtually blackjacked into paying for his own ammunition, clothing, and food by being made to buy Liberty Bonds. Most soldiers got no money at all on pay days.

        We made them buy Liberty Bonds at $100 and then we bought them back -- when they came back from the war and couldn't find work -- at $84 and $86. And the soldiers bought about $2,000,000,000 worth of these bonds!

        Yes, the soldier pays the greater part of the bill. His family pays too. They pay it in the same heart-break that he does. As he suffers, they suffer. At nights, as he lay in the trenches and watched shrapnel burst about him, they lay home in their beds and tossed sleeplessly -- his father, his mother, his wife, his sisters, his brothers, his sons, and his daughters.

        When he returned home minus an eye, or minus a leg or with his mind broken, they suffered too -- as much as and even sometimes more than he. Yes, and they, too, contributed their dollars to the profits of the munitions makers and bankers and shipbuilders and the manufacturers and the speculators made. They, too, bought Liberty Bonds and contributed to the profit of the bankers after the Armistice in the hocus-pocus of manipulated Liberty Bond prices.

        And even now the families of the wounded men and of the mentally broken and those who never were able to readjust themselves are still suffering and still paying.

        

        | Top |

        CHAPTER FOUR

        How To Smash This Racket!

        WELL, it's a racket, all right.

        A few profit -- and the many pay. But there is a way to stop it. You can't end it by disarmament conferences. You can't eliminate it by peace parleys at Geneva. Well-meaning but impractical groups can't wipe it out by resolutions. It can be smashed effectively only by taking the profit out of war.

        The only way to smash this racket is to conscript capital and industry and labor before the nations manhood can be conscripted. One month before the Government can conscript the young men of the nation -- it must conscript capital and industry and labor. Let the officers and the directors and the high-powered executives of our armament factories and our munitions makers and our shipbuilders and our airplane builders and the manufacturers of all the other things that provide profit in war time as well as the bankers and the speculators, be conscripted -- to get $30 a month, the same wage as the lads in the trenches get.

        Let the workers in these plants get the same wages -- all the workers, all presidents, all executives, all directors, all managers, all bankers -- yes, and all generals and all admirals and all officers and all politicians and all government office holders -- everyone in the nation be restricted to a total monthly income not to exceed that paid to the soldier in the trenches!

        Let all these kings and tycoons and masters of business and all those workers in industry and all our senators and governors and majors pay half of their monthly $30 wage to their families and pay war risk insurance and buy Liberty Bonds.

        Why shouldn't they?

        They aren't running any risk of being killed or of having their bodies mangled or their minds shattered. They aren't sleeping in muddy trenches. They aren't hungry. The soldiers are!

        Give capital and industry and labor thirty days to think it over and you will find, by that time, there will be no war. That will smash the war racket -- that and nothing else.

        Maybe I am a little too optimistic. Capital still has some say. So capital won't permit the taking of the profit out of war until the people -- those who do the suffering and still pay the price -- make up their minds that those they elect to office shall do their bidding, and not that of the profiteers.

        Another step necessary in this fight to smash the war racket is the limited plebiscite to determine whether a war should be declared. A plebiscite not of all the voters but merely of those who would be called upon to do the fighting and dying. There wouldn't be very much sense in having a 76-year-old president of a munitions factory or the flat-footed head of an international banking firm or the cross-eyed manager of a uniform manufacturing plant -- all of whom see visions of tremendous profits in the event of war -- voting on whether the nation should go to war or not. They never would be called upon to shoulder arms -- to sleep in a trench and to be shot. Only those who would be called upon to risk their lives for their country should have the privilege of voting to determine whether the nation should go to war.

        There is ample precedent for restricting the voting to those affected. Many of our states have restrictions on those permitted to vote. In most, it is necessary to be able to read and write before you may vote. In some, you must own property. It would be a simple matter each year for the men coming of military age to register in their communities as they did in the draft during the World War and be examined physically. Those who could pass and who would therefore be called upon to bear arms in the event of war would be eligible to vote in a limited plebiscite. They should be the ones to have the power to decide -- and not a Congress few of whose members are within the age limit and fewer still of whom are in physical condition to bear arms. Only those who must suffer should have the right to vote.

        A third step in this business of smashing the war racket is to make certain that our military forces are truly forces for defense only.

        At each session of Congress the question of further naval appropriations comes up. The swivel-chair admirals of Washington (and there are always a lot of them) are very adroit lobbyists. And they are smart. They don't shout that "We need a lot of battleships to war on this nation or that nation." Oh no. First of all, they let it be known that America is menaced by a great naval power. Almost any day, these admirals will tell you, the great fleet of this supposed enemy will strike suddenly and annihilate 125,000,000 people. Just like that. Then they begin to cry for a larger navy. For what? To fight the enemy? Oh my, no. Oh, no. For defense purposes only.

        Then, incidentally, they announce maneuvers in the Pacific. For defense. Uh, huh.

        The Pacific is a great big ocean. We have a tremendous coastline on the Pacific. Will the maneuvers be off the coast, two or three hundred miles? Oh, no. The maneuvers will be two thousand, yes, perhaps even thirty-five hundred miles, off the coast.

        The Japanese, a proud people, of course will be pleased beyond expression to see the united States fleet so close to Nippon's shores. Even as pleased as would be the residents of California were they to dimly discern through the morning mist, the Japanese fleet playing at war games off Los Angeles.

        The ships of our navy, it can be seen, should be specifically limited, by law, to within 200 miles of our coastline. Had that been the law in 1898 the Maine would never have gone to Havana Harbor. She never would have been blown up. There would have been no war with Spain with its attendant loss of life. Two hundred miles is ample, in the opinion of experts, for defense purposes. Our nation cannot start an offensive war if its ships can't go further than 200 miles from the coastline. Planes might be permitted to go as far as 500 miles from the coast for purposes of reconnaissance. And the army should never leave the territorial limits of our nation.

        To summarize: Three steps must be taken to smash the war racket.

            We must take the profit out of war.

            We must permit the youth of the land who would bear arms to decide whether or not there should be war.

            We must limit our military forces to home defense purposes.

        

        | Top |

        CHAPTER FIVE

        To Hell With War!

        I am not a fool as to believe that war is a thing of the past. I know the people do not want war, but there is no use in saying we cannot be pushed into another war.

        Looking back, Woodrow Wilson was re-elected president in 1916 on a platform that he had "kept us out of war" and on the implied promise that he would "keep us out of war." Yet, five months later he asked Congress to declare war on Germany.

        In that five-month interval the people had not been asked whether they had changed their minds. The 4,000,000 young men who put on uniforms and marched or sailed away were not asked whether they wanted to go forth to suffer and die.

        Then what caused our government to change its mind so suddenly?

        Money.

        An allied commission, it may be recalled, came over shortly before the war declaration and called on the President. The President summoned a group of advisers. The head of the commission spoke. Stripped of its diplomatic language, this is what he told the President and his group:

            "There is no use kidding ourselves any longer. The cause of the allies is lost. We now owe you (American bankers, American munitions makers, American manufacturers, American speculators, American exporters) five or six billion dollars.

            If we lose (and without the help of the United States we must lose) we, England, France and Italy, cannot pay back this money . . . and Germany won't.

            So . . . "

        Had secrecy been outlawed as far as war negotiations were concerned, and had the press been invited to be present at that conference, or had radio been available to broadcast the proceedings, America never would have entered the World War. But this conference, like all war discussions, was shrouded in utmost secrecy. When our boys were sent off to war they were told it was a "war to make the world safe for democracy" and a "war to end all wars."

        Well, eighteen years after, the world has less of democracy than it had then. Besides, what business is it of ours whether Russia or Germany or England or France or Italy or Austria live under democracies or monarchies? Whether they are Fascists or Communists? Our problem is to preserve our own democracy.

        And very little, if anything, has been accomplished to assure us that the World War was really the war to end all wars.

        Yes, we have had disarmament conferences and limitations of arms conferences. They don't mean a thing. One has just failed; the results of another have been nullified. We send our professional soldiers and our sailors and our politicians and our diplomats to these conferences. And what happens?

        The professional soldiers and sailors don't want to disarm. No admiral wants to be without a ship. No general wants to be without a command. Both mean men without jobs. They are not for disarmament. They cannot be for limitations of arms. And at all these conferences, lurking in the background but all-powerful, just the same, are the sinister agents of those who profit by war. They see to it that these conferences do not disarm or seriously limit armaments.

        The chief aim of any power at any of these conferences has not been to achieve disarmament to prevent war but rather to get more armament for itself and less for any potential foe.

        There is only one way to disarm with any semblance of practicability. That is for all nations to get together and scrap every ship, every gun, every rifle, every tank, every war plane. Even this, if it were possible, would not be enough.

        The next war, according to experts, will be fought not with battleships, not by artillery, not with rifles and not with machine guns. It will be fought with deadly chemicals and gases.

        Secretly each nation is studying and perfecting newer and ghastlier means of annihilating its foes wholesale. Yes, ships will continue to be built, for the shipbuilders must make their profits. And guns still will be manufactured and powder and rifles will be made, for the munitions makers must make their huge profits. And the soldiers, of course, must wear uniforms, for the manufacturer must make their war profits too.

        But victory or defeat will be determined by the skill and ingenuity of our scientists.

        If we put them to work making poison gas and more and more fiendish mechanical and explosive instruments of destruction, they will have no time for the constructive job of building greater prosperity for all peoples. By putting them to this useful job, we can all make more money out of peace than we can out of war -- even the munitions makers.

        So...I say,

        TO HELL WITH WAR!