summaryrefslogtreecommitdiffstats
path: root/file-ops.c
blob: ffba8e80bc95e1c1cdad66810720b12d1136948b (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
/*
 * Copyright IBM Corporation. 2007
 *
 * Authors:	Balbir Singh <balbir@linux.vnet.ibm.com>
 *		Dhaval Giani <dhaval@linux.vnet.ibm.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2.1 of the GNU Lesser General Public License
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#include <dirent.h>
#include <errno.h>
#include <fts.h>
#include <grp.h>
#include <libcgroup.h>
#include <limits.h>
#include <pwd.h>
#include <search.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>

extern const char cg_filesystem[];
extern struct mount_table *mount_table;

int cg_chown_file(FTS *fts, FTSENT *ent, uid_t owner, gid_t group)
{
	int ret = 1;
	const char *filename = fts->fts_path;
	dbg("seeing file %s\n", filename);
	switch (ent->fts_info) {
	case FTS_ERR:
		errno = ent->fts_errno;
		break;
	case FTS_D:
	case FTS_DC:
	case FTS_NSOK:
	case FTS_NS:
	case FTS_DNR:
	case FTS_DP:
	case FTS_F:
	case FTS_DEFAULT:
		ret = chown(filename, owner, group);
		break;
	}
	return ret;
}

/*
 * TODO: Need to decide a better place to put this function.
 */
int cg_chown_recursive(char *path[], uid_t owner, gid_t group)
{
	int ret = 1;
	FTS *fts = fts_open((char **)path, FTS_PHYSICAL | FTS_NOCHDIR |
				FTS_NOSTAT, NULL);
	while (1) {
		FTSENT *ent;
		ent = fts_read(fts);
		if (!ent) {
			dbg("fts_read failed\n");
			break;
		}
		cg_chown_file(fts, ent, owner, group);
	}
	fts_close(fts);
	return ret;
}

char *cg_build_group_path(struct cg_group *cg_group,
					struct mount_table *mount_info)
{
	char *group_path;

	group_path = malloc(strlen(mount_info->mount_point) +
						strlen(cg_group->name) + 2);
	if (!group_path)
		return NULL;
	strncpy(group_path, mount_info->mount_point,
		strlen(mount_info->mount_point) + 1);
	dbg("group path is %s\n", group_path);
	strncat(group_path, "/", strlen("/"));
	strncat(group_path, cg_group->name, strlen(cg_group->name));
	dbg("group path is %s\n", group_path);
	return group_path;
}

int cg_make_directory(struct cg_group *cg_group, char *group_path[])
{
	int ret;
	ret = mkdir(group_path[0], S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
	if (ret < 0)
		return 0;
	/*
	 * Recursively change all files under the directory
	 */
	ret = cg_chown_recursive(group_path, cg_group->admin_uid,
						cg_group->admin_gid);
	return ret;
}

/*
 * After having parsed the configuration file, walk through the mount_table
 * and mount the specified controllers. This routine might create directories
 * specified as mount_point.
 */
int cg_mount_controllers(void)
{
	int ret;
	struct mount_table *curr = mount_table;
	struct stat buf;

	while (curr) {
		ret = stat(curr->mount_point, &buf);
		if (ret < 0 && errno != ENOENT)
			return 0;

		/*
		 * Check if path needs to be created before mounting
		 */
		if (errno == ENOENT) {
			ret = mkdir(curr->mount_point, S_IRWXU | S_IRWXG |
					S_IRWXO | S_ISVTX);
			if (ret < 0)
				return 0;
		} else if (!S_ISDIR(buf.st_mode)) {
			errno = ENOTDIR;
			return 0;
		}

		ret = mount(cg_filesystem, curr->mount_point,
					cg_filesystem, 0, curr->options);

		if (ret < 0)
			return 0;
		curr = curr->next;
	}
	return 1;
}

/*
 * Called during end of WLM configuration to unmount all controllers or
 * on failure, to cleanup mounted controllers
 */
int cg_unmount_controllers(void)
{
	struct mount_table *curr = mount_table;
	int ret;

	while (curr) {
		/*
		 * We ignore failures and ensure that all mounted
		 * containers are unmounted
		 */
		ret = umount(curr->mount_point);
		if (ret < 0)
			printf("Unmount failed\n");
		ret = rmdir(curr->mount_point);
		if (ret < 0)
			printf("rmdir failed\n");
		curr = curr->next;
	}
	return 1;
}