summaryrefslogtreecommitdiff
path: root/libattr/attr.c
blob: f076b671aa84f183a3baf45c9adc537f83261ebe (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU 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.
 * 
 * Further, this software is distributed without any warranty that it is
 * free of the rightful claim of any third person regarding infringement
 * or the like.  Any license provided herein, whether implied or
 * otherwise, applies only to this software file.  Patent licenses, if
 * any, provided herein do not apply to combinations of this program with
 * other software, or any other product whatsoever.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write the Free Software Foundation, Inc., 59
 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
 * 
 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
 * Mountain View, CA  94043, or:
 * 
 * http://www.sgi.com 
 * 
 * For further information regarding this notice, see: 
 * 
 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
 */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <asm/types.h>

#include <attributes.h>
#include <attr_kern.h>


/* Experimental system call interface for Linux */
static int attrctl(attr_obj_t, int, attr_op_t *, int);

/* Other local function prototypes */
static int _attr_get(attr_obj_t, int, const char *, char *, int *, int);
static int _attr_set(attr_obj_t, int, const char *, const char *, const int, int);
static int _attr_remove(attr_obj_t, int, const char *, int);
static int _attr_listf(attr_obj_t, int, char *, const int, int,
		       attrlist_cursor_t *);
static int _attr_multif(attr_obj_t, int, attr_multiop_t *, int, int);


/*
 * Get the value of an attribute.
 */
int
attr_get(const char *path, const char *attrname, char *attrvalue,
	 int *valuelength, int flags )
{
	attr_obj_t obj;
	int follow;
	obj.path = (char *) path;
	follow = (flags & ATTR_DONTFOLLOW) ? ATTR_TYPE_LPATH : ATTR_TYPE_PATH;
	return _attr_get(obj, follow, attrname, attrvalue, valuelength, flags);
}

int
attr_getf(int fd, const char *attrname, char *attrvalue,
	 int *valuelength, int flags )
{
	attr_obj_t obj;
	obj.fd = fd;
	return _attr_get(obj, ATTR_TYPE_FD, attrname, attrvalue,
			 valuelength, flags);
}

static int
_attr_get(attr_obj_t obj, int type, const char *attrname, char *attrvalue,
	  int *valuelength, int flags)
{
	attr_op_t op;
	memset(&op, 0, sizeof(attr_op_t));
	op.opcode = ATTR_OP_GET;
	op.name = (char *) attrname;
	op.value = (char *) attrvalue;	/* buffer to fill */
	op.length = *valuelength;	/* buffer size */
	op.flags = flags;
	
	if (attrctl(obj, type, &op, 1) < 0)
		return -1;
	errno = op.error;
	*valuelength = op.length;
	return (errno ? -1 : 0);
}


/*
 * Set the value of an attribute, creating the attribute if necessary.
 */
int
attr_set(const char *path, const char *attrname, const char *attrvalue,
	 const int valuelength, int flags)
{
	attr_obj_t obj;
	int follow;
	obj.path = (char *) path;
	follow = (flags & ATTR_DONTFOLLOW) ? ATTR_TYPE_LPATH : ATTR_TYPE_PATH;
	return _attr_set(obj, follow, attrname, attrvalue, valuelength, flags);
}

int
attr_setf(int fd, const char *attrname,
	  const char *attrvalue, const int valuelength, int flags)
{
	attr_obj_t obj;
	obj.fd = fd;
	return _attr_set(obj, ATTR_TYPE_FD, attrname, attrvalue,
			 valuelength, flags);
}

static int
_attr_set(attr_obj_t obj, int type, const char *attrname,
	 const char *attrvalue, const int valuelength, int flags)
{
	attr_op_t op;
	memset(&op, 0, sizeof(attr_op_t));
	op.opcode = ATTR_OP_SET;
	op.name = (char *) attrname;
	op.value = (char *) attrvalue;
	op.length = valuelength;
	op.flags = flags;

	if (attrctl(obj, type, &op, 1) < 0)
		return -1;
	errno = op.error;
	return (errno ? -1 : 0);
}


/*
 * Remove an attribute.
 */
int
attr_remove(const char *path, const char *attrname, int flags)
{
	attr_obj_t obj;
	int follow;
	obj.path = (char *) path;
	follow = (flags & ATTR_DONTFOLLOW) ? ATTR_TYPE_LPATH : ATTR_TYPE_PATH;
	return _attr_remove(obj, follow, attrname, flags);
}

int
attr_removef(int fd, const char *attrname, int flags)
{
	attr_obj_t obj;
	obj.fd = fd;
	return _attr_remove(obj, ATTR_TYPE_FD, attrname, flags);
}

static int
_attr_remove(attr_obj_t obj, int type, const char *attrname, int flags)
{
	attr_op_t op;
	memset(&op, 0, sizeof(attr_op_t));
	op.opcode = ATTR_OP_REMOVE;
	op.name = (char *) attrname;
	op.flags = flags;

	if (attrctl(obj, type, &op, 1) < 0)
		return -1;
	errno = op.error;
	return (errno ? -1 : 0);
}


/*
 * List the names and sizes of the values of all the attributes of an object.
 */
int
attr_list(const char *path, char *buffer, const int buffersize,
	  int flags, attrlist_cursor_t *cursor)
{
	attr_obj_t obj;
	int follow;
	obj.path = (char *) path;
	follow = (flags & ATTR_DONTFOLLOW) ? ATTR_TYPE_LPATH : ATTR_TYPE_PATH;
	return _attr_listf(obj, follow, buffer, buffersize, flags, cursor);
}

int
attr_listf(int fd, char *buffer, const int buffersize,
	   int flags, attrlist_cursor_t *cursor)
{
	attr_obj_t obj;
	obj.fd = fd;
	return _attr_listf(obj, ATTR_TYPE_FD, buffer, buffersize,
			   flags, cursor);
}

static int
_attr_listf(attr_obj_t obj, int type, char *buffer, const int buffersize,
	    int flags, attrlist_cursor_t *cursor)
{
	attr_op_t op;
	memset(&op, 0, sizeof(attr_op_t));
	op.opcode = ATTR_OP_IRIX_LIST;
	op.value = (char *) buffer;
	op.length = buffersize;
	op.flags = flags;
	op.aux = cursor;

	if (attrctl(obj, type, &op, 1) < 0)
		return -1;
	errno = op.error;
	return (errno ? -1 : 0);
}

/*
 * Operate on multiple attributes of the same object simultaneously
 */
int
attr_multi(const char *path, attr_multiop_t *multiops, int count, int flags)
{
	attr_obj_t obj;
	int follow;
	obj.path = (char *) path;
	follow = (flags & ATTR_DONTFOLLOW) ? ATTR_TYPE_LPATH : ATTR_TYPE_PATH;
	return _attr_multif(obj, follow, multiops, count, flags);
}

int
attr_multif(int fd, attr_multiop_t *multiops, int count, int flags)
{
	attr_obj_t obj;
	obj.fd = fd;
	return _attr_multif(obj, ATTR_TYPE_FD, multiops, count, flags);
}

static int
_attr_multif(attr_obj_t obj, int type, attr_multiop_t *multiops, int count,
	     int flags)
{
	int		i;
	int		error = 0;
	attr_op_t	*ops;

	/* From the manpage: "attr_multi will fail if ... A bit other than */
	/* ATTR_DONTFOLLOW was set in the flag argument." flags must be */
	/* checked here as they are not passed into the kernel. */
	/* All other flags are checked in the kernel (linvfs_attrctl). */

	if ((flags & ATTR_DONTFOLLOW) != flags) {
		errno = EINVAL;
		return -1;
	}

	if (! (ops = malloc(count * sizeof (attr_op_t)))) {
		errno = ENOMEM;
		return -1;
	}

	for (i = 0; i < count; i++) {
		ops[i].opcode = multiops[i].am_opcode;
		ops[i].name = multiops[i].am_attrname;
		ops[i].value = multiops[i].am_attrvalue;
		ops[i].length = multiops[i].am_length;
		ops[i].flags = multiops[i].am_flags;
	}

	if (attrctl(obj, type, ops, count) < 0) {
		error = -1;
		goto free_mem;
	}

	/* copy return vals */
	for (i = 0; i < count; i++) {
		multiops[i].am_error = ops[i].error;
		if (ops[i].opcode == ATTR_OP_GET)
			multiops[i].am_length = ops[i].length;
	}

 free_mem:
	free(ops);
	return error;
}


/* 
 * attrctl(2) experimental system call function definition.
 */

#if __i386__
#  define HAVE_ACL_SYSCALL 1
#  ifndef SYS__attrctl
#    define SYS__attrctl       250
#  endif
#elif __ia64__
#  define HAVE_ACL_SYSCALL 1
#  ifndef SYS__attrctl
#    define SYS__attrctl       1260
#  endif
#else
#  define HAVE_ACL_SYSCALL 0
#endif

static int
attrctl(attr_obj_t obj, int type, attr_op_t *ops, int count)
{
#if HAVE_ACL_SYSCALL
	return syscall(SYS__attrctl, obj, type, ops, count);
#else
	errno = ENOSYS;
	return -1;
#endif
}