summaryrefslogtreecommitdiff
path: root/sysutils/policykit/files/patch-src_polkit_polkit-sysdeps.c
blob: 147cca500c462d43c1f152d4145650b0c600503f (plain) (blame)
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
--- src/polkit/polkit-sysdeps.c.orig	2007-11-28 16:26:14.000000000 -0500
+++ src/polkit/polkit-sysdeps.c	2008-03-28 14:36:50.000000000 -0400
@@ -39,7 +39,6 @@
 #include <grp.h>
 #include <unistd.h>
 #include <errno.h>
-#include <sys/inotify.h>
 #include <syslog.h>
 
 #include "polkit-sysdeps.h"
@@ -82,7 +81,11 @@ polkit_sysdeps_get_start_time_for_pid (p
         start_time = 0;
         contents = NULL;
 
+#ifdef __FreeBSD__
+        filename = kit_strdup_printf ("/proc/%d/status", pid);
+#else
         filename = kit_strdup_printf ("/proc/%d/stat", pid);
+#endif
         if (filename == NULL) {
                 errno = ENOMEM;
                 goto out;
@@ -93,6 +96,36 @@ polkit_sysdeps_get_start_time_for_pid (p
                 goto out;
         }
 
+#ifdef __FreeBSD__
+        tokens = kit_strsplit (contents, ' ', &num_tokens);
+        if (tokens == NULL)
+                goto out;
+        if (num_tokens < 8) {
+                kit_strfreev (tokens);
+                goto out;
+        }
+
+        p = kit_strdup (tokens[7]);
+        kit_strfreev (tokens);
+
+        tokens = kit_strsplit (p, ',', &num_tokens);
+        kit_free (p);
+        if (tokens == NULL)
+                goto out;
+        if (num_tokens >= 1) {
+                start_time = strtoll (tokens[0], &endp, 10);
+                if (endp == tokens[0]) {
+                        kit_strfreev (tokens);
+                        goto out;
+                }
+        } else {
+                kit_strfreev (tokens);
+                goto out;
+        }
+
+        kit_strfreev (tokens);
+#else
+
         /* start time is the 19th token after the '(process name)' entry */
 
         p = strchr (contents, ')');
@@ -118,6 +151,7 @@ polkit_sysdeps_get_start_time_for_pid (p
         }
 
         kit_strfreev (tokens);
+#endif
 
 out:
         kit_free (filename);
@@ -153,7 +187,11 @@ polkit_sysdeps_get_exe_for_pid (pid_t pi
 
         ret = 0;
 
+#ifdef __FreeBSD__
+        snprintf (proc_name, sizeof (proc_name), "/proc/%d/file", pid);
+#else
         snprintf (proc_name, sizeof (proc_name), "/proc/%d/exe", pid);
+#endif
         ret = readlink (proc_name, out_buf, buf_size - 1);
         if (ret == -1) {
                 strncpy (out_buf, "(unknown)", buf_size);
@@ -166,6 +204,108 @@ out:
         return ret;
 }
 
+#ifndef HAVE_GETLINE
+/* Taken from GNU sed. */
+/* Read up to (and including) a '\n' from STREAM into *LINEPTR
+   (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
+   NULL), pointing to *N characters of space.  It is realloc'd as
+   necessary.  Returns the number of characters read (not including the
+   null terminator), or -1 on error or EOF.  */
+
+ssize_t
+polkit_sysdeps_getline (char **lineptr, size_t *n, FILE *stream)
+{
+  char *line, *p;
+  long size, copy;
+
+  if (lineptr == NULL || n == NULL) {
+          errno = EINVAL;
+          return (ssize_t) -1;
+  }
+
+  if (ferror (stream))
+          return (ssize_t) -1;
+
+  /* Make sure we have a line buffer to start with.  */
+  if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars.  */ {
+#ifndef        MAX_CANON
+#define        MAX_CANON        256
+#endif
+          if (!*lineptr)
+                  line = (char *) malloc (MAX_CANON);
+          else
+                  line = (char *) realloc (*lineptr, MAX_CANON);
+          if (line == NULL)
+                  return (ssize_t) -1;
+          *lineptr = line;
+          *n = MAX_CANON;
+  }
+
+  line = *lineptr;
+  size = *n;
+
+  copy = size;
+  p = line;
+
+  while (1) {
+          long len;
+
+          while (--copy > 0) {
+                  int c = getc (stream);
+
+                  if (c == EOF)
+                          goto lose;
+                  else if ((*p++ = c) == '\n')
+                          goto win;
+          }
+
+          /* Need to enlarge the line buffer.  */
+          len = p - line;
+          size *= 2;
+          line = (char *) realloc (line, size);
+          if (line == NULL)
+                  goto lose;
+          *lineptr = line;
+          *n = size;
+          p = line + len;
+          copy = size - len;
+  }
+
+lose:
+  if (p == *lineptr)
+          return (ssize_t) -1;
+
+  /* Return a partial line since we got an error in the middle.  */
+win:
+  *p = '\0';
+  return p - *lineptr;
+}
+#else
+ssize_t
+polkit_sysdeps_getline (char **lineptr, size_t *n, FILE *stream)
+{
+        return getline (lineptr, n, stream);
+}
+#endif
+
+#ifndef HAVE_CLEARENV
+extern char **environ;
+int
+polkit_sysdeps_clearenv (void)
+{
+	if (environ != NULL) {
+        	environ[0] = NULL;
+	}
+        return 0;
+}
+#else
+int
+polkit_sysdeps_clearenv (void)
+{
+        return clearenv ();
+}
+#endif
+
 #ifdef POLKIT_BUILD_TESTS
 
 static polkit_bool_t