How to use SU inside script?

hi guys,

I new to unix im generating a script within c code.
I need to upgrade the software to a newer version and in need su privilege.
what is the correct syntax in running su within the script?

i would greatly appreciate your help
many thanks
rafael

/* Y o u r   D e s c r i p t i o n                       */
/*                            AppBuilder Photon Code Lib */
/*                                         Version 2.03  */

/* Standard headers */
#include <stdio.h>
#include <unistd.h>

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>

/* Local headers */
#include "ablibs.h"
#include "AppGlobal.h"
#include "abimport.h"
#include "proto.h"
#include "version.h"

#include <sys/stat.h>
#include <sys/reboot.h>

struct stat sb;

using namespace std;

int
Yes( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo )
{
	// find where abc/123/version x is located
	system("find /fs/*/abc/123/*/ > /root/find.txt");

	// only read the first result
	std::string line;
	std::ifstream stream("/root/find.txt");
	getline(stream,line);

	// current version
	int maj = VERSION_MAJOR;
	int min = VERSION_MINOR;
	int pat = VERSION_PATCH;

	// comparing to
	int maj_comp = 0;
	int min_comp = 0;
	int pat_comp = 0;

	// check if it is a valid folder
	if (stat(line.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode))
	{
		unsigned found = line.find_last_of("/");

		stringstream line_to_int;
		line_to_int << line[found-5];
		line_to_int >> maj_comp;

		line_to_int.str("");
		line_to_int << line[found-3];
		line_to_int >> min_comp;

		line_to_int.str("");
		line_to_int << line[found-1];
		line_to_int >> pat_comp;

		//if ((maj_comp == maj) || ( min_comp == min) || ( pat_comp == pat))
		if ((maj_comp != maj) || ( min_comp != min) || ( pat_comp != pat))
		{
			std::ofstream installation("/root/upgrade.sh");

			if (installation.is_open())
			{
				installation  << "#!/bin/sh\n";
				//installation  << "su -c \n";
				installation  << "cd " << line <<"\n";
				installation  << "sh ./install\n";
				//installation  << "shutdown";
				installation.close();
			}

			system("chmod 777 /root/upgrade.sh");
			system("/root/upgrade.sh");

		}
		else
		{
			cout << "current version is up to date" << endl;
		}

	}

	// check path if it exist if it does check
	// if current version is greater than upgrade
	// perform upgrade

	// else display error message cannot find file
	// check if usb is properly connected
	// check if path is valid


	/* eliminate 'unreferenced' warnings */
	widget = widget, apinfo = apinfo, cbinfo = cbinfo;

	return( Pt_CONTINUE );

}

'su -c' does not read the lines after it, and expects a parameter.

su -c "cd whatever ; sh ./installation"

Also, su is going to ask for a password, so if this isn't run from a terminal, it won't work. (there are graphical su replacements, like kdesu.)

Also, 777 is not the magic sledgehammer to fix all permissions problems. If this script is to be run by root, 700 should be more than sufficient.

Also, I think you could replace 'find' with 'echo' here for a big performance improvement, since you don't care about finding the contents of those folders (find is recursive, it will locate everything inside /a/*/b.). The shell is the thing which handles these *'s here, not find.

Also, you dont' need to save into a temp file, use popen and read the string directly out of it.

Perhaps something like:

char buf[1024], *b;
FILE *p=popen("echo /fs/*/abc/123/*/", "r");
fgets(buf, 1024, p);
pclose(p);

// Find last /
b=strchr(buf, '/');
while(strchr(buf, '/')) b=strchr(b, '/');

// Check for filename like /0.1.2.... and read into ..._comp variables
if(sscanf(b, "/%d.%d.%d", &maj_comp, &min_comp, &patch_comp) == 3)
{
        ...
}
1 Like

the pipe helped.

i had permission issues on my first create file.
my code works now.

Thanks

1 Like