xmldaemon/main.cpp
author Florian Pose <fp@igh-essen.com>
Tue, 02 Jan 2007 13:15:02 +0000
changeset 508 11fb47492456
parent 359 e93ea158b423
permissions -rw-r--r--
Updated NEWS and Changelog files.
359
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
/******************************************************************************
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
 *
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
 *  $Id$
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
 *
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
 *  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
 *
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
 *  This file is part of the IgH EtherCAT Master.
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
 *
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
 *  The IgH EtherCAT Master is free software; you can redistribute it
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
 *  and/or modify it under the terms of the GNU General Public License
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
 *  as published by the Free Software Foundation; either version 2 of the
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
 *  License, or (at your option) any later version.
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
 *
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
 *  The IgH EtherCAT Master is distributed in the hope that it will be
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
 *  GNU General Public License for more details.
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
 *
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
 *  You should have received a copy of the GNU General Public License
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
 *  along with the IgH EtherCAT Master; if not, write to the Free Software
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
 *
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
 *  The right to use EtherCAT Technology is granted and comes free of
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
 *  charge under condition of compatibility of product made by
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
 *  Licensee. People intending to distribute/sell products based on the
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
 *  code, have to sign an agreement to guarantee that products using
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
 *  software based on IgH EtherCAT master stay compatible with the actual
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
 *  EtherCAT specification (which are released themselves as an open
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
 *  standard) as the (only) precondition to have the right to use EtherCAT
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
 *  Technology, IP and trade marks.
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
 *
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
 *****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
/**
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
   \file
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
   EtherCAT XML Daemon.
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
*/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
/*****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
#include <signal.h>
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
#include <sys/stat.h>
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
#include <dirent.h>
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
#include <syslog.h>
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
#include <libxml/parser.h>
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
#include <iostream>
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
#include <list>
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
using namespace std;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
#include "slave_device.hpp"
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    53
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
/*****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
unsigned int sig_int_term = 0;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
unsigned int sig_hangup = 0;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    58
string xml_dir;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    59
bool become_daemon = true;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    60
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
list<SlaveDevice> slaveDevices;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    63
/*****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
void parse_xml_file(const char *);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
void parse_info(xmlDocPtr, xmlNodePtr);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    67
void parse_descriptions(xmlDocPtr, xmlNodePtr);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    68
void parse_devices(xmlDocPtr, xmlNodePtr);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    70
void read_xml_dir();
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    71
void set_signal_handlers();
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    72
void get_options(int, char *[]);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    73
void print_usage();
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
void signal_handler(int);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    75
void init_daemon();
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    76
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
/*****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    78
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    79
int main(int argc, char *argv[])
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
{
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
    set_signal_handlers();
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
    get_options(argc, argv);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
    read_xml_dir();
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    85
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    86
    if (become_daemon) init_daemon();
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    87
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    88
    openlog("ecxmld", LOG_PID, LOG_DAEMON);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    89
    syslog(LOG_INFO, "EtherCAT XML daemon starting up.");
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    90
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    91
    return 0;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    92
}
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    93
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    94
/*****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    95
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    96
void read_xml_dir()
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    97
{
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    98
    DIR *dir;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    99
    struct dirent *dir_ent;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   100
    string entry;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   101
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   102
    if (!(dir = opendir(xml_dir.c_str()))) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   103
        cerr << "ERROR: Failed to open XML directory \"" << xml_dir << "\"!"
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   104
             << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   105
        exit(1);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   106
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   107
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   108
    while ((dir_ent = readdir(dir))) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   109
        entry = dir_ent->d_name;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   110
        if (entry.size() < 4
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   111
            || entry.substr(entry.size() - 4) != ".xml") continue;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   112
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   113
        parse_xml_file((xml_dir + "/" + entry).c_str());
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   114
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   115
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   116
    // Verzeichnis schliessen
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   117
    closedir(dir);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   118
}
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   119
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   120
/*****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   121
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   122
void parse_xml_file(const char *xml_file)
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   123
{
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   124
    xmlDocPtr doc;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
    xmlNodePtr cur;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
    cout << xml_file << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
    if (!(doc = xmlParseFile(xml_file))) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   130
        cerr << "ERROR: Parse error in document!" << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   131
        return;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   132
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   133
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
    if (!(cur = xmlDocGetRootElement(doc))) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   135
        cout << "Empty document!" << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   136
        xmlFreeDoc(doc);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   137
        return;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   138
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   139
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   140
    if (xmlStrcmp(cur->name, (const xmlChar *) "EtherCATInfo")) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   141
        cerr << "Document of the wrong type!" << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   142
        xmlFreeDoc(doc);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   143
        return;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   144
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   145
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   146
    parse_info(doc, cur);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
    xmlFreeDoc(doc);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
}
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   149
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   150
/*****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   151
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   152
void parse_info(xmlDocPtr doc, xmlNodePtr cur)
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   153
{
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   154
    cout << "info" << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   155
    cur = cur->xmlChildrenNode;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   156
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   157
    while (cur) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   158
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "Descriptions"))) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   159
            parse_descriptions(doc, cur);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   160
        }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   161
	cur = cur->next;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   162
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   163
}
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   164
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   165
/*****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   166
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   167
void parse_descriptions(xmlDocPtr doc, xmlNodePtr cur)
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   168
{
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   169
    cout << "desc" << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   170
    cur = cur->xmlChildrenNode;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   171
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   172
    while (cur) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   173
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "Devices"))) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   174
            parse_devices(doc, cur);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   175
        }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   176
	cur = cur->next;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   177
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   178
}
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   179
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   180
/*****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   181
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   182
void parse_devices(xmlDocPtr doc, xmlNodePtr cur)
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   183
{
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   184
    cout << "devices" << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   185
    cur = cur->xmlChildrenNode;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   186
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   187
    while (cur) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   188
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "Device"))) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   189
            slaveDevices.push_back(SlaveDevice());
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   190
            slaveDevices.back().fromXml(doc, cur);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   191
        }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   192
	cur = cur->next;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   194
}
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
/*****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   198
void get_options(int argc, char *argv[])
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   199
{
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
    int c;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
    while (1) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
        if ((c = getopt(argc, argv, "d:kh")) == -1) break;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
            switch (c) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
            case 'd':
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
                xml_dir = optarg;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   208
                break;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
            case 'k':
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
                become_daemon = false;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
                break;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
            case 'h':
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
                print_usage();
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
                exit(0);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   217
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   218
            default:
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   219
                print_usage();
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
                exit(1);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   221
        }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   223
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   224
    if (optind < argc) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   225
        cerr << "ERROR: Too many arguments!" << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   226
        print_usage();
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   227
        exit(1);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   228
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   229
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   230
    if (xml_dir == "") {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   231
        cerr << "ERROR: XML directory not set!" << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   232
        print_usage();
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   233
        exit(1);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   234
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   235
}
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   236
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   237
/*****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   238
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   239
void print_usage()
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   240
{
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   241
    cout << "Usage: ecxmld [OPTIONS]" << endl
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   242
         << "   -d DIR   Set XML directory (MANDATORY)." << endl
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   243
         << "   -k       Do not detach from console." << endl
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   244
         << "   -h       Show this help." << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   245
}
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   246
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   247
/*****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   248
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   249
void signal_handler(int sig)
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   250
{
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   251
    if (sig == SIGHUP) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   252
        sig_hangup++;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   253
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   254
    else if (sig == SIGINT || sig == SIGTERM) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   255
        sig_int_term++;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   256
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   257
}
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   258
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   259
/*****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   260
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   261
void set_signal_handlers()
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   262
{
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   263
    struct sigaction action;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   264
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   265
    action.sa_handler = signal_handler;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   266
    sigemptyset(&action.sa_mask);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   267
    action.sa_flags = 0;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   268
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   269
    sigaction(SIGHUP, &action, 0);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   270
    sigaction(SIGINT, &action, 0);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   271
    sigaction(SIGTERM, &action, 0);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   272
}
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   273
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   274
/*****************************************************************************/
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   275
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   276
void init_daemon()
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   277
{
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   278
    pid_t pid;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   279
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   280
    if ((pid = fork()) < 0) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   281
        cerr << endl << "ERROR: fork() failed!" << endl << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   282
        exit(1);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   283
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   284
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   285
    if (pid) exit(0);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   286
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   287
    if (setsid() == -1) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   288
        cerr << "ERROR: Failed to become session leader!" << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   289
        exit(1);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   290
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   291
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   292
    if (chdir("/") < 0) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   293
        cerr << "ERROR: Failed to change to file root!" << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   294
        exit(1);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   295
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   296
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   297
    umask(0);
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   298
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   299
    if (close(0) < 0) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   300
        cerr << "WARNING: Failed to close STDIN!" << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   301
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   302
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   303
    if (close(1) < 0) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   304
        cerr << "WARNING: Failed to close STDOUT!" << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   305
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   306
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   307
    if (close(2) < 0) {
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   308
        cerr << "WARNING: Failed to close STDERR!" << endl;
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   309
    }
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   310
}
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   311
e93ea158b423 Added XML daemon.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   312
/*****************************************************************************/