summaryrefslogtreecommitdiff
path: root/src/rcl-ntpd-utils.c
blob: ff4eb9601b8964d6a165d257515853c87fce712d (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (C) 2023 Andrey V.Kosteltsev <kx@radix.pro>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "rcl-ntpd-utils.h"

static gboolean exec_cmd( const gchar *cmd )
{
  int       exit_status;
  GError   *error = NULL;
  gboolean  ret = TRUE;

  if( !cmd || *cmd == '\0' ) return FALSE;

  if( !g_spawn_command_line_sync( cmd, NULL, NULL, &exit_status, &error ) )
  {
    g_error_free( error );
    ret = FALSE;
  }
  g_free( (gpointer)cmd );

  if( exit_status != 0 )
    ret = FALSE;

  return ret;
}

gboolean ntp_daemon_installed( void )
{
  if( g_file_test( NTPD_CONF, G_FILE_TEST_EXISTS ) &&
      g_file_test( NTPD_RC,   G_FILE_TEST_EXISTS )   )
    return TRUE;
  else
    return FALSE;
}

gboolean ntp_daemon_enabled( void )
{
  if( g_file_test( NTPD_CONF, G_FILE_TEST_EXISTS ) &&
      g_file_test( NTPD_RC,   G_FILE_TEST_EXISTS ) &&
      g_file_test( NTPD_RC,   G_FILE_TEST_IS_EXECUTABLE ) )
    return TRUE;
  else
    return FALSE;
}

gboolean ntp_daemon_status( void )
{
  gchar *cmd;

  if( g_file_test( NTPD_RC, G_FILE_TEST_EXISTS ) &&
      g_file_test( NTPD_RC, G_FILE_TEST_IS_EXECUTABLE ) )
  {
    cmd = g_strconcat( NTPD_RC, " status", NULL );
    if( !exec_cmd( (const gchar *)cmd ) )
      return FALSE;
    else
      return TRUE;
  }

  return FALSE;
}

gboolean stop_ntp_daemon( void )
{
  if( ntp_daemon_enabled() && !ntp_daemon_status() )
    return TRUE;

  if( ntp_daemon_enabled() )
  {
    gchar *cmd;
    cmd = g_strconcat( NTPD_RC, " stop", NULL );
    if( !exec_cmd( (const gchar *)cmd ) )
      return FALSE;
    else
      return TRUE;
  }

  return FALSE;
}

gboolean start_ntp_daemon( void )
{
  if( ntp_daemon_enabled() && ntp_daemon_status() )
    return TRUE;

  if( ntp_daemon_enabled() )
  {
    gchar *cmd;
    cmd = g_strconcat( NTPD_RC, " start", NULL );
    if( !exec_cmd( (const gchar *)cmd ) )
      return FALSE;
    else
      return TRUE;
  }

  return FALSE;
}

gboolean disable_ntp_daemon( void )
{
  gchar *cmd;

  if( !ntp_daemon_enabled() )
    return TRUE;

  if( ntp_daemon_status() )
    (void)stop_ntp_daemon();

  if(  g_file_test( NTPD_RC, G_FILE_TEST_EXISTS ) &&
      !g_file_test( NTPD_RC, G_FILE_TEST_IS_EXECUTABLE ) )
    return TRUE;

  if( g_file_test( NTPD_RC, G_FILE_TEST_EXISTS ) )
  {
    cmd = g_strconcat( "chmod 0644 ", NTPD_RC, NULL );
    if( !exec_cmd( (const gchar *)cmd ) )
      return FALSE;
    else
      return TRUE;
  }
  else
    return FALSE;
}

gboolean enable_ntp_daemon( void )
{
  gchar *cmd;

  if( ntp_daemon_enabled() )
    return TRUE;

  if( g_file_test( NTPD_RC, G_FILE_TEST_EXISTS ) &&
      g_file_test( NTPD_RC, G_FILE_TEST_IS_EXECUTABLE ) )
    return TRUE;

  if( g_file_test( NTPD_RC, G_FILE_TEST_EXISTS ) )
  {
    cmd = g_strconcat( "chmod 0755 ", NTPD_RC, NULL );
    if( !exec_cmd( (const gchar *)cmd ) )
      return FALSE;
    else
      return TRUE;
  }
  else
    return FALSE;
}