summaryrefslogtreecommitdiffstats
path: root/urunlevel/runlevel/install_initd.c
blob: 270e4d0bf045f29690697c2147f9b118a46f1f82 (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
/*
 * Mini install_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 install_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 *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);
	}

	if (!in_init) {
		bb_xasprintf(&info_text, "/etc/init.d/%s", bb_get_last_path_component(pathname));
		symlink(pathname, info_text);
		free(info_text);
	}

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

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

	    if (this_one) {
		if (strcmp(this_one->path, info->path) == 0) {
		    result = EXIT_FAILURE;
		    free(info_text);
		    bb_xasprintf(&info_text, "/etc/init.d/%s", bb_get_last_path_component(pathname));
		    remove_file(info_text, FILEUTILS_FORCE);
		    break;
		}
	    }
	    current = current->link;
	}

    if (result == EXIT_FAILURE)
	bb_error_msg("Unable to install 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;
}