<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]无论在root用户下面依然是xiangdong用户提交git，git push均以xiangdong用户提交小工具，防止误以root用户提交。周边知识之C语言getcwd()函数：取得当前的工作目录，获取被执行程序的绝对目录,]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[版本控制]]></category>
<pubDate>Sun, 01 Oct 2017 13:57:25 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	背景：Git分不同角色在提交时，root提交时是administrator（Crontab用来Pull一些Ansible脚本到本地）, 而xiangdong提交时是xiangdong，往往容易出现一个问题是不小心给root了，这块一是xiangdong上去有时权限不够可能切换为root了，另一个是登录时可能就是Root用户，一提交就成Admin了。鉴于此，自己整个C代码实现提交判断，这样接管了git push，从而对粗心的我来讲起到一个助理的作用。<br/><br/>一、搞这玩意儿的原因：<br/>root情况：<br/><textarea name="code" class="php" rows="15" cols="100">
git config -l
user.name=Administrator
user.email=admin@example.com
</textarea><br/>xiangdong:<br/><textarea name="code" class="php" rows="15" cols="100">
git config -l
user.name=xiangdong
user.email=xiangdong@justwinit.cn
core.autocrlf=input
</textarea><br/>解决一个无论是root还是xiangdong的环境下，只需要执行下面我们搞的这个c语言生成的二进制文件就能一直只以向东的用户进行提交git，看起来扯淡，但还是有点用的，敲啥呢： gitpush ，和git push只是没有那个空格了。经实践证明是可行的，如下：<br/><br/><br/>二、生产这个玩意的过程：<br/>源码及Makefile和生成的二进制文件路径：<br/><textarea name="code" class="php" rows="15" cols="100">
#tree -L 2 ../../gitpush/
../../gitpush/
├── bin
│   └── gitpush
└── src
&nbsp;&nbsp;&nbsp;&nbsp;├── gitpush.c
&nbsp;&nbsp;&nbsp;&nbsp;└── Makefile
</textarea><br/><br/>Makefile文件内容：<br/>cat Makefile <br/><textarea name="code" class="php" rows="15" cols="100">
gitpush:gitpush.c
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gcc -g -o&nbsp;&nbsp;gitpush -Wall gitpush.c
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mv gitpush ../bin
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;chmod a+x ../bin/gitpush
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cp -rf /usr/local/gitpush/src/gitpush.sh /etc/profile.d/gitpush.sh
clean:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rm -rf gitpush&nbsp;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rm -rf /etc/profile.d/gitpush.sh
</textarea><br/>源代码：cat gitpush.c <br/><textarea name="code" class="php" rows="15" cols="100">
#include &lt;stdio.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdarg.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;pwd.h&gt;
int main()
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char curpath[80];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char forbidname[]=&quot;root&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char whoami[80];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char command[36000];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FILE *pPipe;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char psBuffer[36000];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;struct passwd *pwd;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pwd = getpwuid(getuid());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//printf(&quot;当前登陆的用户名为：%s&#92;n&quot;, pwd-&gt;pw_name);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(whoami,pwd-&gt;pw_name);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getcwd(curpath, sizeof(curpath));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//printf(&quot;current working directory : %s&#92;n&quot;, curpath);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//printf(&quot;The UID is %d &#92;n&quot;, getuid());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//strcpy(whoami,getlogin()); #要是先xiangdong登录，再sudo su -成root，值还是向东。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//printf(&quot;The login name is %s&#92;n&quot;, getlogin());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//printf(&quot;The forbidlogin name is %s&#92;n&quot;, forbidname);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(0 == strcmp(forbidname,whoami))&#123;//root，得提示一下换角色再Push
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;提醒：请小心，现在正在Root环境下面，立即切换到xiangdong用户并作提交。&#92;n&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;snprintf(command,sizeof(command),&quot;/usr/bin/sudo -i -u xiangdong -H cd %s &amp;&amp; git push&quot;,curpath);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;else&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;snprintf(command,sizeof(command),&quot;cd %s &amp;&amp; git push&quot;,curpath);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;执行:%s&#92;n&quot;,command);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( (pPipe = popen( command, &quot;r&quot; )) == NULL )&#123;//执行脚本
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Excute Command Faild :%s&quot;,command);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit( 1 );
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(!feof(pPipe))/* Read pipe until end of file. */
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(fgets(psBuffer, sizeof(psBuffer), pPipe) != NULL)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%s&quot;,psBuffer);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pclose(pPipe);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;
&#125;
</textarea><br/><br/><br/>最后，PATH路径：<br/>cat /etc/profile.d/gitpush.sh <br/><textarea name="code" class="php" rows="15" cols="100">
export PATH=$PATH:/usr/local/gitpush/bin
</textarea><br/><br/>三、验证步骤阶段：<br/>root下执行情况：<br/>#gitpush<br/>#gitpush<br/>当前登陆的用户名为：root<br/>current working directory : /home/xiangdong/shell<br/>The UID is 0 <br/>The login name is xiangdong<br/>The forbidlogin name is root<br/>请小心，你现在正在Root环境下面，不过没关系，我给你切换到xiangdong用户并作提交。执行git push命令：/usr/bin/sudo -i -u xiangdong -H cd /home/xiangdong/shell &amp;&amp; git push<br/>Counting objects: 4, done.<br/>Delta compression using up to 4 threads.<br/>Compressing objects: 100% (2/2), done.<br/>Writing objects: 100% (3/3), 288 bytes, done.<br/>Total 3 (delta 1), reused 1 (delta 0)<br/>To git@gitlab.qr.justwinit.net:levooops/shell.git<br/>&nbsp;&nbsp; bd83c51..b400811&nbsp;&nbsp;master -&gt; master<br/><br/>xiangdong下面执行情况：<br/>$gitpush<br/>执行:cd /home/xiangdong/shell &amp;&amp; git push<br/>Counting objects: 5, done.<br/>Delta compression using up to 4 threads.<br/>Compressing objects: 100% (2/2), done.<br/>Writing objects: 100% (3/3), 277 bytes, done.<br/>Total 3 (delta 1), reused 0 (delta 0)<br/>To git@gitlab.qr.justwinit.net:levooops/shell.git<br/>&nbsp;&nbsp; b400811..0b19550&nbsp;&nbsp;master -&gt; master<br/><br/>成功Log:<br/>$git log<br/>commit 0b195507efa6278774d4f500a7086003d6744684<br/>Author: xiangdong &lt;xiangdong@justwinit.cn&gt;<br/>Date:&nbsp;&nbsp; Sun Oct 1 23:15:32 2017 +0800<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;加上README.md<br/><br/>commit b40081190bf020d49375a4117a181f00ea0a2503<br/>Author: xiangdong &lt;xiangdong@justwinit.cn&gt;<br/>Date:&nbsp;&nbsp; Sun Oct 1 23:09:58 2017 +0800<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;加上README.md<br/><br/>================================================================================<br/>附录：<br/>为什么 sudo cd 会报错 command not found:<br/>https://www.starduster.me/2015/09/12/why-command-not-found-when-sudo-cd/<br/><br/>相关函数：get_current_dir_name, getwd, chdir<br/>头文件：#include &lt;unistd.h&gt;<br/>定义函数：char * getcwd(char * buf, size_t size);<br/>函数说明：getcwd()会将当前的工作目录绝对路径复制到参数buf 所指的内存空间，参数size 为buf 的空间大小。<br/>注：<br/>1、在调用此函数时，buf 所指的内存空间要足够大。若工作目录绝对路径的字符串长度超过参数size 大小，则返回NULL，errno 的值则为ERANGE。<br/>2、倘若参数buf 为NULL，getcwd()会依参数size 的大小自动配置内存(使用malloc())，如果参数size 也为0，则getcwd()会依工作目录绝对路径的字符串程度来决定所配置的内存大小，进程可以在使用完次字符串后利用free()来释放此空间。<br/><br/>返回值：执行成功则将结果复制到参数buf 所指的内存空间, 或是返回自动配置的字符串指针. 失败返回NULL,错误代码存于errno.<br/><br/>范例<br/><textarea name="code" class="php" rows="15" cols="100">
#include &lt;unistd.h&gt;
main()
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;char buf[80];
&nbsp;&nbsp;&nbsp;&nbsp;getcwd(buf, sizeof(buf));
&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;current working directory : %s&#92;n&quot;, buf);
&#125;
</textarea><br/><br/>执行：<br/>current working directory :/tmp<br/><br/>cat test_processname.cpp<br/><textarea name="code" class="php" rows="15" cols="100">
#include &lt;limits.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;unistd.h&gt;
size_t get_executable_path( char* processdir,char* processname, size_t len)
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char* path_end;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(readlink(&quot;/proc/self/exe&quot;, processdir,len) &lt;=0)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path_end = strrchr(processdir,&nbsp;&nbsp;&#039;/&#039;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(path_end == NULL)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return -1;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;++path_end;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strcpy(processname, path_end);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*path_end = &#039;&#92;0&#039;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return (size_t)(path_end - processdir);
&#125;
int main()
&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char path[PATH_MAX];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char processname[1024];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get_executable_path(path, processname, sizeof(path));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;directory:%s&#92;nprocessname:%s&#92;n&quot;,path,processname);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char buf[80];
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getcwd(buf, sizeof(buf));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;current working directory : %s&#92;n&quot;, buf);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;The UID is %d &#92;n&quot;, getuid());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;The login name is %s&#92;n&quot;, getlogin());
&#125;
</textarea><br/><br/><br/>g++ test_processname.cpp -o gitpush<br/>mv /tmp/gp /usr/local/gitpush/gitpush <br/><br/>cat /etc/profile.d/gp.sh <br/>export PATH=$PATH:/usr/local/gitpush<br/><br/>#gp<br/>directory:/usr/local/gitpush/<br/>processname:gp<br/>current working directory : /root<br/><br/><br/>./gitpush<br/>directory:/tmp/<br/>processname:gitpush<br/>current working directory : /tmp<br/>The UID is 0 <br/>The login name is xiangdong
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]无论在root用户下面依然是xiangdong用户提交git，git push均以xiangdong用户提交小工具，防止误以root用户提交。周边知识之C语言getcwd()函数：取得当前的工作目录，获取被执行程序的绝对目录,]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>