[go: up one dir, main page]

Menu

[ba8fa9]: / src / main.c  Maximize  Restore  History

Download this file

135 lines (109 with data), 4.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright (c) 2013, Christian Ferrari <tiian@users.sourceforge.net>
* All rights reserved.
*
* This file is part of FLOM.
*
* FLOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
* FLOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FLOM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#ifdef HAVE_STDIO_H
# include <stdio.h>
#endif
#ifdef HAVE_GLIB_H
# include <glib.h>
#endif
#define FLOM_TRACE_MODULE FLOM_TRACE_MOD_GENERIC
#include "flom_config.h"
#include "flom_client.h"
#include "flom_conns.h"
#include "flom_errors.h"
#include "flom_exec.h"
#include "flom_inst_conf.h"
#include "flom_rsrc.h"
#include "flom_trace.h"
static gboolean print_version = FALSE;
static char *trace_file = NULL;
static gchar **command_argv = NULL;
/* command line options */
static GOptionEntry entries[] =
{
{ "version", 'v', 0, G_OPTION_ARG_NONE, &print_version, "Print package info and exit", NULL },
{ "trace-file", 't', 0, G_OPTION_ARG_STRING, &trace_file, "Specify daemon trace file name (absolute path required)", NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &command_argv, "Command must be executed under flom control" },
{ NULL }
};
int main (int argc, char *argv[])
{
GError *error = NULL;
GOptionContext *option_context;
int child_status = 0;
int ret_cod = FLOM_RC_INTERNAL_ERROR;
struct flom_conn_data_s cd;
FLOM_TRACE_INIT;
option_context = g_option_context_new("-- command to execute");
g_option_context_add_main_entries(option_context, entries, NULL);
if (!g_option_context_parse(option_context, &argc, &argv, &error)) {
g_print("option parsing failed: %s\n", error->message);
exit(1);
}
g_option_context_free(option_context);
if (print_version) {
g_print("FLOM: Free LOck Manager\n"
"Copyright (c) 2013, Christian Ferrari; "
"all rights reserved.\n"
"License: GPL (GNU Public License) version 2\n"
"Package name: %s; package version: %s; release date: %s\n"
"Access http://sourceforge.net/projects/flom/ to report bugs "
"and partecipate to the project\n",
FLOM_PACKAGE_NAME, FLOM_PACKAGE_VERSION, FLOM_PACKAGE_DATE);
exit(0);
}
flom_config_reset();
flom_config_set_trace_file(trace_file);
if (FLOM_RC_OK != (ret_cod = global_res_name_preg_init())) {
g_print("global_res_name_preg_init: ret_cod=%d\n", ret_cod);
exit(1);
}
/* open connection to a valid flom lock manager... */
if (FLOM_RC_OK != (ret_cod = flom_client_connect(&cd))) {
g_print("flom_client_connect: ret_cod=%d (%s)\n",
ret_cod, flom_strerror(ret_cod));
exit(1);
}
/* sending lock command */
if (FLOM_RC_OK != (ret_cod = flom_client_lock(&cd))) {
g_print("flom_client_lock: ret_cod=%d (%s)\n",
ret_cod, flom_strerror(ret_cod));
exit(1);
}
/* execute the command */
if (FLOM_RC_OK != (ret_cod = flom_exec(command_argv, &child_status))) {
g_print("flom_exec: ret_cod=%d\n", ret_cod);
exit(1);
}
/* sending unlock command */
if (FLOM_RC_OK != (ret_cod = flom_client_unlock(&cd))) {
g_print("flom_client_unlock: ret_cod=%d (%s)\n",
ret_cod, flom_strerror(ret_cod));
exit(1);
}
/* gracely disconnect from daemon */
if (FLOM_RC_OK != (ret_cod = flom_client_disconnect(&cd))) {
g_print("flom_client_unlock: ret_cod=%d (%s)\n",
ret_cod, flom_strerror(ret_cod));
}
g_strfreev (command_argv);
command_argv = NULL;
return child_status;
}