Skip to content

Instantly share code, notes, and snippets.

@vfrico
Created May 15, 2018 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfrico/e554d2ae33471d40a40f70e249b1633b to your computer and use it in GitHub Desktop.
Save vfrico/e554d2ae33471d40a40f70e249b1633b to your computer and use it in GitHub Desktop.
Java wrapper in C
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/**
C program to launch a Java Jar (eg: Minecraft server)
The purpose is to create a binary file that can be set with the SETUID permissions and owned by root.
Using this way, the server can run on the 80 port without be asked the root password.
*/
int main (int argc, char *argv[])
{
//Ruta al ejecutable de Java
char * java = "/usr/lib/jvm/java-7-openjdk-amd64/bin/java";
printf("Ejecutable de java: %s\n", java);
char * jar = "/opt/servidor/minecraft_server.jar";
printf("Jar: %s\n", jar);
chdir("/opt/servidor/");
int pid;
int pid_hijo;
int status;
pid = fork();
switch(pid) {
case -1:
printf("Error al crear proceso %d\n",pid);
exit(1);
case 0:
//El hijo: que llame a Java
execlp("/usr/bin/java","/usr/bin/java","-jar","/opt/servidor/minecraft_server.jar", NULL);
exit(2);
default:
//El padre
wait(&status);
main(argc, argv);
break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment