summaryrefslogtreecommitdiff
blob: cbd97d7cc8511ab09e01f9e251f01b952001aa6e (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * scripts.c
 *
 * Get info etc for Gentoo style rc-scripts.
 *
 * Copyright (C) 2004,2005 Martin Schlemmer <azarah@nosferatu.za.org>
 *
 *
 *      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 version 2 of the License.
 *
 *      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.,
 *      675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Header$
 */

#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include "internal/rccore.h"

LIST_HEAD (rcscript_list);

int
get_rcscripts (void)
{
  rcscript_info_t *info;
  char **file_list = NULL;
  char *rcscript;
  char *confd_file = NULL;
  int count;

  file_list = rc_ls_dir (RCSCRIPTS_INITDDIR, 0);
  if (NULL == file_list)
    {
      errno = ENOENT;
      DBG_MSG ("'%s' is empty!\n", RCSCRIPTS_INITDDIR);

      return -1;
    }

  str_list_for_each_item (file_list, rcscript, count)
    {
      /* Is it a file? */
      if (!(rc_is_file (rcscript, 1))
	  /* Do not process scripts, source or backup files. */
	  || (CHECK_FILE_EXTENSION (rcscript, ".c"))
	  || (CHECK_FILE_EXTENSION (rcscript, ".bak"))
	  || (CHECK_FILE_EXTENSION (rcscript, "~")))
	{
	  DBG_MSG ("'%s' is not a valid rc-script!\n", rc_basename (rcscript));
	}
      else
	{
	  regex_data_t tmp_data;
	  dyn_buf_t *dynbuf = NULL;
	  char *buf = NULL;

	  dynbuf = new_dyn_buf_mmap_file (rcscript);
	  if (NULL == dynbuf)
	    {
	      DBG_MSG ("Could not open '%s' for reading!\n",
		       rc_basename (rcscript));
	      goto error;
	    }

	  buf = read_line_dyn_buf (dynbuf);
	  free_dyn_buf (dynbuf);
	  if ((NULL == buf) && (0 != errno))
	    goto error;
	  if (NULL == buf)
	    {
	      DBG_MSG ("'%s' is not a valid rc-script!\n",
		       rc_basename (rcscript));
	      continue;
	    }

	  /* Check if it starts with '#!/sbin/runscript' */
	  DO_REGEX (tmp_data, buf, "[ \t]*#![ \t]*/sbin/runscript[ \t]*.*",
		    check_error);
	  free (buf);
	  if (REGEX_FULL_MATCH != tmp_data.match)
	    {
	      DBG_MSG ("'%s' is not a valid rc-script!\n",
		       rc_basename (rcscript));
	      continue;
	    }

	  /* We do not want rc-scripts ending in '.sh' */
	  if (CHECK_FILE_EXTENSION (rcscript, ".sh"))
	    {
	      EWARN ("'%s' is invalid (should not end with '.sh')!\n",
		     rc_basename (rcscript));
	      continue;
	    }

	  DBG_MSG ("Adding rc-script '%s' to list.\n", rc_basename (rcscript));

	  info = xmalloc (sizeof (rcscript_info_t));
	  if (NULL == info)
	    goto error;

	  /* Copy the name */
	  info->filename = xstrndup (rcscript, strlen (rcscript));
	  if (NULL == info->filename)
	    goto loop_error;

	  /* Get the modification time */
	  info->mtime = rc_get_mtime (rcscript, 1);
	  if (0 == info->mtime)
	    {
	      DBG_MSG ("Failed to get modification time for '%s'!\n", rcscript);
	      /* We do not care if it fails - we will pick up
	       * later if there is a problem with the file */
	    }

	  /* File name for the conf.d config file (if any) */
	  confd_file = rc_strcatpaths (RCSCRIPTS_CONFDDIR, rc_basename (rcscript));
	  if (NULL == confd_file)
	    {
	      DBG_MSG ("Failed to allocate temporary buffer!\n");
	      goto loop_error;
	    }

	  /* Get the modification time of the conf.d file
	   * (if any rc_file_exists) */
	  info->confd_mtime = rc_get_mtime (confd_file, 1);
	  if (0 == info->confd_mtime)
	    {
	      DBG_MSG ("Failed to get modification time for '%s'!\n",
		       confd_file);
	      /* We do not care that it fails, as not all
	       * rc-scripts will have conf.d config files */
	    }

	  free (confd_file);

	  list_add_tail (&info->node, &rcscript_list);

	  continue;

check_error:
	  free (buf);
	  goto error;

loop_error:
	  if (NULL != info)
	    free (info->filename);
	  free (info);

	  goto error;
	}
    }

  /* Final check if we have some entries */
  if (!check_strv (file_list))
    {
      errno = ENOENT;
      DBG_MSG ("No rc-scripts to parse!\n");
      goto error;
    }

  str_list_free (file_list);

  return 0;

error:
  str_list_free (file_list);

  return -1;
}

/* Returns 0 if we do not need to regen the cache file, else -1 with
 * errno set if something went wrong */
int
check_rcscripts_mtime (const char *cachefile)
{
  rcscript_info_t *info;
  time_t cache_mtime;
  time_t rc_conf_mtime;
  time_t rc_confd_mtime;

  if (!check_arg_str (cachefile))
    return -1;

  cache_mtime = rc_get_mtime (cachefile, 1);
  if (0 == cache_mtime)
    {
      DBG_MSG ("Could not get modification time for cache file '%s'!\n",
	       cachefile);
      return -1;
    }

  /* Get and compare mtime for RC_CONF_FILE_NAME with that of cachefile */
  rc_conf_mtime = rc_get_mtime (RC_CONF_FILE_NAME, 1);
  if (rc_conf_mtime > cache_mtime)
    {
      DBG_MSG ("'%s' have a later modification time than '%s'.\n",
	       RC_CONF_FILE_NAME, cachefile);
      return -1;
    }
  /* Get and compare mtime for RC_CONFD_FILE_NAME with that of cachefile */
  rc_confd_mtime = rc_get_mtime (RC_CONFD_FILE_NAME, 1);
  if (rc_confd_mtime > cache_mtime)
    {
      DBG_MSG ("'%s' have a later modification time than '%s'.\n",
	       RC_CONFD_FILE_NAME, cachefile);
      return -1;
    }

  /* Get and compare mtime for each rc-script and its conf.d config file
   * with that of cachefile */
  list_for_each_entry (info, &rcscript_list, node)
    {
      if ((info->mtime > cache_mtime) || (info->confd_mtime > cache_mtime))
	{
	  DBG_MSG ("'%s' have a later modification time than '%s'.\n",
		   info->filename, cachefile);
	  return -1;
	}
    }

  return 0;
}

rcscript_info_t *
get_rcscript_info (const char *scriptname)
{
  rcscript_info_t *info;

  if (!check_arg_str (scriptname))
    return NULL;

  list_for_each_entry (info, &rcscript_list, node)
    {
      if ((strlen (scriptname) == strlen (rc_basename (info->filename)))
	  && (0 == strncmp (scriptname, rc_basename (info->filename),
			    strlen (scriptname))))
	return info;
    }

  return NULL;
}