chiark / gitweb /
[PATCH] fixed up udev.spec to handle selinux stuff properly now.
[elogind.git] / udev_lib.c
1 /*
2  * udev_lib - generic stuff used by udev
3  *
4  * Copyright (C) 2004 Kay Sievers <kay@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <sys/mman.h>
28
29 #include "libsysfs/sysfs/libsysfs.h"
30 #include "udev.h"
31 #include "udev_lib.h"
32
33
34 char *get_action(void)
35 {
36         char *action;
37
38         action = getenv("ACTION");
39         if (action != NULL && strlen(action) > ACTION_SIZE)
40                 action[ACTION_SIZE-1] = '\0';
41
42         return action;
43 }
44
45 char *get_devpath(void)
46 {
47         char *devpath;
48
49         devpath = getenv("DEVPATH");
50         if (devpath != NULL && strlen(devpath) > DEVPATH_SIZE)
51                 devpath[DEVPATH_SIZE-1] = '\0';
52
53         return devpath;
54 }
55
56 char *get_devnode(void)
57 {
58         char *devnode;
59
60         devnode = getenv("DEVNODE");
61         if (devnode != NULL && strlen(devnode) > NAME_SIZE)
62                 devnode[NAME_SIZE-1] = '\0';
63
64         return devnode;
65 }
66
67 char *get_seqnum(void)
68 {
69         char *seqnum;
70
71         seqnum = getenv("SEQNUM");
72
73         return seqnum;
74 }
75
76 char *get_subsystem(char *subsystem)
77 {
78         if (subsystem != NULL && strlen(subsystem) > SUBSYSTEM_SIZE)
79                 subsystem[SUBSYSTEM_SIZE-1] = '\0';
80
81         return subsystem;
82 }
83
84 int file_map(const char *filename, char **buf, size_t *bufsize)
85 {
86         struct stat stats;
87         int fd;
88
89         fd = open(filename, O_RDONLY);
90         if (fd < 0) {
91                 return -1;
92         }
93
94         if (fstat(fd, &stats) < 0) {
95                 return -1;
96         }
97
98         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
99         if (*buf == MAP_FAILED) {
100                 return -1;
101         }
102         *bufsize = stats.st_size;
103
104         close(fd);
105
106         return 0;
107 }
108
109 void file_unmap(char *buf, size_t bufsize)
110 {
111         munmap(buf, bufsize);
112 }
113
114 size_t buf_get_line(char *buf, size_t buflen, size_t cur)
115 {
116         size_t count = 0;
117
118         for (count = cur; count < buflen && buf[count] != '\n'; count++);
119
120         return count - cur;
121 }
122