| 3007 | | /* lock paged memory if needed */ |
|---|
| 3008 | | if (lock_memory) { |
|---|
| 3009 | | #ifdef HAVE_MLOCKALL |
|---|
| 3010 | | int res = mlockall(MCL_CURRENT | MCL_FUTURE); |
|---|
| 3011 | | if (res != 0) { |
|---|
| 3012 | | fprintf(stderr, "warning: -k invalid, mlockall() failed: %s\n", |
|---|
| 3013 | | strerror(errno)); |
|---|
| 3014 | | } |
|---|
| 3015 | | #else |
|---|
| 3016 | | fprintf(stderr, "warning: -k invalid, mlockall() not supported on this platform. proceeding without.\n"); |
|---|
| 3017 | | #endif |
|---|
| 3018 | | } |
|---|
| 3019 | | |
|---|
| 3020 | | /* lose root privileges if we have them */ |
|---|
| 3021 | | if (getuid() == 0 || geteuid() == 0) { |
|---|
| 3022 | | if (username == 0 || *username == '\0') { |
|---|
| 3023 | | fprintf(stderr, "can't run as root without the -u switch\n"); |
|---|
| 3024 | | return 1; |
|---|
| 3025 | | } |
|---|
| 3026 | | if ((pw = getpwnam(username)) == 0) { |
|---|
| 3027 | | fprintf(stderr, "can't find the user %s to switch to\n", username); |
|---|
| 3028 | | return 1; |
|---|
| 3029 | | } |
|---|
| 3030 | | if (setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) { |
|---|
| 3031 | | fprintf(stderr, "failed to assume identity of user %s\n", username); |
|---|
| 3032 | | return 1; |
|---|
| 3033 | | } |
|---|
| 3034 | | } |
|---|
| 3035 | | |
|---|
| | 3018 | /* lock paged memory if needed */ |
|---|
| | 3019 | if (lock_memory) { |
|---|
| | 3020 | #ifdef HAVE_MLOCKALL |
|---|
| | 3021 | int res = mlockall(MCL_CURRENT | MCL_FUTURE); |
|---|
| | 3022 | if (res != 0) { |
|---|
| | 3023 | fprintf(stderr, "warning: -k invalid, mlockall() failed: %s\n", |
|---|
| | 3024 | strerror(errno)); |
|---|
| | 3025 | } |
|---|
| | 3026 | #else |
|---|
| | 3027 | fprintf(stderr, "warning: -k invalid, mlockall() not supported on this platform. proceeding without.\n"); |
|---|
| | 3028 | #endif |
|---|
| | 3029 | } |
|---|
| | 3030 | |
|---|
| | 3031 | /* lose root privileges if we have them */ |
|---|
| | 3032 | if (getuid() == 0 || geteuid() == 0) { |
|---|
| | 3033 | if (username == 0 || *username == '\0') { |
|---|
| | 3034 | fprintf(stderr, "can't run as root without the -u switch\n"); |
|---|
| | 3035 | return 1; |
|---|
| | 3036 | } |
|---|
| | 3037 | if ((pw = getpwnam(username)) == 0) { |
|---|
| | 3038 | fprintf(stderr, "can't find the user %s to switch to\n", username); |
|---|
| | 3039 | return 1; |
|---|
| | 3040 | } |
|---|
| | 3041 | if (setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) { |
|---|
| | 3042 | fprintf(stderr, "failed to assume identity of user %s\n", username); |
|---|
| | 3043 | return 1; |
|---|
| | 3044 | } |
|---|
| | 3045 | } |
|---|