summaryrefslogtreecommitdiff
blob: 914f7ab6b09062d29b0253c98f1817322cf29d24 (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
/*
 * runlevels.c
 *
 * Functions dealing with runlevels.
 *
 * 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 "rcscripts.h"

static char **get_runlevel_dirs (void);

LIST_HEAD (runlevel_list);

char **
get_runlevel_dirs (void)
{
  char **dir_list = NULL;
  char **runlvl_list = NULL;
  char *dir_item;
  int count;

  dir_list = ls_dir (RUNLEVELS_DIR, 0);
  if (NULL == dir_list)
    {
      errno = ENOENT;
      DBG_MSG ("Failed to get any entries in '%' !\n", RUNLEVELS_DIR);

      return NULL;
    }

  str_list_for_each_item (dir_list, dir_item, count)
    {
      if (is_dir (dir_item, 0))
	{
	  char *tmp_str;

	  tmp_str = xstrndup (dir_item, strlen (dir_item));
	  if (NULL == tmp_str)
	    goto error;

	  str_list_add_item (runlvl_list, tmp_str, error);
	}
    }

  str_list_free (dir_list);

  if (!check_strv (runlvl_list))
    {
      if (NULL != runlvl_list)
	str_list_free (runlvl_list);
    }

  return runlvl_list;

error:
  if (NULL != dir_list)
    str_list_free (dir_list);
  if (NULL != runlvl_list)
    str_list_free (runlvl_list);

  return NULL;
}

int
get_runlevels (void)
{
  char **runlvl_list = NULL;
  char *runlevel;
  int count;

  runlvl_list = get_runlevel_dirs ();
  if (NULL == runlvl_list)
    {
      DBG_MSG ("Failed to get any runlevels\n");

      return -1;
    }

  str_list_for_each_item (runlvl_list, runlevel, count)
    {
      runlevel_info_t *runlevel_info;
      char **dir_list = NULL;
      char *dir_item;
      int dir_count;

      DBG_MSG ("Adding runlevel '%s'\n", gbasename (runlevel));

      runlevel_info = xmalloc (sizeof (runlevel_info_t));
      if (NULL == runlevel_info)
	goto error;

      runlevel_info->dirname = xstrndup (runlevel, strlen (runlevel));
      if (NULL == runlevel_info->dirname)
	goto error;

      INIT_LIST_HEAD (&runlevel_info->entries);

      dir_list = ls_dir (runlevel, 0);
      if (NULL == dir_list)
	{
	  if (0 != errno)
	    goto error;

	  goto no_entries;
	}

      str_list_for_each_item (dir_list, dir_item, dir_count)
	{
	  rcscript_info_t *script_info;
	  rcscript_info_t *new_script_info = NULL;

	  if (!is_link (dir_item))
	    {
	      DBG_MSG ("Skipping non symlink '%s' !\n", dir_item);
	      continue;
	    }

	  script_info = get_rcscript_info (gbasename (dir_item));
	  if (NULL == script_info)
	    {
	      DBG_MSG ("Skipping invalid entry '%s' !\n", dir_item);
	      continue;
	    }

	  new_script_info = xmalloc (sizeof (rcscript_info_t));
	  if (NULL == new_script_info)
	    {
	      str_list_free (dir_list);
	      goto error;
	    }

	  DBG_MSG ("Adding '%s' to runlevel '%s'\n",
		   gbasename (script_info->filename),
		   gbasename (runlevel));

	  /* Add a copy, as the next and prev pointers will be changed */
	  memcpy (new_script_info, script_info, sizeof (rcscript_info_t));
	  list_add_tail (&new_script_info->node, &runlevel_info->entries);
	}

      str_list_free (dir_list);

no_entries:
      list_add_tail (&runlevel_info->node, &runlevel_list);
    }

  str_list_free (runlvl_list);

  return 0;

error:
  if (NULL != runlvl_list)
    str_list_free (runlvl_list);

  return -1;
}

runlevel_info_t *
get_runlevel_info (const char *runlevel)
{
  runlevel_info_t *info;

  if (!check_arg_str (runlevel))
    return NULL;

  list_for_each_entry (info, &runlevel_list, node)
    {
      if ((strlen (runlevel) == strlen (gbasename (info->dirname)))
	  && (0 == strncmp (runlevel, gbasename (info->dirname),
			    strlen (runlevel))))
	return info;
    }

  return NULL;
}

bool
is_runlevel (const char *runlevel)
{
  char *runlevel_dir = NULL;
  int len;

  /* strlen (RUNLEVELS_DIR) + strlen (runlevel) + "/" + '\0' */
  len = strlen (RUNLEVELS_DIR) + strlen (runlevel) + 2;
  runlevel_dir = xmalloc (sizeof (char) * len);
  if (NULL == runlevel_dir)
    return FALSE;

  snprintf (runlevel_dir, len, "%s/%s", RUNLEVELS_DIR, runlevel);

  if (is_dir (runlevel_dir, 0))
    return TRUE;

  return FALSE;
}