~ubuntu-branches/ubuntu/raring/rtkit/raring-security

« back to all changes in this revision

Viewing changes to rtkit-daemon.c

  • Committer: Bazaar Package Importer
  • Author(s): Luke Yelavich
  • Date: 2009-08-05 09:59:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090805095935-i4o105n5cfj9oyly
Tags: 0.4-0ubuntu1
* New upstream release
* 01_rename_user_struct.patch: Dropped, applied upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
183
183
        struct process *next;
184
184
};
185
185
 
186
 
struct user {
 
186
struct rtkit_user {
187
187
        uid_t uid;
188
188
 
189
189
        time_t timestamp;
193
193
        unsigned n_processes;
194
194
        unsigned n_threads;
195
195
 
196
 
        struct user *next;
 
196
        struct rtkit_user *next;
197
197
};
198
198
 
199
 
static struct user *users = NULL;
 
199
static struct rtkit_user *users = NULL;
200
200
static unsigned n_users = 0;
201
201
static unsigned n_total_processes = 0;
202
202
static unsigned n_total_threads = 0;
229
229
}
230
230
 
231
231
static int read_starttime(pid_t pid, pid_t tid, unsigned long long *st) {
232
 
        char fn[128];
 
232
        char fn[128], line[256], *p;
233
233
        int r;
234
234
        FILE *f;
235
235
 
239
239
                assert_se(snprintf(fn, sizeof(fn)-1, "%s/%llu/stat", get_proc_path(), (unsigned long long) pid) < (int) (sizeof(fn)-1));
240
240
        fn[sizeof(fn)-1] = 0;
241
241
 
242
 
        if (!(f = fopen(fn, "r"))) {
 
242
        if (!(f = fopen(fn, "r")))
 
243
                return -errno;
 
244
 
 
245
        if (!(fgets(line, sizeof(line), f))) {
243
246
                r = -errno;
 
247
                fclose(f);
244
248
                return r;
245
249
        }
246
250
 
247
 
        if (fscanf(f,
248
 
                   "%*d "  /* pid */
249
 
                   "%*s "  /* comm */
 
251
        fclose(f);
 
252
 
 
253
        /* Let's skip the pid and comm fields. The latter is enclosed
 
254
         * in () but does not escape any () in its value, so let's
 
255
         * skip over it manually */
 
256
 
 
257
        if (!(p = strrchr(line, ')')))
 
258
                return -EIO;
 
259
 
 
260
        p++;
 
261
 
 
262
        if (sscanf(p, " "
250
263
                   "%*c "  /* state */
251
264
                   "%*d "  /* ppid */
252
265
                   "%*d "  /* pgrp */
267
280
                   "%*d "  /* num_threads */
268
281
                   "%*d "  /* itrealvalue */
269
282
                   "%llu "  /* starttime */,
270
 
                   st) != 1) {
271
 
                fclose(f);
 
283
                   st) != 1)
272
284
                return -EIO;
273
 
        }
274
285
 
275
 
        fclose(f);
276
286
        return 0;
277
287
}
278
288
 
291
301
        free(p);
292
302
}
293
303
 
294
 
static void free_user(struct user *u) {
 
304
static void free_user(struct rtkit_user *u) {
295
305
        struct process *p;
296
306
 
297
307
        while ((p = u->processes)) {
302
312
        free(u);
303
313
}
304
314
 
305
 
static bool user_in_burst(struct user *u) {
 
315
static bool user_in_burst(struct rtkit_user *u) {
306
316
        time_t now = time(NULL);
307
317
 
308
318
        return now < u->timestamp + actions_burst_sec;
309
319
}
310
320
 
311
 
static bool verify_burst(struct user *u) {
 
321
static bool verify_burst(struct rtkit_user *u) {
312
322
 
313
323
        if (!user_in_burst(u)) {
314
324
                /* Restart burst phase */
327
337
        return true;
328
338
}
329
339
 
330
 
static int find_user(struct user **_u, uid_t uid) {
331
 
        struct user *u;
 
340
static int find_user(struct rtkit_user **_u, uid_t uid) {
 
341
        struct rtkit_user *u;
332
342
 
333
343
        for (u = users; u; u = u->next)
334
344
                if (u->uid == uid) {
341
351
                return -EBUSY;
342
352
        }
343
353
 
344
 
        if (!(u = malloc(sizeof(struct user))))
 
354
        if (!(u = malloc(sizeof(struct rtkit_user))))
345
355
                return -ENOMEM;
346
356
 
347
357
        u->uid = uid;
357
367
        return 0;
358
368
}
359
369
 
360
 
static int find_process(struct process** _p, struct user *u, pid_t pid, unsigned long long starttime) {
 
370
static int find_process(struct process** _p, struct rtkit_user *u, pid_t pid, unsigned long long starttime) {
361
371
        struct process *p;
362
372
 
363
373
        for (p = u->processes; p; p = p->next)
387
397
        return 0;
388
398
}
389
399
 
390
 
static int find_thread(struct thread** _t, struct user *u, struct process *p, pid_t pid, unsigned long long starttime) {
 
400
static int find_thread(struct thread** _t, struct rtkit_user *u, struct process *p, pid_t pid, unsigned long long starttime) {
391
401
        struct thread *t;
392
402
 
393
403
        for (t = p->threads; t; t = t->next)
472
482
        return FALSE;
473
483
}
474
484
 
475
 
static void thread_gc(struct user *u, struct process *p) {
 
485
static void thread_gc(struct rtkit_user *u, struct process *p) {
476
486
        struct thread *t, *n, *l;
477
487
 
478
488
        /* Cleanup dead theads of a specific user we don't need to keep track about anymore */
499
509
        assert(!p->threads || u->n_threads);
500
510
}
501
511
 
502
 
static void process_gc(struct user *u) {
 
512
static void process_gc(struct rtkit_user *u) {
503
513
        struct process *p, *n, *l;
504
514
 
505
515
        /* Cleanup dead processes of a specific user we don't need to keep track about anymore */
528
538
}
529
539
 
530
540
static void user_gc(void) {
531
 
        struct user *u, *n, *l;
 
541
        struct rtkit_user *u, *n, *l;
532
542
 
533
543
        /* Cleanup all users we don't need to keep track about anymore */
534
544
 
637
647
        return good ? 0 : -EPERM;
638
648
}
639
649
 
640
 
static int verify_process_user(struct user *u, struct process *p) {
 
650
static int verify_process_user(struct rtkit_user *u, struct process *p) {
641
651
        char fn[128];
642
652
        int r;
643
653
        struct stat st;
726
736
        return exe;
727
737
}
728
738
 
729
 
static int process_set_realtime(struct user *u, struct process *p, struct thread *t, unsigned priority) {
 
739
static int process_set_realtime(struct rtkit_user *u, struct process *p, struct thread *t, unsigned priority) {
730
740
        int r;
731
741
        struct sched_param param;
732
742
        char user[64], exe[128];
796
806
        return r;
797
807
}
798
808
 
799
 
static int process_set_high_priority(struct user *u, struct process *p, struct thread *t, int priority) {
 
809
static int process_set_high_priority(struct rtkit_user *u, struct process *p, struct thread *t, int priority) {
800
810
        int r;
801
811
        struct sched_param param;
802
812
        char user[64], exe[128];
861
871
}
862
872
 
863
873
static void reset_known(void) {
864
 
        struct user *u;
 
874
        struct rtkit_user *u;
865
875
        struct process *p;
866
876
        struct thread *t;
867
877
 
1024
1034
}
1025
1035
 
1026
1036
static int lookup_client(
1027
 
                struct user **_u,
 
1037
                struct rtkit_user **_u,
1028
1038
                struct process **_p,
1029
1039
                struct thread **_t,
1030
1040
                DBusConnection *c,
1035
1045
        int r;
1036
1046
        unsigned long pid, uid;
1037
1047
        unsigned long long starttime;
1038
 
        struct user *u;
 
1048
        struct rtkit_user *u;
1039
1049
        struct process *p;
1040
1050
        struct thread *t;
1041
1051
 
1115
1125
        return -EIO;
1116
1126
}
1117
1127
 
1118
 
static int verify_polkit(DBusConnection *c, struct user *u, struct process *p, const char *action) {
 
1128
static int verify_polkit(DBusConnection *c, struct rtkit_user *u, struct process *p, const char *action) {
1119
1129
        DBusError error;
1120
1130
        DBusMessage *m = NULL, *r = NULL;
1121
1131
        const char *unix_process = "unix-process";
1236
1246
 
1237
1247
                uint64_t thread;
1238
1248
                uint32_t priority;
1239
 
                struct user *u;
 
1249
                struct rtkit_user *u;
1240
1250
                struct process *p;
1241
1251
                struct thread *t;
1242
1252
                int ret;
1278
1288
 
1279
1289
                uint64_t thread;
1280
1290
                int32_t priority;
1281
 
                struct user *u;
 
1291
                struct rtkit_user *u;
1282
1292
                struct process *p;
1283
1293
                struct thread *t;
1284
1294
                int ret;
2147
2157
int main(int argc, char *argv[]) {
2148
2158
        DBusConnection *bus = NULL;
2149
2159
        int ret = 1;
2150
 
        struct user *u;
 
2160
        struct rtkit_user *u;
2151
2161
 
2152
2162
        if (parse_command_line(argc, argv, &ret) <= 0)
2153
2163
                goto finish;