Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 6c21d55247420ace69cad1d05267fc34 > files > 276

pcsc-lite-doc-1.7.2-4.fc15.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>pcsc-lite: utils.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.7.4 -->
<div id="top">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">pcsc-lite&#160;<span id="projectnumber">1.7.2</span></div>
  </td>
 </tr>
 </tbody>
</table>
</div>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li><a href="dirs.html"><span>Directories</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><a class="el" href="dir_7a4553ee0c3b13d0e595a5d1659110a6.html">src</a>      </li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">utils.c</div>  </div>
</div>
<div class="contents">
<a href="utils_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * MUSCLE SmartCard Development ( http://www.linuxnet.com )</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * Copyright (C) 2006-2009</span>
<a name="l00005"></a>00005 <span class="comment"> *  Ludovic Rousseau &lt;ludovic.rousseau@free.fr&gt;</span>
<a name="l00006"></a>00006 <span class="comment"> *</span>
<a name="l00007"></a>00007 <span class="comment"> * $Id: utils.c 5674 2011-03-26 19:53:47Z rousseau $</span>
<a name="l00008"></a>00008 <span class="comment"> */</span>
<a name="l00009"></a>00009 
<a name="l00015"></a>00015 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00016"></a>00016 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
<a name="l00017"></a>00017 <span class="preprocessor">#include &lt;unistd.h&gt;</span>
<a name="l00018"></a>00018 <span class="preprocessor">#include &lt;errno.h&gt;</span>
<a name="l00019"></a>00019 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00020"></a>00020 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00021"></a>00021 <span class="preprocessor">#include &lt;signal.h&gt;</span>
<a name="l00022"></a>00022 <span class="preprocessor">#include &lt;dirent.h&gt;</span>
<a name="l00023"></a>00023 <span class="preprocessor">#include &lt;fcntl.h&gt;</span>
<a name="l00024"></a>00024 <span class="preprocessor">#include &lt;pthread.h&gt;</span>
<a name="l00025"></a>00025 
<a name="l00026"></a>00026 <span class="preprocessor">#include &quot;config.h&quot;</span>
<a name="l00027"></a>00027 <span class="preprocessor">#include &quot;<a class="code" href="debuglog_8h.html" title="This handles debugging.">debuglog.h</a>&quot;</span>
<a name="l00028"></a>00028 <span class="preprocessor">#include &quot;utils.h&quot;</span>
<a name="l00029"></a>00029 <span class="preprocessor">#include &quot;<a class="code" href="pcscd_8h.html" title="This keeps a list of defines for pcsc-lite.">pcscd.h</a>&quot;</span>
<a name="l00030"></a>00030 <span class="preprocessor">#include &quot;<a class="code" href="sys__generic_8h.html" title="This handles abstract system level calls.">sys_generic.h</a>&quot;</span>
<a name="l00031"></a>00031 
<a name="l00032"></a>00032 pid_t GetDaemonPid(<span class="keywordtype">void</span>)
<a name="l00033"></a>00033 {
<a name="l00034"></a>00034     <span class="keywordtype">int</span> fd;
<a name="l00035"></a>00035     pid_t pid;
<a name="l00036"></a>00036 
<a name="l00037"></a>00037     <span class="comment">/* pids are only 15 bits but 4294967296</span>
<a name="l00038"></a>00038 <span class="comment">     * (32 bits in case of a new system use it) is on 10 bytes</span>
<a name="l00039"></a>00039 <span class="comment">     */</span>
<a name="l00040"></a>00040     fd = open(PCSCLITE_RUN_PID, O_RDONLY);
<a name="l00041"></a>00041     <span class="keywordflow">if</span> (fd &gt;= 0)
<a name="l00042"></a>00042     {
<a name="l00043"></a>00043         <span class="keywordtype">char</span> pid_ascii[PID_ASCII_SIZE];
<a name="l00044"></a>00044 
<a name="l00045"></a>00045         (void)read(fd, pid_ascii, PID_ASCII_SIZE);
<a name="l00046"></a>00046         (void)close(fd);
<a name="l00047"></a>00047 
<a name="l00048"></a>00048         pid = atoi(pid_ascii);
<a name="l00049"></a>00049     }
<a name="l00050"></a>00050     <span class="keywordflow">else</span>
<a name="l00051"></a>00051     {
<a name="l00052"></a>00052         Log2(PCSC_LOG_CRITICAL, <span class="stringliteral">&quot;Can&#39;t open &quot;</span> PCSCLITE_RUN_PID <span class="stringliteral">&quot;: %s&quot;</span>,
<a name="l00053"></a>00053             strerror(errno));
<a name="l00054"></a>00054         <span class="keywordflow">return</span> -1;
<a name="l00055"></a>00055     }
<a name="l00056"></a>00056 
<a name="l00057"></a>00057     <span class="keywordflow">return</span> pid;
<a name="l00058"></a>00058 } <span class="comment">/* GetDaemonPid */</span>
<a name="l00059"></a>00059 
<a name="l00060"></a>00060 <span class="keywordtype">int</span> SendHotplugSignal(<span class="keywordtype">void</span>)
<a name="l00061"></a>00061 {
<a name="l00062"></a>00062     pid_t pid;
<a name="l00063"></a>00063 
<a name="l00064"></a>00064     pid = GetDaemonPid();
<a name="l00065"></a>00065 
<a name="l00066"></a>00066     <span class="keywordflow">if</span> (pid != -1)
<a name="l00067"></a>00067     {
<a name="l00068"></a>00068         Log2(PCSC_LOG_INFO, <span class="stringliteral">&quot;Send hotplug signal to pcscd (pid=%d)&quot;</span>, pid);
<a name="l00069"></a>00069         <span class="keywordflow">if</span> (kill(pid, SIGUSR1) &lt; 0)
<a name="l00070"></a>00070         {
<a name="l00071"></a>00071             Log3(PCSC_LOG_CRITICAL, <span class="stringliteral">&quot;Can&#39;t signal pcscd (pid=%d): %s&quot;</span>,
<a name="l00072"></a>00072                 pid, strerror(errno));
<a name="l00073"></a>00073             <span class="keywordflow">return</span> EXIT_FAILURE ;
<a name="l00074"></a>00074         }
<a name="l00075"></a>00075         (void)<a class="code" href="sys__generic_8h.html#abbd906a9a16166248e32862f67fc49cd" title="Makes the current process sleep for some seconds.">SYS_Sleep</a>(1);
<a name="l00076"></a>00076     }
<a name="l00077"></a>00077 
<a name="l00078"></a>00078     <span class="keywordflow">return</span> EXIT_SUCCESS;
<a name="l00079"></a>00079 } <span class="comment">/* SendHotplugSignal */</span>
<a name="l00080"></a>00080 
<a name="l00088"></a><a class="code" href="utils_8c.html#a6c89a6b233096b6a80880c82886f380b">00088</a> <span class="preprocessor">#define OPENCT_FILE &quot;/var/run/openct/status&quot;</span>
<a name="l00089"></a>00089 <span class="preprocessor"></span><span class="keywordtype">int</span> CheckForOpenCT(<span class="keywordtype">void</span>)
<a name="l00090"></a>00090 {
<a name="l00091"></a>00091     <span class="keyword">struct </span>stat buf;
<a name="l00092"></a>00092 
<a name="l00093"></a>00093     <span class="keywordflow">if</span> (0 == stat(<a class="code" href="utils_8c.html#a6c89a6b233096b6a80880c82886f380b" title="Check is OpenCT is running and display a critical message if it is.">OPENCT_FILE</a>, &amp;buf))
<a name="l00094"></a>00094     {
<a name="l00095"></a>00095         Log1(PCSC_LOG_CRITICAL, <span class="stringliteral">&quot;File &quot;</span> <a class="code" href="utils_8c.html#a6c89a6b233096b6a80880c82886f380b" title="Check is OpenCT is running and display a critical message if it is.">OPENCT_FILE</a> <span class="stringliteral">&quot; found. Remove OpenCT and try again&quot;</span>);
<a name="l00096"></a>00096         <span class="keywordflow">return</span> 1;
<a name="l00097"></a>00097     }
<a name="l00098"></a>00098 
<a name="l00099"></a>00099     <span class="keywordflow">return</span> 0;
<a name="l00100"></a>00100 } <span class="comment">/* CheckForOpenCT */</span>
<a name="l00101"></a>00101 
<a name="l00106"></a><a class="code" href="utils_8c.html#a470f4ac4abe231a7b3346be4fe455912">00106</a> <span class="keywordtype">long</span> <span class="keywordtype">int</span> <a class="code" href="utils_8c.html#a470f4ac4abe231a7b3346be4fe455912" title="return the difference (as long int) in µs between 2 struct timeval r = a - b">time_sub</a>(<span class="keyword">struct</span> timeval *a, <span class="keyword">struct</span> timeval *b)
<a name="l00107"></a>00107 {
<a name="l00108"></a>00108     <span class="keyword">struct </span>timeval r;
<a name="l00109"></a>00109     r.tv_sec = a -&gt; tv_sec - b -&gt; tv_sec;
<a name="l00110"></a>00110     r.tv_usec = a -&gt; tv_usec - b -&gt; tv_usec;
<a name="l00111"></a>00111     <span class="keywordflow">if</span> (r.tv_usec &lt; 0)
<a name="l00112"></a>00112     {
<a name="l00113"></a>00113         r.tv_sec--;
<a name="l00114"></a>00114         r.tv_usec += 1000000;
<a name="l00115"></a>00115     }
<a name="l00116"></a>00116 
<a name="l00117"></a>00117     <span class="keywordflow">return</span> r.tv_sec * 1000000 + r.tv_usec;
<a name="l00118"></a>00118 } <span class="comment">/* time_sub */</span>
<a name="l00119"></a>00119 
<a name="l00120"></a>00120 <span class="keywordtype">int</span> ThreadCreate(pthread_t * pthThread, <span class="keywordtype">int</span> attributes,
<a name="l00121"></a>00121     PCSCLITE_THREAD_FUNCTION(pvFunction), LPVOID pvArg)
<a name="l00122"></a>00122 {
<a name="l00123"></a>00123     pthread_attr_t attr;
<a name="l00124"></a>00124     <span class="keywordtype">int</span> ret;
<a name="l00125"></a>00125 
<a name="l00126"></a>00126     ret = pthread_attr_init(&amp;attr);
<a name="l00127"></a>00127     <span class="keywordflow">if</span> (ret)
<a name="l00128"></a>00128         <span class="keywordflow">return</span> ret;
<a name="l00129"></a>00129 
<a name="l00130"></a>00130     ret = pthread_attr_setdetachstate(&amp;attr,
<a name="l00131"></a>00131         attributes &amp; THREAD_ATTR_DETACHED ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE);
<a name="l00132"></a>00132     <span class="keywordflow">if</span> (ret)
<a name="l00133"></a>00133     {
<a name="l00134"></a>00134         (void)pthread_attr_destroy(&amp;attr);
<a name="l00135"></a>00135         <span class="keywordflow">return</span> ret;
<a name="l00136"></a>00136     }
<a name="l00137"></a>00137 
<a name="l00138"></a>00138     ret = pthread_create(pthThread, &amp;attr, pvFunction, pvArg);
<a name="l00139"></a>00139     <span class="keywordflow">if</span> (ret)
<a name="l00140"></a>00140         <span class="keywordflow">return</span> ret;
<a name="l00141"></a>00141 
<a name="l00142"></a>00142     ret = pthread_attr_destroy(&amp;attr);
<a name="l00143"></a>00143     <span class="keywordflow">return</span> ret;
<a name="l00144"></a>00144 }
</pre></div></div>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Thu Sep 15 2011 for pcsc-lite by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
</body>
</html>