]> WPIA git - gigi.git/blob - natives/org_cacert_gigi_natives_SetUID.c
Merge "fix: language detection pattern for Group description"
[gigi.git] / natives / org_cacert_gigi_natives_SetUID.c
1 #include <jni.h>
2 #include <sys/types.h>
3
4 #include <unistd.h>
5
6 #ifndef _Included_org_cacert_natives_SetUID
7 #define _Included_org_cacert_natives_SetUID
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 static jobject getStatus(JNIEnv *env, int successCode, const char * message) {
13     jstring message_str = (*env)->NewStringUTF(env, message);
14     jboolean success = successCode;
15     jclass cls = (*env)->FindClass(env, "Lorg/cacert/gigi/natives/SetUID$Status;");
16     jmethodID constructor = (*env)->GetMethodID(env, cls, "<init>", "(ZLjava/lang/String;)V");
17     return (*env)->NewObject(env, cls, constructor, success, message_str);
18 }
19
20 JNIEXPORT jobject JNICALL Java_org_cacert_gigi_natives_SetUID_setUid
21         (JNIEnv *env, jobject obj, jint uid, jint gid) {
22
23     /* We don't need the reference for the object/class we are working on */
24     (void)obj;
25
26     if(setgid((int)gid)) {
27         return (jobject)getStatus(env, 0, "Error while setting GID.");
28     }
29
30     if(setuid((int)uid)) {
31         return (jobject)getStatus(env, 0, "Error while setting UID.");
32     }
33
34     return (jobject)getStatus(env, 1, "Successfully set uid/gid.");
35 }
36
37 #ifdef __cplusplus
38 }
39 #endif
40 #endif