title: Example of using sysctl() call in C on Linux
body: <p>I've read some of the warnings against using the sysctl() call in C, and it seems if I cannot use sysctl() safely, the only other way I can find to make the needed change would be to use soemething like:</p>
  <pre class="lang-c prettyprint-override"><code>system(&quot;echo  fs.inotify.max_user_watches=NEW_MAX_DIRECTORIES  &gt;&gt; /etc/sysctl.conf&quot;);
  system(&quot;sysctl -p&quot;);
  </code></pre>
  <p>(of course, this assumes ensuring the binary is running as root.  However, I would rather NOT have to shell out using system calls.</p>
  <p>Can someone point me in the correct and safe of using sysctl()?</p>
  <p>here is a snippet of the code I am using.</p>
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;getopt.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;string.h&gt;
  #include &lt;errno.h&gt;        
  int main ()
  {
          int ret;
          const char *LOGNAME=&quot;iNotifyMonitor&quot;;
          logger(INFO, &quot;================================================&quot;);
          ret = startDaemon();
          daemonRunning = ret;
  
          if (ret == 0)
          {
                  daemonRunning = 1;
                  FIRST_RUN = 0;
          }
  
          if(ret)
          {
                  syslog(LOG_USER | LOG_ERR, &quot;Error starting iNotifyMonitor&quot;);
                  logger(ERR, &quot;Unable to start iNotifyMonitor&quot;);
                  closelog();
                  return EXIT_FAILURE;
          }
  
          signal(SIGINT, signalHandler);
          signal(SIGHUP, signalHandler);
          char *log_file_name = malloc(sizeof(char *) * sizeof(char *));
          sprintf(log_file_name, &quot;%s%s&quot;, INM_LOG_DIR, INM_LOG_FILE);
          /* Try to open log file to this daemon */
          if (INM_OPEN_LOG &amp;&amp; INM_LOG_FILE)
          {
          log_stream = fopen(concatString(INM_LOG_DIR, INM_LOG_FILE), &quot;a+&quot;);
                  if (log_stream == NULL)
                  {
                          char *errMsg;
                          sprintf(errMsg, &quot;Cannot open log file %s, error: %s&quot;, concatString(INM_LOG_DIR, INM_LOG_FILE), strerror(errno));
              log_stream = stdout;
          }
          }
          else
          {
                  log_stream = stdout;
      }
  
      while (daemonRunning == 1)
          {
          if (ret &lt; 0)
                  {
              logger(LOG_ERR, &quot;Can not write to log stream: %s, error: %s&quot;, (log_stream == stdout) ? &quot;stdout&quot; : log_file_name, strerror(errno));
                          break;
                  }
                  ret = fflush(log_stream);
                  if (ret != 0)
                  {
                  logger(LOG_ERR, &quot;Can not fflush() log stream: %s, error: %s&quot;,
                                  (log_stream == stdout) ? &quot;stdout&quot; : log_file_name, strerror(errno));
                          break;
                  }
                  int curcount =countDirectory(&quot;/home/darrinw/Development/CrossRoads/&quot;);
                  directoryCount = curcount;
                  if(directoryCounrt &gt; INM_MAX_DIRECTORIES)
                  {
                          int newVal = roundUp(directoryCount, 32768);
  
                          // call to sysctl() to modify fs.inotify.max_users_watches=newVal
                  }
  
  
          sleep(INM_SCAN_INTERVAL);
          }
  </code></pre>