[BACK]Return to cap.c CVS log [TXT][DIR] Up to [local] / prex-old / usr / test / cap

Annotation of prex-old/usr/test/cap/cap.c, Revision 1.1.1.1

1.1       nbrk        1: /*-
                      2:  * Copyright (c) 2005-2006, Kohsuke Ohtani
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. Neither the name of the author nor the names of any co-contributors
                     14:  *    may be used to endorse or promote products derived from this software
                     15:  *    without specific prior written permission.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27:  * SUCH DAMAGE.
                     28:  */
                     29:
                     30: /*
                     31:  * cap.c - test program for task capability.
                     32:  */
                     33:
                     34: #include <prex/prex.h>
                     35: #include <prex/capability.h>
                     36:
                     37: #include <stdio.h>
                     38:
                     39: static task_t task;
                     40: static cap_t cap;
                     41:
                     42: static void
                     43: cap_get(void)
                     44: {
                     45:        int err;
                     46:
                     47:        if ((err = task_getcap(task, &cap)) != 0) {
                     48:                printf("err=%d\n", err);
                     49:                panic("cap: failed to get capability\n");
                     50:        }
                     51:        printf(" capability=%x\n", cap);
                     52: }
                     53:
                     54: static void
                     55: cap_remove(int c)
                     56: {
                     57:        int err;
                     58:
                     59:        cap &= ~c;
                     60:        if ((err = task_setcap(task, &cap)) != 0) {
                     61:                printf("err=%d\n", err);
                     62:                panic("cap: failed to change capability\n");
                     63:        }
                     64: }
                     65:
                     66: int
                     67: main(int argc, char *argv[])
                     68: {
                     69:        task_t child;
                     70:        thread_t th;
                     71:        int err;
                     72:
                     73:        printf("cap - test program for capability\n");
                     74:
                     75:        task = task_self();
                     76:        printf("task=%x\n", (u_int)task);
                     77:
                     78:        if (task_create(task, VM_NEW, &child) != 0)
                     79:                panic("failed to create task");
                     80:
                     81:        if (thread_create(child, &th) != 0)
                     82:                panic("failed to create thread");
                     83:
                     84:        printf("Get capability\n");
                     85:        cap_get();
                     86:
                     87:        /*
                     88:         * Test CAP_TASK
                     89:         */
                     90:        printf("Set task name\n");
                     91:        if ((err = task_name(child, "foo")) != 0)
                     92:                panic("failed to set task name\n");
                     93:
                     94:        printf("\nRemove CAP_TASK\n");
                     95:        cap_remove(CAP_TASK);
                     96:        cap_get();
                     97:        printf(" - OK!\n");
                     98:
                     99:        printf("Set task name\n");
                    100:        err = task_name(child, "foo");
                    101:        if (err)
                    102:                printf("task_name() returns error=%d\n", err);
                    103:        else
                    104:                panic("task_name() must return error");
                    105:
                    106:
                    107:        /*
                    108:         * Test CAP_NICE
                    109:         */
                    110:        printf("Set priority\n");
                    111:        err = thread_setprio(th, 199);
                    112:        if (err)
                    113:                panic("failed to set priority\n");
                    114:
                    115:        printf("\nRemove CAP_NICE\n");
                    116:        cap_remove(CAP_NICE);
                    117:        cap_get();
                    118:        printf(" - OK!\n");
                    119:
                    120:        printf("Set priority\n");
                    121:        err = thread_setprio(th, 199);
                    122:        if (err)
                    123:                printf("thread_setprio() returns error=%d\n", err);
                    124:        else
                    125:                panic("thread_setprio() must return error");
                    126:
                    127:
                    128:        /*
                    129:         * Test CAP_SETPCAP
                    130:         */
                    131:        printf("\nRemove CAP_SETPCAP\n");
                    132:        cap_remove(CAP_SETPCAP);
                    133:        cap_get();
                    134:        printf(" - OK!\n");
                    135:
                    136:        printf("\nTest CAP_SETPCAP\n");
                    137:
                    138:        /* This cause panic if it finishes successfully. */
                    139:        cap_remove(CAP_SETPCAP);
                    140:        cap_get();
                    141:
                    142:        printf(" - Oops!\n");
                    143:        return 0;
                    144: }

CVSweb