summaryrefslogtreecommitdiffstats
path: root/lib/lrm/racommon.c
blob: 2670f05cd66d5e60c7719aff6eca356fd9c6890e (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
/*
 * Common functions for LRM interface to resource agents
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * File: racommon.c
 * Author: Sun Jiang Dong <sunjd@cn.ibm.com>
 * Copyright (c) 2004 International Business Machines
 *
 */


#include <lha_internal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include <libgen.h>  /* Add it for compiling on OSX */
#include <glib.h>
#include <sys/stat.h>
#include <clplumbing/cl_log.h>
#include <lrm/raexec.h>
#include <lrm/racommon.h>

void
get_ra_pathname(const char* class_path, const char* type, const char* provider,
		char pathname[])
{
	char* type_dup;
	char* base_name;

	type_dup = g_strndup(type, RA_MAX_NAME_LENGTH);
	if (type_dup == NULL) {
		cl_log(LOG_ERR, "No enough memory to allocate.");
		pathname[0] = '\0';
		return;
	}

	base_name = basename(type_dup);

	if ( strncmp(type, base_name, RA_MAX_NAME_LENGTH) == 0 ) {
		/*the type does not include path*/
		if (provider) {
			snprintf(pathname, RA_MAX_NAME_LENGTH, "%s/%s/%s",
				class_path, provider, type);
		}else{
			snprintf(pathname, RA_MAX_NAME_LENGTH, "%s/%s",
				class_path,type);
		}
	}else{
		/*the type includes path, just copy it to pathname*/
		if ( *type == '/' ) {
			g_strlcpy(pathname, type, RA_MAX_NAME_LENGTH);
		} else {
			*pathname = '\0';
			cl_log(LOG_ERR, "%s: relative paths not allowed: %s",
			__FUNCTION__, type);
		}
	}

	g_free(type_dup);
}

/*
 *    Description:   Filter a file.
 *    Return Value:
 *		     TRUE:  the file is qualified.
 *		     FALSE: the file is unqualified.
 *    Notes: A qualifed file is a regular file with execute bits
 *           which does not start with '.'
 */
gboolean
filtered(char * file_name)
{
	struct stat buf;
	char *s;

	if ( stat(file_name, &buf) != 0 ) {
		return FALSE;
	}
	if ( ((s = strrchr(file_name,'/')) && *(s+1) == '.')
			|| *file_name == '.' ) {
		return FALSE;
	}

	if (   S_ISREG(buf.st_mode)
            && (   ( buf.st_mode & S_IXUSR ) || ( buf.st_mode & S_IXGRP )
		|| ( buf.st_mode & S_IXOTH ) ) ) {
		return TRUE;
	}
	return FALSE;
}

int
get_runnable_list(const char* class_path, GList ** rsc_info)
{
	struct dirent **namelist;
	int file_num;

	if ( rsc_info == NULL ) {
		cl_log(LOG_ERR, "Parameter error: get_resource_list");
		return -2;
	}

	if ( *rsc_info != NULL ) {
		cl_log(LOG_ERR, "Parameter error: get_resource_list."\
			"will cause memory leak.");
		*rsc_info = NULL;
	}

	file_num = scandir(class_path, &namelist, NULL, alphasort);
	if (file_num < 0) {
		cl_log(LOG_ERR, "scandir failed in RA plugin");
		return -2;
	} else{
		while (file_num--) {
			char tmp_buffer[FILENAME_MAX+1];

			tmp_buffer[0] = '\0';
			tmp_buffer[FILENAME_MAX] = '\0';
			snprintf(tmp_buffer, FILENAME_MAX, "%s/%s",
				 class_path, namelist[file_num]->d_name );
			if ( filtered(tmp_buffer) == TRUE ) {
				*rsc_info = g_list_append(*rsc_info,
						g_strdup(namelist[file_num]->d_name));
			}
			free(namelist[file_num]);
		}
		free(namelist);
	}
	return g_list_length(*rsc_info);
}

int
get_failed_exec_rc(void)
{
	int rc;

	switch (errno) { /* see execve(2) */
		case ENOENT:  /* No such file or directory */
		case EISDIR:   /* Is a directory */
			rc = EXECRA_NOT_INSTALLED;
			break;
		case EACCES:   /* permission denied (various errors) */
			rc = EXECRA_INSUFFICIENT_PRIV;
			break;
		default:
			rc = EXECRA_EXEC_UNKNOWN_ERROR;
			break;
	}
	return rc;
}

void
closefiles(void)
{
	int fd;

	/* close all descriptors except stdin/out/err and channels to logd */
	for (fd = getdtablesize() - 1; fd > STDERR_FILENO; fd--) {
		/*if (!cl_log_is_logd_fd(fd))*/
			close(fd);
	}
}