summaryrefslogtreecommitdiffstats
path: root/urunlevel/runlevel/remove_initd.c
blob: 1adda109de555ea77911d7b52759fcb07af93ba3 (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
/*
 * Mini remove_initd implementation for busybox.
 *
 * Copyright (C) 2005 by David Seikel won_fang@yahoo.com.au
 *
 * Clean room implementation of LSB init.d specs.
 * I didn't steal any of this, honest B-).
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA
 *
 */

#include <string.h>
#include <unistd.h>

#include "busybox.h"
#include "lib_init_d.h"


int remove_initd_main(int argc, char **argv)
{
    int result = EXIT_FAILURE;

    argv += optind;
    if (!*argv)
	bb_show_usage();
    else {
	int in_init = 0;
	llist_t *current = NULL;
	llist_t *previous = NULL;
	llist_t *unsorted = get_scripts();
	init_d_info_t *info = NULL;
	char *info_text = NULL;
	char *pathname = NULL;
	RESERVE_CONFIG_BUFFER(resolved_path, PATH_MAX);

	bb_xasprintf(&pathname, "%s", *argv);
	if ((*argv)[0] != '/') {
	    char *cwd = xgetcwd(NULL);
	    
	    if (cwd) {
		free(pathname);
		bb_xasprintf(&pathname, "%s/%s", cwd, *argv);
	    }
	}

	if (realpath(pathname, resolved_path) != NULL) {
	    if (strncmp("/etc/init.d/", resolved_path, 12) == 0)
		in_init = 1;
	} else {
	    bb_perror_msg("%s", pathname);
	}

	info_text = get_init_info(pathname);
	info = parse_init_info(info_text, bb_get_last_path_component(pathname));

	current = unsorted;
	while (current) {
	    init_d_info_t *this_one = (init_d_info_t *) current->data;

	    if (strcmp(this_one->path, info->path) == 0) {
		current = llist_delete(&unsorted, previous, current);
		break;
	    }
	    previous = current;
	    current = current->link;
	}

	sort_scripts(unsorted, NULL);
	result = EXIT_SUCCESS;
	current = unsorted;
	while (current) {
	    int k;
	    init_d_info_t *this_one = (init_d_info_t *) current->data;

	    if (this_one) {
		for (k = 0; k < info->sizes[0]; k++) {
		    int i;

		    for (i = 0; i < this_one->sizes[1]; i++) {
			if (strcmp(this_one->reqstart[i], info->provides[k]) == 0) {
			    result = EXIT_FAILURE;
			    bb_error_msg("%s is required by %s.", pathname, this_one->path);
			    break;
			}
		    }
		    if (result == EXIT_FAILURE)
			break;
		}
	    }

	    current = current->link;
	}

	/* This is not LSB compliant, the LSB package manager is supposed to remove it, but...
	 * We may have created it, so we should remove it.
	 * LSB says that init.d scripts are "configuration files", I disagree, they are executables.
	 * LSB further says that config files are left behind after package removal.  This is a
	 * contradiction with "package manager is supposed to remove it".
	 *
	 * Safest thing to do is zap it.
	 */
	if ((in_init) && (result == EXIT_SUCCESS)) {
	    free(info_text);
	    bb_xasprintf(&info_text, "/etc/init.d/%s", bb_get_last_path_component(pathname));
	    remove_file(info_text, FILEUTILS_FORCE);
	}

    if (result == EXIT_FAILURE)
	bb_error_msg("Unable to remove the %s init.d script.", pathname);

#ifdef CONFIG_FEATURE_CLEAN_UP
	RELEASE_CONFIG_BUFFER(resolved_path);
#endif
	free(info);
	free(pathname);
	free(info_text);
    }

    return result;
}