title: Implementing a custom camera HAL in Android
body: <p>I'm working on a custom version of Android on a device with a closed source camera HAL, so I've had to add a camera wrapper, which is a lib that Android loads instead of the vendor camera, so that bugs and peculiarities with the vendor implementation can be worked around. I've found that I need to vary the behavior between apps, so that while in Snapchat I don't want to implement a certain parameter, for example, Hangouts crashes if I don't set it. The problem is Android doesn't tell the camera which app is using it. I tried the following code to see what my pid was but the result was "/system/bin/mediaserver" not exactly what I had in mind. Asking for parent id was even worse, it just returned 1. What I seek is trivial in Android java, but I'm constrained to C and whatever includes I can find in Bionic here. Any help?</p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  </code></pre>
  
  <p>later</p>
  
  <pre><code>const static char * whiteList[] = {"com.android.camera2"};
  const static int whiteListLen = 1;
  const pid_t pid = getpid();
  char plink[64];
  char pname[64];
  FILE *fptr;
  snprintf(plink, sizeof(plink), "/proc/%i/cmdline", pid);
  fptr = fopen(plink, "r");
  if (fptr != NULL) {
      ALOGD("Determining name of running PID %i", pid);
      fscanf(fptr, "%s", pname);
      ALOGD("Process is %s", pname);
      for(int i = 0; i &lt; whiteListLen; i++) {
          if(strcmp(whiteList[i], pname) == 0) {
              ALOGD("Camera opened by whitelisted app %s, enabling XXX.", pname);
              XXX;
              break;
              }
          }
  } else {
      ALOGD("Unable to open /proc/%i/cmdline", pid);
  }
  fclose(fptr);
  </code></pre>